binary-trees

Right Threading a Binary Tree

I'm having a hell of a time trying to figure this one out. Everywhere I look, I seem to be only running into explanations on how to actually traverse through the list non-recursively (the part I actually understand). Can anyone out there hammer in how exactly I can go through the list initially and find the actual predecessor/successor n...

Does it make sense to implement iterators for containers which has no obvious end - e.g. trees?

I`m writing binary search tree template for two reasons - learning C++ and learning most common algorithms and data structures. So, here is the question - as long as I want to implement iterators, it seems to me that there is no strict definition for where tree ends. What are your suggestions? How do I do this? ...

Convert Preorder listing of a binary tree to postorder and vice versa

How can I find the preorder listing of a tree if only the postorder listing is given and vice versa. Also, in the tree, every non-leaf node has two children (i.e. Each node has either two or zero children.) EDIT: Another given assumption is that the label of each node is unique and has a field that will identify it as an internal node o...

Algorithm to return length of shortest branch in a binary tree

A binary tree can be encoded using two functions l and r such that for a node n, l(n) give the left child of n , r(n) give the right child of n. A branch of a tree is a path from the root to a leaf, the length of a branch to a particular leaf is the number of arcs on the path from the root to that leaf. Let MinBranch(l,r,x) be a simple...

Sorting a binary search tree on different key value

Say I have a binary tree with the following definition for a node. struct node { int key1 ; int key2 ; } The binary search tree is created on the basis of key1. Now is it possible to rearrange the binary search tree on basis of key2 in O(1) space. Although I can do this in variable space using an array of pointers to nodes. The act...

Post order traversal of binary tree without recursion

Hello fellow coders, Does anyone know the algorithm for doing a post order traversal of a binary tree WITHOUT using recursion. Any information would be greatly appreciated. ...

Tree iterator, can you optimize this any further?

As a follow up to my original question about a small piece of this code I decided to ask a follow up to see if you can do better then what we came up with so far. The code below iterates over a binary tree (left/right = child/next ). I do believe there is room for one less conditional in here (the down boolean). The fastest answer wins!...

Binary Tree Node Fault

Here's the node definition: struct node{ int data; stuct node * left; struct node * right; }; What I am trying to do is list all the nodes that point to an ancestor node. After posting the wrong solution and taking advice from the answers, my new solution is: Recursively go through the binary tree. Add the current node to...

Shortest branch in a binary tree?

A binary tree can be encoded using two functions l and r such that for a node n, l(n) give the left child of n, r(n) give the right child of n. A branch of a tree is a path from the root to a leaf, the length of a branch to a particular leaf is the number of arcs on the path from the root to that leaf. Let MinBranch(l,r,x) be a simple ...

The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

This has been bothering me for a while. I know that given N keys to arrange in the form of a binary search tree, the possible number of trees that can be created correspond to the Nth number from the Catalan sequence. I have been trying to determine why this is; unable to find anything that might even attempt to explain it intuitively ...

A little problem with BST implementation

I`m writing STL-like container for binary-search tree. I have template class for Tree itself and nested class TreeNode. My question is where should I put the binary-predicate function which compairing keys - Into the tree class or into the Node class? If I decide to put it in a Tree class, all of my nodes would not know how to compare t...

How can I find a Binary tree from a given Traversal Method?

How can I find a binary tree from a given traversal method (inorder,post-order,pre-order)? ...

Are AVL Trees Evil?

I was reading the article from Steve Yegge about singletons. In it he mentions his teacher told him AVL Trees were evil. Is it just that red and black trees are a better solution? ...

IntervalTree Java Implementation

I need an IntervalTree or RangeTree implementation in Java, and am having trouble finding one with working deletion support. There's a built-in one at sun.jvm.hotspot.utilities.IntervalTree, but the deleteNode method in the RBTree superclass states: /** * FIXME: this does not work properly yet for augmented red-black * trees since it...

How to determine whether a binary tree is complete?

A complete binary tree is defined as a binary tree in which every level, except possibly the deepest, is completely filled. At deepest level, all nodes must be as far left as possible. I'd think a simple recursive algorithm will be able to tell whether a given binary tree is complete, but I can't seem to figure it out. ...

how can delete a binary tree node with all possibility?

i created a binary tree with some integer values ...i can search the tree by my code ,but i dont know how to proceed delete node operation ..help me with simple program using c..thanks in advance....... ...

tree iterations c++

Looking for examples on simple tree iterations in C++, both recursive and iterative. (post, pre and in-order) ...

Java Binary Tree, how to implement Node?

In the tree class I'm suppose to compare two node, for you know searching and adding items. I have some issues with how to make it comparable. When one adds data(generic, anything) to the tree one calls the Tree class which then makes a new Node object. How can I declare the variable data/element in the Node class so that it is of type E...

How can we find given two binary trees that they are equal ?

Hi, What would be the efficient algorithm to find if two given binary trees are equal - in structure and content? Thanks. ...

Enumerate search trees

According to this question the number of different search trees of a certain size is equal to a catalan number. Is it possible to enumerate those trees? That is, can someone implement the following two functions: Node* id2tree(int id); // return root of tree int tree2id(Node* root); // return id of tree (I ask because the binary co...