binary-trees

Having issues with Binary Search Trees in C#

I'm having a problem with the following code snippet: using System; using System.Collections.Generic; using System.Text; namespace trees_by_firas { class Program { static void Main(string[] args) { BinarySearchTree t = new BinarySearchTree(); t.insert(ref t.root, 10); t.inser...

Logical error BST ... wrong results

HI i did this code with help of Marc Gravell in http://stackoverflow.com/questions/406791/ & http://stackoverflow.com/questions/406402/bst-c-code-with-errors , its Binary search tree , but now I'm having logical error the results coming are wrong the output of my code is the following: 2 3 5 6 10 17 ----------------------------------...

In-order tree traversal

I have the following text from an academic course I took a while ago about in-order traversal (they also call it pancaking) of a binary tree (not BST): In-order 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. ...

What is validating a binary search tree?

I read on here of an exercise in interviews known as validating a binary search tree. How exactly does this work? What would one be looking for in validating a binary search tree? I have written a basic search tree, but never heard of this concept. Thanks ...

Finding last element of a binary heap

Hello everybody, quoting Wikipedia: It is perfectly acceptable to use a traditional binary tree data structure to implement a binary heap. There is an issue with finding the adjacent element on the last level on the binary heap when adding an element which can be resolved algorithmically... Any ideas on how such an ...

Search times for binary search tree

Does anyone know how to figure out search time for a binary search tree(i.e. worst-case, best-case, and average-case)? ...

The best way to calculate the height in a binary search tree? (balancing an AVL-tree)

I'm looking for the best way to calculate a nodes balance in an AVL-tree. I thought I had it working, but after some heavy inserting/updating I can see that it's not working correct (at all). This is kind of a two-part question, the first part would be how to calculate the height of a sub-tree, I know the definition "The height of a no...

Real world examples of tree structures

I'm looking for some examples of tree structures that are used in commercial/free software projects, modern or old. I can see examples on wikipedia, but I am looking for more concrete examples and how they're used. For example primary keys in databases are (from what I've read) stored in BST structure or a variation of the BST (feel free...

Binary Trees in C

I'm trying to insert nodes into a tree in order. My function works fine... when there's only three nodes. I have this code: typedef struct _Tnode Tnode; struct _Tnode { char* data; Tnode* left; Tnode* right; }; Along with this: Tnode* add_tnode(Tnode* current_node, char* value) { Tnode* ret_value; if(current_node == N...

Detect cycles in a geneology graph during a Depth-first search

I'm loading horse genealogical data recursively. For some wrong sets of data my recursion never stops... and that is because in the data there are cycles. How can I detect those cycles to stop recursing? I thougth of while recursing mantain a hashTable with all "visited" horses. But that will find some false positives, because a horse ...

Tree Abstact Data Type

I'm doing a unit called Data Structures and Algorithms. We've just started and my professor has just taught us the basics of what Algebraic Semantics is and what Axioms are etc. Till now, I've just used Trees in the form of arrays. Not using the signature for pre-ordered tree as tree(value, tree, tree) where value is the value in the nod...

B-tree faster than AVL or RedBlack-Tree?

I know that performance never is black and white, often one implementation is faster in case X and slower in case Y, etc. but in general - are B-trees faster then AVL or RedBlack-Trees? They are considerably more complex to implement then AVL trees (and maybe even RedBlack-trees?), but are they faster (does their complexity pay off) ? E...

Permutations of a binary tree.

Consider a binary tree: n is a node, if n is an integer (+ a b) is a node, if a and b are nodes. We have the following three operations: (+ a b) -> (+ b a) (+ (+ a b) c) -> (+ a (+ b c)) (+ a (+ b c)) -> (+ (+ a b) c) -- (2. in reverse) I need an algorithm for giving all the possible permutations of a given tree using these opera...

Finding Overlap among multiple intervals

Let's say I have a list of intervals (or ranges) (Eg. 10-15, 5-7, 9-12..). The problem is to find the subset of ranges that overlaps. Of course I can use Interval tree for this. The actual problem that I have is there are multiple ranges. Best explained by an example: 10-15, 5-7, 9-12 1-2, 3-6, 14-15 3-5, 9-15, 10-15 In the abov...

How to properly insert/delete in a binary search tree in C?

I kinda have to put my previous C questions on hold cause this one is more important now... I have already coded the insert and delete functions on my binary search tree but the delete function is incomplete. There's a couple of things I need help in... 1) Is my insert function good or can it be improved somehow? 2) My delete function...

What is the degree of a tree? (As in, a tree ADT)

I understand that the degree of a node is the number of children it has. However, how do we define the degree of a tree? ...

Tree matching using serialization of tree and unique id generation for each subtree.

What would be the best way to serialize a given binary tree and inturn evaluate a unique id for each serialized binary tree? For example, I need to serialize the sub-tree (2,7,(5,6,11)) and generate a unique id 'x' representing that sub-tree so that whenever I come across a similar sub-tree (2,7,(5,6,11)) it would serialize to the sam...

How to traverse a binary tree in O(n) time without extra memory

Given a binary tree with an integer, Left & Right pointers, how can one traverse the tree in O(n) time and O(1) extra memory (no stack/queue/recursion)? This guy gave a solution which is not O(n) total time that encoded the current path as an integer (and thus works on for trees of limited depth). I am looking for the classical solutio...

C++ Implementation of a Binary Heap

I need a min-heap implemented as a binary tree. Really fast access to the minimum node and insertion sort. Is there a good implementation in stl or boost that anyone can point me too? ...

When inserting to a BST, is the first item inserted always the root of the tree?

Looking at the implementation on Wikipedia, it would seem that a standard BST (non self balancing) never re-arranges itself during inserts, and so the very first item added will always be the root. Is this correct? If so, doesn't that mean that there is the potential for a BST to often have much worse than O(logN)? Using this as a refer...