binary-trees

How can I find the common ancestor of two nodes in a binary tree?

The Binary Tree here is not a Binary Search Tree. Its just a Binary Tree. The structure could be taken as - struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort - Consider this binary tree (from http://lcm.csa.iisc.ernet.in/dsa/no...

How can I create binary trees in Perl?

How do can I create binary trees in Perl? ...

Iterative version of Binary Tree Search

Basic Tree-search algorithm for searching a node (with value k) in a binary search tree. 'x' denotes the node of the binary search tree. TREE-SEARCH (x, k) if x= NIL or k = key[x] then return x if k < key[x] then return TREE-SEARCH(left[x], k) else return TREE-SEARCH(right[x], k) Iterative Version: ITERATIVE-TREE-SEARCH...

How to implement tail recursion for tree algorithms in prolog.

How could I convert the following into a tail recursive version. sum(void,0). sum(t(V,L,R),S) :- sum(L,S1), sum(R,S2), S is V + S1 + S2. It seems impossible to maintain a single accumulator as the branching is in 2^n magnitude. A possible solution would be to have the accumulator add a new accumulator to a list on each iterati...

Prove that binary trees with the same inorder and preorder traversals are identical?

Does anybody know how to prove that if two binary trees have the same inorder and preorder traversals, then they are identical? (perhaps by showing that you can't have two different binary trees with identical inorder and preorder traversals) Alternatively, show a case that would disprove this, or show why can't it be done? (I'll admit...

compare Hash with Binary search tree

Hi, We all know that a hash table has O(1) time for both inserts and look-ups if the hash function was well chosen. So, what are the reason we want to use Binary Search Tree? Just because a perfect hash function was hard to design? Here how I come up with this question? I notice that Standard C++ STL has set and map which are impleme...

pointer problem in implementing Tree in C

I am implementing an avl tree for my assignment. #include <string.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> struct TreeNode { char *item; struct TreeNode *left; struct TreeNode *right; signed char balance; }; typedef struct TreeNode Node; void _print_avl (Node *, int , const char *); Node * get_new_node (...

Tool for displaying binary decision tree

The system I'm currently working on involves the creation of binary decision trees. Lots of them. Some of them are stored in XML format so they can be analyzed manually if needed. The tree structure is basically nested <NODE> tags. Each node may also have a few child tags defining the properties of the node. What I would like to do is ...

Java Algorithm for finding the largest set of independent nodes in a binary tree

By independent nodes, I mean that the returned set can not contain nodes that are in immediate relations, parent and child cannot both be included. I tried to use Google, with no success. I don't think I have the right search words. A link, any help would be very much appreciated. Just started on this now. I need to return the actual...

How to fill a binary tree from a string, java.

Hi, I'm suppose to fill a binary tree with integers based upon a string like this. [int](LT)(RT) LT is an expression of the same form for the left part of the tree. Same with RT. A valid string would be something like this: 4(2(1)(3))(6(5)(7). How can I fill this tree? This is not a sorted tree of any kind. So it can just fill every...

Java recursive binary tree

Welcome! I have a recursive public static method named less that takes a tree node (an original binary tree, not really a search tree) and a int parameter that returns if all the values in the tree are less than the integer. So, I would use a public class TN { public int value; public TN left, right; public TN(int v, TN l, TN r) {v...

Binary tree with different node types

I'm working on a somewhat complex mathematical code, written in C++. I'm using (templated) tree structures for adaptive function representation. Due to some mathematical properties I end up in a situation where I need to change from one type of node to another. This needs to happen transparently and with minimal overhead, both in terms o...

List implemented using an inorder binary tree

For the new computer science assignment we are to implement a list/array using an inorder binary tree. I would just like a suggestion rather than a solution. The idea is having a binary tree that has its nodes accessible via indexes, e.g. t = ListTree() t.insert(2,0) # 1st argument is the value, 2nd the index to insert at t.get(0) # ...

How to create a linked list of nodes that are contained in the max-Depth of a Binary Tree using Java

I've already created the Binary Tree and Linked list classes, I'm just in need of an algorithm that prints ONLY the nodes of the largest path. The height and size of the Binary Tree are already stored in the root node, but my problem is traversing only the largest path while adding each node to my linked list. ...

Proof for depth of balanced search tree

If T is a balanced BST with n elements, L its left subtree and R its right one, how can I prove that its depth is less than or equal to 2log(n) + 1? There is a proof by induction which I have but I don't get it. (I understand that stackoverflow is mainly programming oriented but I found some questions about binary search trees and dec...

Binary Tree's usage

Dear All, Can someone give me a real life example ( in programming, C#) of needing to use a Binary Tree or even just an ordinary tree? I understand the principle of a Binary Tree and how they work, but I'm trying to find some real life example's of their usage? Tony ...

Binary Search Tree Array Imp. C++

Hello, I'm just having trouble with Inserting into the array...And having the children branch off from the root, or "parent".. I've been trying to insert data into an array based implementation of a BST: BST::BST(int capacity) : items(new item[capacity]), size(0) { // define the constructor to the BST Class. } void BST::insert (...

Calculating the depth of a binary tree in LISP recursively

I have the following binary tree A / \ B C / \ D E represented as a list in Lisp (A 2 B 0 C 2 D 0 E 0) where the letters are node names and the numbers are the number of child nodes (0 for none, 1 one node, 2 two nodes). I need to find highest from root node to leaf depth of the tree (the depth of the binary tree that is)...

How do I find the nth ancestor of a node in a binary search tree?

I got asked this at a job interview. Here's my O(log n) solution. Find the depth of the node. Repeat the search, but stop at depth - n. Is there a way to do this without the second pass? ...

Starting with an empty tree, what is the complexity of inserting into Red Black Tree in big-O notation?

If I have 10 elements and starting with an empty tree, What is the complexity of inserting 10 elements into Red Black in big-O notation? Is it going to be more than O(log 10) because each time it inserts an element, it has to search for appropriate location for the element and performs a series of rotations between ancestor nodes and c...