binary-trees

Can I make a binary search tree with this?

I've made BSTs before. Can I use this to make a BST without modifications? template <class Item> class binary_tree_node { public: private: Item data_field; binary_tree_node *left_ptr; binary_tree_node *right_ptr; }; I tried making a BST with this but ran into some problems. For one thing, when I create the root node,...

Are structs necessary in binary search trees

I've looked at some code for BSTs and I can see that each node is a struct. Is this necessary? ...

Binary Trees in Scheme

Consider the following BNF defining trees of numbers. Notice that a tree can either be a leaf, a node-1 with one subtrees, or a node-2 with two subtrees. tree ::= (’leaf number) | (’node-1 tree) | (’node-2 tree tree) a. Write a template for recursive procedures on these trees. b. Define the procedure (leaf-count t) that returns the ...

Find an algorithm in RBTREE in O(logn)

I need to find a data structure which I can do with the following actions: Build(S,k) - O(nlogn) Search(S,k) - O(logn) Insert(S,k) - O(logn) Delete(S,k) - O(logn) Decrease-Upto(s,k,d) - O(logn) - this method should subtract d(d>0) every node which is <=k The obvious first choise was RedBlackTree. However, I can't come to a solution ...

How to delete a binary search tree from memory?

I have a BST which is a linked list in C++. How would I delete the whole thing from memory? Would it be done from a class function? ...

Why does my C++ code fail to delete all nodes in my BST?

This is supposed to traverse a BST and delete every node, including the root node. However, at the end, I get the message "root still has a left node." Why aren't all nodes deleted? void deleteTree() { deleteNode(root); if(root->right) cout << "root still has a right node" << endl; if(root->left) cout <<...

How to print out a BST in C++

My C++ program creates a binary search tree. I know how to print out the values in pre-order, post-order, and in-order. However, I want to do something a little more difficult. I want to print out the values the way they would look if someone drew the tree on paper. It would have the root at the center at the top, it's left child rig...

How to rebuild a BST from a file

My C++ program creates an unbalanced BST from user input, and saves it do disk. It does this by first indexing each node by doing a pre-order traversal and assigning each node a unique number. Next, it outputs the BST to a file. It does this by doing a pre-order traversal and then for each node printing to file the data value, the ind...

Need help in returning from a recursive method

Hi, I am trying to trace the path of a node in a binary tree (not a binary search tree). Given a node, I am trying to print the values of the path from the root. I have written the following program. package dsa.tree; import java.util.Stack; public class TracePath { private Node n1; public static void main(String args[])...

Java equivalent of C++ std::map?

I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/search Each element is composed of a unique key and a mapped value Keys follow a strict weak ordering I'm looking for implementations with o...

Traversal of a tree to find a node

Hi again.... I'm either really stupid or my program hates me. Probably both. Problem: I am searching through a tree to find a value that is passed. Unfortunately, it does not work. I started debugging it with prints and stuff, and what is weird is it actually finds the value... But skips the return statement!!! /** * Returns...

Binary tree to get minimum element in O(1)

I'm accessing the minimum element of a binary tree lots of times. What implementations allow me to access the minimum element in constant time, rather than O(log n)? ...

I can't seem to delete the simplest case on a Binary Search Tree in C

Hi, I've posted about this last year because some university project and now I have to do it again (I never finished what I had to do last year). I've already looked at me previous code, all you guys answers on those questions, but still, I can't seem to understand this. I'm not going to put all my questions in one long post, it just m...

Fixing pointers in binary tree delete function (node with 2 children)

Hi, I'm a having a little trouble thinking how the hell do I fix the appropriate pointers when trying to delete a node from a binary tree where that node has 2 children. I understand the basic concept and I'm basically just having trouble fixing the pointers... So, basically, my delete function is mostly done and every case is already...

Creating a Binary Tree for a Knockout Tournament

I am trying to create a binary tree for use in a knockout tournament. The tree consists of TNodes with Left and Right pointers. This is the code that I have come up with (below); however, it runs into difficulties with the pointers in the CreateTree section. Once this creates an empty tree of large enough size, I need to add the names ...

Red-Black Tree Deleting Problem C#

Hey, I am trying to implement a red-black tree in C#. I already created an object called sRbTreeNode which has a String key, Color, Left, Right and Parent properties. I successfully managed implementing the methods Insert, InsertFixUp, LeftRotate, RightRotate, Delete, and now im having trouble with the method DeleteFixUp. DeleteFixUp i...

What data structure using O(n) storage with O(log n) query time should I use for Range Minimum Queries?

I'm stumped by the following homework question for an algorithms class: Suppose that we are given a sequence of n values x1, x2 ... xn, and seek to quickly answer repeated queries of the form: given i and j, find the smallest value in xi... xj Design a data structure that uses O(n) space and answers queries in O(log n)...

draw binary tree with php

Hi, I'm looking for a good library / API to draw a binary tree using PHP. I've tried using Image_GraphViz, but it doesn't seem to work. I've also looked at phpsyntaxtree, but its not documented. Alternatively, I'm also looking for a jQuery plugin that does this. (just not this one because it has no documentation). Thanks ...

Find kth smallest element in a binary search tree in Optimum way

Hi, I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently? The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an inorder traversal of the entire tree. But deep down I feel that I am not using the ...

What is the average asymptotic depth of a simple unbalanced search tree?

For a balanced search tree, it is O(log(N)) for all cases. For unbalanced search trees, the worst case is O(N), e.g., insert 1, 2, 3, 4,.. and best case complexity is when it is balanced, e.g., insert 6, 4, 8, 3, 5 7. How do we define the average case complexity for unbalanced search tree? ...