tree-traversal

Print all paths from root to leaf in a Binary tree

若如初见. 提交于 2019-12-01 00:04:12
Here is the code I wrote to print all paths of a Binary tree from root to leaf: public static void printRootToLeaf(Node1 root, List<Integer> list) { if(root == null) { return; } list.add(root.data); if(root.left == null && root.right == null) { System.out.println(list); return; } printRootToLeaf(root.left, list); printRootToLeaf(root.right, list); } I am calling this method in main like this: public static void main(String[] args) { Node1 root = new Node1(1); Node1 two = new Node1(2); Node1 three = new Node1(3); Node1 four = new Node1(4); Node1 five = new Node1(5); Node1 six = new Node1(6);

Build all Hamiltonian paths from an edge list

流过昼夜 提交于 2019-11-30 15:55:40
I'm having trouble finding a way to build a tree path from a list of related tuples? I only want a list of every path where each node is visited once, aka hamiltonian path. I keep getting close but missing some path. For example, let's say we have this list of connections: connections = [(1, 4), (1, 5), (2, 5), (3, 4), (4, 1), (4, 3), (4, 5), (5, 1), (5, 2), (5, 4)] desired output: [[1,4,3], [1,4,5,2], [1,5,2], [1,5,4,3], [2,5,1,4,3], [2,5,4,1], [2,5,4,3], [3,4,1,5,2], [3,4,5,1], [3,4,5,2], [4, 3], [4,1,5,2], [4,5,1], [4,5,2], [5, 2], [5,1,4,3], [5,4,1], [5,4,3] ] So each possible path is

Tree traversal algorithm for directory structures with a lot of files

南笙酒味 提交于 2019-11-30 06:47:56
When recursively traversing through a directory structure, what is the most efficient algorithm to use if you have more files than directories? I notice that when using depth-first traversal, it seems to take longer when there are a lot of files in a given directory. Does breadth-first traversal work more efficiently in this case? I have no way to profile the two algorithms at the moment so your insights are very much welcome. EDIT: In response to alphazero's comment, I'm using PHP on a Linux machine. Igor Serebryany It makes sense that breadth-first would work better. When you enter your root

Traversing through all nodes of a binary tree in Java

空扰寡人 提交于 2019-11-30 03:57:48
Let's say I have a simple binary tree node class, like so: public class BinaryTreeNode { public String identifier = ""; public BinaryTreeNode parent = null; public BinaryTreeNode left = null; public BinaryTreeNode right = null; public BinaryTreeNode(BinaryTreeNode parent, String identifier) { this.parent = parent; //passing null makes this the root node this.identifier = identifier; } public boolean IsRoot() { return parent == null; } } How would I add a method which is able to recursively traverse through any size tree, visiting each and every existing node from left to right, without

Inorder tree traversal: Which definition is correct?

不想你离开。 提交于 2019-11-29 22:55:06
I have the following text from an academic course I took a while ago about inorder traversal (they also call it pancaking) of a binary tree (not BST): Inorder tree traversal Draw a line around the outside of the tree. Start to the left of the root, and go around the outside of the tree, to end up to the right of the root. Stay as close to the tree as possible, but do not cross the tree. (Think of the tree — its branches and nodes — as a solid barrier.) The order of the nodes is the order in which this line passes underneath them. If you are unsure as to when you go “underneath” a node,

Build all Hamiltonian paths from an edge list

烂漫一生 提交于 2019-11-29 22:49:39
问题 I'm having trouble finding a way to build a tree path from a list of related tuples? I only want a list of every path where each node is visited once, aka hamiltonian path. I keep getting close but missing some path. For example, let's say we have this list of connections: connections = [(1, 4), (1, 5), (2, 5), (3, 4), (4, 1), (4, 3), (4, 5), (5, 1), (5, 2), (5, 4)] desired output: [[1,4,3], [1,4,5,2], [1,5,2], [1,5,4,3], [2,5,1,4,3], [2,5,4,1], [2,5,4,3], [3,4,1,5,2], [3,4,5,1], [3,4,5,2],

Pre-order to post-order traversal

一曲冷凌霜 提交于 2019-11-29 21:35:46
If the pre-order traversal of a binary search tree is 6, 2, 1, 4, 3, 7, 10, 9, 11, how to get the post-order traversal? You are given the pre-order traversal of the tree, which is constructed by doing: output, traverse left, traverse right. As the post-order traversal comes from a BST, you can deduce the in-order traversal (traverse left, output, traverse right) from the post-order traversal by sorting the numbers. In your example, the in-order traversal is 1, 2, 3, 4, 6, 7, 9, 10, 11. From two traversals we can then construct the original tree. Let's use a simpler example for this: Pre-order:

Help me understand Inorder Traversal without using recursion

99封情书 提交于 2019-11-29 18:53:05
I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion. This is what I've tried so far: def traverseInorder(node): lifo = Lifo() lifo.push(node) while True: if node is None: break if node.left is not None: lifo.push(node.left) node = node.left continue prev = node while True: if node is None: break print node.value prev = node node = lifo.pop() node = prev if node.right is not None: lifo.push(node.right) node = node.right else: break

How to create an array from this result set (nested categories stored in databased with traversal model)?

我们两清 提交于 2019-11-29 15:40:39
问题 Based on this question: Getting a modified preorder tree traversal model (nested set) into a <ul> The logic bellow is used to build an ordered list, but how to do the same with an array? I want to build a nested array. // bootstrap loop $result = ''; $currDepth = -1; // -1 to get the outer <ul> while (!empty($tree)) { $currNode = array_shift($tree); // Level down? if ($currNode['depth'] > $currDepth) { // Yes, open <ul> $result .= '<ul>'; } // Level up? if ($currNode['depth'] < $currDepth) {

Tree traversal algorithm for directory structures with a lot of files

独自空忆成欢 提交于 2019-11-29 07:52:55
问题 When recursively traversing through a directory structure, what is the most efficient algorithm to use if you have more files than directories? I notice that when using depth-first traversal, it seems to take longer when there are a lot of files in a given directory. Does breadth-first traversal work more efficiently in this case? I have no way to profile the two algorithms at the moment so your insights are very much welcome. EDIT: In response to alphazero's comment, I'm using PHP on a Linux