tree-traversal

Can every recursive process be transformed into an iterative process?

半城伤御伤魂 提交于 2019-12-10 18:25:58
问题 I was reading the book, Structure and Interpretation of Computer Programs , where in it tells about the distinction between a recursive procedure and recursive process, and similarly between iterative procedure and iterative process. So, a recursive procedure could still generate an iterative process. My question is: given a procedure which generates a recursive process, can you always write another procedure that achieves the same result but generates an iterative process? The specific

How to do a Level Order Traversal? [duplicate]

余生颓废 提交于 2019-12-10 17:39:01
问题 This question already has answers here : Binary tree level order traversal (5 answers) Closed 2 years ago . I'm trying to do a linear order traversal on a binary tree but having trouble getting the correct output. Basically I have created a queue and start by enqueuing the root, then until the queue is empty I dequeue the first element and add its children to the end of the queue. When dequeuing it returns a generic element (). I have a problem in converting this element to a tree node so

Why is inorder and preorder traversal useful for creating an algorithm to decide if T2 is a subtree of T1

本小妞迷上赌 提交于 2019-12-09 05:25:04
问题 I'm looking at an interview book and the question is: You have two very large binary trees: T1 , with millions of nodes, and T2 , with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 . The authors mentions this as a possible solution: Note that the problem here specifies that T1 has millions of nodes—this means that we should be careful of how much space we use. Let’s say, for example, T1 has 10 million nodes—this means that the data alone is about 40 mb . We could

Print all paths from root to leaf in a Binary tree

不想你离开。 提交于 2019-12-09 01:22:54
问题 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

Traversing an imbalanced binary tree in array form

孤人 提交于 2019-12-07 22:28:08
问题 An imbalanced (or non-heap) binary tree can be represented using an array as follows: array = [1, 2, null, 3, 4, 5, 6, null, 7, 8, null] 1 / \ 2 null / \ 3 4 / \ / \ 5 6 null 7 / \ 8 null How can I do a tree traversal using the given array? More specifically, how can I calculate a parent's left and right children's indices? In a balanced tree (such as a heap tree), we can easily calculate two children indices using their parent's index, and vice versa, as follows. leftChildIdx = 2 * parentIdx

In Eclipse JDT Java parser, is it possible to traverse the AST node by node without the using of visitors?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 14:21:19
问题 The standard way to access information on Nodes through the Eclipse JDT API is with Visitor's pattern. For example: unit.accept(new MyVisitorAdapter<Object>() { @Override public void visit(MethodCallExpr node, Object arg) { System.out.println("found method call: " + node.toString()); } }, null); In this case, to visit a node I need to specify what type of node I'm interested ( MethodCallExpr for this case). But, to proceed to access node information in a generic way, I should override all the

Arangodb custom filter/visitor for my tree graph

主宰稳场 提交于 2019-12-07 12:13:07
问题 I have a graph with two edge definitions like this: isDepartment: [organisation] -> [organisation] hasAccess: [user] -> [organisation] Organisations are nested in a tree (no cycles). There are multiple top-level organisations without any incoming isDepartment edges. Users are granted access to one or more organisations. These can be top-level organisations or organisations somewhere lower down the tree. If a user has access to an organisation, it has access to all child organisations. I am

Time complexity of level order traversal

最后都变了- 提交于 2019-12-07 07:03:31
问题 What is the time complexity of binary tree level order traversal ? Is it O(n) or O(log n)? void levelorder(Node *n) { queue < Node * >q; q.enqueue(n); while(!q.empty()) { Node * node = q.front(); DoSmthwith node; q.dequeue(); if(node->left != NULL) q.enqueue(node->left); if (node->right != NULL) q.enqueue(node->right); } } 回答1: It is O(n) , or to be exact Theta(n) . Have a look on each node in the tree - each node is "visited" at most 3 times, and at least once) - when it is discovered (all

explain the Haskell breadth first numbering code to traverse trees

社会主义新天地 提交于 2019-12-07 04:10:43
问题 I am reading this paper by Chris Okasaki; titled "Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design". A question is - how is the magic happening in the algorithm? There are some figures (e.g. figure 7 titled "threading the output of one level into the input of next level") Unfortunately, maybe it's only me, but that figure has completely baffled me. I don't understand how the threading happens at all? 回答1: Breadth first traversal means traversing levels of a tree one

Recursive tree traversal in level order

强颜欢笑 提交于 2019-12-06 14:17:45
问题 I have the following recursive data structure and a method iterating over it. Whilst doing so it should add a unique number n to each node e.g. its respective number in level order traversal of the tree. var data = { children: [ { children: [ ... ] }, { children: [ ... ] }, { children: [ ... ] }, ... ] } var process = function (node) { node.children.forEach(child, function () { process(child); }); return node; } How can I achieve this without changes to the data structure and minimal changes