RecursiveIteratorIterator and RecursiveDirectoryIterator to nested html lists

Here is one using DomDocument The basic idea is the contents of each directory is represented by a <ul> and each element in the directory by a <li> If element is a non-empty directory it will contain a <ul> to represent its contens and so on. $path = $_SERVER[‘DOCUMENT_ROOT’].’/test’; $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); … Read more

Cross product of sets using recursion

The simplest recursive definition of the cartesian product I can think of looks like this. You can see that like yours, this has a loop — actually, two loops embedded in a list comprehension. Unlike yours, this can handle two or more sequences: def product(*seqs): if not seqs: return [[]] else: return [[x] + p … Read more

How to deep copy a Binary Tree?

try class Node { private String value; private Node left; private Node right; public Node(String value, Node left, Node right) { this.value = value; … } Node copy() { Node left = null; Node right = null; if (this.left != null) { left = this.left.copy(); } if (this.right != null) { right = this.right.copy(); } … Read more

Traverse a XML using Recursive function

using System.Xml; namespace ConsoleApplication1 { class Program { static void Main( string[] args ) { var doc = new XmlDocument(); // Load xml document. TraverseNodes( doc.ChildNodes ); } private static void TraverseNodes( XmlNodeList nodes ) { foreach( XmlNode node in nodes ) { // Do something with the node. TraverseNodes( node.ChildNodes ); } } } … Read more