tree-traversal

C++ design question on traversing binary trees

I have a binary tree T which I would like to copy to another tree. Suppose I have a visit method that gets evaluated at every node: struct visit { virtual void operator() (node* n)=0; }; and I have a visitor algorithm void visitor(node* t, visit& v) { //do a preorder traversal using stack or recursion if (!t) return; v(t); v...

Can I do inorder traversal of a binary tree without recursion and stack?

Can anyone give me a solution for traversing a binary tree in inorder without recursion and without using a stack? ...

How to modify preorder tree traversal algorithm to handle nodes with multiple parents?

I've been searching for a while now and can't seem to find an alternative solution. I need the tree traversal algorithm in such a way that a node can have more than 1 parent, if it's possible (found a great article here: Storing Hierarchical Data in a Database). Are there any algorithms so that, starting from a root node, we can determi...

Java Binary Tree. Priting InOrder traversal

I am having some problems printing an inOrder traversal of my binary tree. Even after inserting many items into the tree it's only printing 3 items. public class BinaryTree { private TreeNode root; private int size; public BinaryTree(){ this.size = 0; } public boolean insert(TreeNode node){ if( ro...

How to traverse a Btree?

I have a Btree and I'm trying to figure out how traverse it so that the keys are displayed ascending order. All I can figure out is that this can be done with a recursive function. What's the pseudo-code to do it? ...

jQuery: Giving each matched element an unique ID

I am writing an 'inline translator' application to be used with a cloud computing platform to extend non-supported languages. The majority of this uses jQuery to find the text value, replace it with the translation, then append the element with a span tag that has an unique ID, to be used elsewhere within the application. The problem ar...

How do I output the preorder traversal of a tree given the inorder and postorder tranversal?

Given the code for outputing the postorder traversal of a tree when I have the preorder and the inorder traversal in an interger array. How do I similarily get the preorder with the inorder and postorder array given? void postorder( int preorder[], int prestart, int inorder[], int inostart, int length) { if(length==0) return; //termi...

BST preorder traversal and writting tree content to temporary array

I'm trying to write binary search tree's content to temporary array in order to use in main. However I'm not sure how to do it... I have tried something like this: void Book::preorder(TreeNode *ptr, Person &temp[], int x) { if(ptr!=NULL) { temp[x].name=ptr->item.name; x++; preorder(ptr->left, temp, x); preorder(ptr->right, te...

Postorder Traversal

In-order tree traversal obviously has application; getting the contents in order. Preorder traversal seems really useful for creating a copy of the tree. Is there a common use for postorder traversal of a binary tree? ...

SceneGraph traversal in Haskell

I want to implement a simple SceneGraph in Haskell using Data.Tree consisting of Transform and Shape nodes. In a SceneGraph the spatial transformation is accumulated while traversing and applied to the shape for rendering. type Transform = Vector2 Double data Shape = Circle Double | Square Double data SceneNode = XFormNode Transform...

Bin Tree Post Order Traversal, No recursion, no node flag

Is there another way to do this? Just spent 2 hours trying to figure it out. I have a solution (see DumpPostOrder below) however, is there is a better or more efficient method? It feels like there may be. Rules are - no recursion, and the nodes cannot have a visited flag. Ie, you can only use left + right members. My approach was to de...

median of BST in o(logn) time complexity

I came across solution given at http://discuss.joelonsoftware.com/default.asp?interview.11.780597.8 using Morris InOrder traversal using which we can find the median in O(n) time. But is it possible to acheive the same using O(logn) time? The same has been asked here - http://www.careercup.com/question?id=192816 ...

Real world pre/post-order tree traversal examples

I understand pre-order, in-order, and post-order tree traversal algorithms just fine. (Reference). I understand a few uses: in-order for traversing binary search trees in order, pre-order for cloning a tree. But I can't for the life of me come up with a real world task that I'd need post-order traversal to accomplish. Can you give me ...

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

Based on this question: http://stackoverflow.com/questions/1310649/getting-a-modified-preorder-tree-traversal-model-nested-set-into-a-ul The logic bellow is used to build an ordened 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...

Constructively manipulating any value/object within a JSON tree of unknown depth

Hi, I have a JSON tree that contains nodes and children - the format is: jsonObject = { id:nodeid_1, children: [ { id:nodeid_2, children:[] }, { id:nodeid_3, children:[ { id:nodeid_4, children:[] }, { id:nodeid_5, children:[] } } } I don't know the depth of this tre...

Tree traversal with page property sortIndex

I'm building a tree structure based on a list retrieved from a db.Model called Pages. Each Page entry has a parentKey property that's a db.SelfReferenceProperty() and a db.IntegerProperty() called sortIndex. I fetch the list and call a method to travers over the list and make a nestled dict as my tree. The reason I fetch the entire li...

Finding border of a binary tree

We are given a binary search tree; we need to find out its border. So, if the binary tree is 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / / \ 15 35 120 155 250 It should print out 50 25 15 35 120 155 250 20 150 10. If the binary tree is ...

Post order traversal of a formula

In data structures, I get converting in order and pre-order formula conversions into trees. However, I'm not so good with post-order. For the given formula x y z + a b - c * / - I came up with - / \ * / (divide) / \ / \ x + - c ...

is there anyway to travel all nodes of tree in shortest distance

not necessarly u need to travel once ...

Binary Search Tree- breadthFirst function Call

I have the algorithm for void leveltraversal(ostream& out); but i am not sure how to call it in main () . In my Assignment we are not allowed to change the header file. Is there a way to call it without overloading it? Update: void BST::levelTraversal(ostream& out){ queue<BST::BinNode*> q; BinNode* cur = myRoot; BinNode* top = NUL...