Binary tree depth - c++

Trying to find depth of binary search tree. Have done something with a bit of google search but my code is crashing:
int treeDepth(int depth) const
{
int l = this->left->treeDepth(depth);
int d = this->right->treeDepth(depth);
return depth + std::max(l, d);
}
calling this function with: root->treeDepth(1);

First of all, I think that you may be confusing the depth of a tree with the height of a tree. Refer to What is the difference between tree depth and height? for an explanation.
For example, following are the depth and height of the nodes in this (binary) tree ('0' being the root):
0 -- depth 0, height 2
/ \
1 2 -- depth 1, height 1 and 2, respectively, for nodes 1 and 2
/ \
3 4 -- depth 2, height 0
As you can see, that the depth of the tree root is '0', which is O(1) computation. The height of the tree, however, is more interesting with regards to recursion and can be computed using the following:
struct TreeNode {
T _value;
typedef TreeNode* node_ptr;
node_ptr _left, _right;
};
...
int treeHeight(Node* node)
{
return std::max(node->_left? 1 + height(node->_left) : 0,
node->_right? 1 + height(node->_right) : 0);
}
...
std::cout << treeHeight(root);
...
The fundamental idea is this:
During the recursive (depth-first) traversal, if a leaf node is reached, return a value of '0' (which is the height of every leaf node). Otherwise, compute the height of the tree rooted at the non-leaf node (which is the max of the heights of the left subtree and the right subtree of that node + 1 for the node itself). Start from the root of the tree.
The second aspect that I want to address in your question is regarding what I gather from your function signature:
int treeDepth(int depth) const
and the way you are calling it:
root->treeDepth(1);
The depth of a tree is not conducive to being a property of the root. Instead, it is a property of the tree, which is composed of tree nodes, one of which is the root node. So, I would define the class (C++ struct shown here) as follows:
template<class T>
struct BinaryTree {
struct TreeNode {
T _value;
typedef TreeNode* node_ptr;
node_ptr _left, _right;
};
TreeNode _root_node;
typedef typename TreeNode::node_ptr node_ptr;
node_ptr _root;
...
int height(node_ptr node) const;
...
};
Finally, finding the depth of a given node in a tree:
int depth(node_ptr node) const;
// returns depth of node if it is in the tree, else -1
is a problem where you may apply recursion. However, a recursive approach is not natural and a breadth-first (or level-order) traversal will be more suited for this.

there are actually many problems in your code.
Fist of all you need to implement a recursion termination, that is a condition needed to stop your function from calling its self forever
in your case you need to write something like
if(left==nullptr && rigth==nullptr)
return depth;
The recursion termination is VERY IMPORTANT! you always need it when you're writing a recursive function. Without you're 100% going into a never ending loop (in the best case).
then, when you re-call your function you need to change the depth value
i mean, if you're going down on another node of the tree it means that the tree is tall at least "depth+1" so you need to pass depth + 1 not just depth,
and at the and of the function just write
return std::max(l,d);
also,you're using pointers, always write a condition to check if you're really trying to access a well defined address, that is, before even trying to access an address (ex. this->left) you need to write a condition like if( this->left!=nullptr) (nullptr from c++ 11 otherwise NULL will as well get the work done)

You need to read up a little bit about recursion.
One of the fundamental tenets of recursion is that there must be a "stop condition" which will at some point terminate the recursion.
Otherwise, you have what is known as "runaway recursion", your stack fills up, and your program crashes and burns.
In your case, a "stop condition" would be reaching a this->left or this->right which happens to be NULL.
So, for the future readers (as suggested by Barmar in a comment)
int l = left == NULL? 0 : left->treeDepth(depth);
int d = right == NULL? 0 : right->treeDepth(depth);

try something like this:
int treeDepth() const
{
int l = left == NULL? 0 : left->treeDepth();
int d = right== NULL? 0 : right->treeDepth();
return 1 + std::max(l, d);
}
In this case you don't need additional paramether "depth"

Related

Node search in Binary Tree overflows stack

I use the following method to traverse* a binary tree of 300 000 levels:
Node* find(int v){
if(value==v)
return this;
else if(right && value<v)
return right->find(v);
else if(left && value>v)
return left->find(v);
}
However I get a segmentation fault due to stack overflow.
Any ideas on how to traverse the deep tree without the overhead of recursive function calls?
*
By "traverse" I mean "search for a node with given value", not full tree traversal.
Yes! For a 300 000 level tree avoid recursion. Traverse your tree and find the value iteratively using a loop.
Binary Search Tree representation
25 // Level 1
20 36 // Level 2
10 22 30 40 // Level 3
.. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. // Level n
Just to clarify the problem further. Your tree has a depth of n = 300.000 levels. Thus, in the worst case scenario a Binary Search Tree (BST) will have to visit ALL of the tree's nodes. This is bad news because that worst case has an algorithmic O(n) time complexity. Such a tree can have:
2ˆ300.000 nodes = 9.9701e+90308 nodes (approximately).
9.9701e+90308 nodes is an exponentially massive number of nodes to visit. With these numbers it becomes so clear why the call stack overflows.
Solution (iterative way):
I'm assuming your Node class/struct declaration is a classic standard integer BST one. Then you could adapt it and it will work:
struct Node {
int data;
Node* right;
Node* left;
};
Node* find(int v) {
Node* temp = root; // temp Node* value copy to not mess up tree structure by changing the root
while (temp != nullptr) {
if (temp->data == v) {
return temp;
}
if (v > temp->data) {
temp = temp->right;
}
else {
temp = temp->left;
}
}
return nullptr;
}
Taking this iterative approach avoids recursion, hence saving you the hassle of having to recursively find the value in a tree so large with your program call stack.
A simple loop where you have a variable of type Node* which you set to the next node, then loop again ...
Don't forget the case that the value you are searching for does not exist!
You could implement the recursion by not using the call stack but a user-defined stack or something similar; this could be done via the existing stack template. The approach would be to have a while loop which iterates until the stack is empty; as the existing implementaion uses depth-first search, elimination of the recursive calls can be found here.
When the tree that you have is a Binary Search Tree, and all you want to do is search for a node in it that has a specific value, then things are simple: no recursion is necessary, you can do it using a simple loop as others have pointed out.
In the more general case of having a tree which is not necessarily a Binary Search Tree, and wanting to perform a full traversal of it, the simplest way is using recursion, but as you already understand, if the tree is very deep, then recursion will not work.
So, in order to avoid recursion, you have to implement a stack on the C++ heap. You need to declare a new StackElement class that will contain one member for each local variable that your original recursive function had, and one member for each parameter that your original recursive function accepted. (You might be able to get away with fewer member variables, you can worry about that after you have gotten your code to work.)
You can store instances of StackElement in a stack collection, or you can simply have each one of them contain a pointer to its parent, thus fully implementing the stack by yourself.
So, instead of your function recursively calling itself, it will simply consist of a loop. Your function enters the loop with the current StackElement being initialized with information about the root node of your tree. Its parent pointer will be null, which is another way of saying that the stack will be empty.
In every place where the recursive version of your function was calling itself, your new function will be allocating a new instance of StackElement, initializing it, and repeating the loop using this new instance as the current element.
In every place where the recursive version of your function was returning, your new function will be releasing the current StackElement, popping the one that was sitting on the top of the stack, making it the new current element, and repeating the loop.
When you find the node you were looking for, you simply break from the loop.
Alternatively, if the node of your existing tree supports a) a link to its "parent" node and b) user data (where you can store a "visited" flag) then you don't need to implement your own stack, you can just traverse the tree in-place: in each iteration of your loop you first check if the current node is the node you were looking for; if not, then you enumerate through children until you find one which has not been visited yet, and then you visit it; when you reach a leaf, or a node whose children have all been visited, then you back-track by following the link to the parent. Also, if you have the freedom to destroy the tree as you are traversing it, then you do not even need the concept of "user data": once you are done with a child node, you free it and make it null.
Well, it can be made tail recursive at the cost of a single additional local variable and a few comparisons:
Node* find(int v){
if(value==v)
return this;
else if(!right && value<v)
return NULL;
else if(!left && value>v)
return NULL;
else {
Node *tmp = NULL;
if(value<v)
tmp = right;
else if(value>v)
tmp = left;
return tmp->find(v);
}
}
Walking through a binary tree is a recursive process, where you'll keep walking until you find that the node you're at currently points nowhere.
It is that you need an appropriate base condition. Something which looks like:
if (treeNode == NULL)
return NULL;
In general, traversing a tree is accomplished this way (in C):
void traverse(treeNode *pTree){
if (pTree==0)
return;
printf("%d\n",pTree->nodeData);
traverse(pTree->leftChild);
traverse(pTree->rightChild);
}

Function that returns the rightmost node in the bottom level of a complete Binary Search Tree

I just need help writing a recursive function that returns the rightmost node at the deepest level of a binary search tree (it's actually a node based heap). It is important to remember that the tree will always be complete. I have tried a few things, but nothing worked well enough to post here as a start.
I have found similar questions but those all relate to the leftmost node which I can do because it is always in the same place, but the rightmost node varies depending on how filled the tree is.
I have functions getLeftHeight and getRightHeight
void HeapClass::findDeleteNode( HeapNode *&workingPtr ){
}
I have the prototype like this, where I send in a pointer that I later take values out of and delete in a different function. I'm new to this website so sorry if this question is lacking information or posted incorrectly. Any help would be appreciated, thank you.
You'll need to compare the height of the left subtree and the right. If the height of the right one is greater or equal to the left, you recurse on that node, otherwise on the left
void HeapClass::findDeleteNode( HeapNode *&workingPtr )
{
bool isLeaf = getLeftHeight(workingPtr)) == 0 && getRightHeight(workingPtr) == 0;
if(isLeaf) {
// found it
return;
}
findDeleteNode(getLeftHeight(workingPtr) > getRightHeight(workingPtr) ? workingPtr->left : workingPtr->right);
}

print BST min-max

I have a binary search tree (BST). I need to print it's values in such case: min, max, second min, second max...
For example: if the tree contains values 1,2,3,4,5,6,7, it should print: 1,7,2,6,3,5,4. I can't change the tree, or use another data structure like set or list. Also i need to do it in O(nlogn).
The nodes of BST are represented with struct:
struct nodeT{
int value;
nodeT * left;
nodeT * right;
};
void PrintTree(nodeT * head){
}
For the minimum you could have a function which recursively finds a value equal or larger than another value. You call this first with e.g. INT_MIN which means it will find the lowest value in the tree. Then you use the found value, add one, and call the same function again, and that will find the second smallest value, etc.
For the largest value, use the same basic algorithm, but check for equal or smaller than, and start with INT_MAX.
When the current "smallest" value found is larger than the current "largest" value, then you passed each other and end the searching.
Note 1: This works well with integer values, maybe not so well with floating point values.
Note 2: I have not put any regard to big-O, it's just something I thought of at the top of my head.
In pseudo code:
print_smallest_and_largest()
{
int current_smallest = INT_MIN;
int current_largest = INT_MAX;
// Infinite loop
for (;;)
{
current_smallest = find_equal_or_bigger_than(current_smallest);
current_largest = find_equal_or_smaller_than(current_largest);
if (current_smallest > current_largest)
break; // Got all values
printf("Smallest: %d\n", current_smallest);
printf("Largest: %d\n", current_largest);
++current_smallest;
--current_largest;
}
}
In BST, The left subtree of a node contains only nodes with keys less than the node's key. And the right subtree of a node contains only nodes with keys greater than the node's key. So after a BST has been constructed, you should notice that - the minimum element should be in leftmost node and the maximum element should be in the rightmost node.
So You can do these by traversing the tree in the leftmost or rightmost position to retrieve the minimum/maximum element in O(log n) time:
struct treeNode {
treeNode* left;
treeNode* right;
int key;
}*root;
template <class Object>
treeNode* BinarySearchTree <Object> :: minHelper(treeNode* &node) {
if(node -> left == NULL) return node;
else minHelper(node -> left);
}
template <class Object>
treeNode* BinarySearchTree <Object> :: min() {
return minHelper(root);
}
template <class Object>
treeNode* BinarySearchTree <Object> :: maxHelper(treeNode* &node) {
if(node -> right == NULL) return node;
else maxHelper(node -> right);
}
template <class Object>
treeNode* BinarySearchTree <Object> :: max() {
return maxHelper(root);
}
Here I wrote a complete program with all possible operations with BST
The minimum value is:
if the root has no left child then the root. otherwise the leftmost node, such that the node that has no right branch from root to itself.
The maximum value is: if the root has no right child then the root. otherwise the rightmost node that has no left branch from root to itself.
You can find these two using lg(N) operations in average case
So the complete algorithm becomes:
Find the minimum value. Print it and delete the node. Note that printing and deleting takes lg(N) time
If the tree is not empty, find the maximum value, print it and delete the node. Otherwise go to step 3.
If the tree is not empty, return to step 1. otherwise terminate.
Runtime analysis : By definition, deleting a node in BST takes lg(N) time in average case and we've shown earlier that finding the max or min value takes lg(N) time too(in average case). So the runtime is O(NlgN) in average case.
Is the restriction on no other data structures purely pedagogical (fine if it is)? It would make things simpler if you could use another data structure. In case it isn't obvious, here is a way to do it in O(N) time (but O(N) space)
Traverse the N-element BST and get back the sorted array S in O(N) time, O(N) space.
print out S[0], S[N-1], S[1], S[N-2], S[2], ...
The other, recursive, way is outlined by the other answers.

Generalized (non-slicing) pointer to templated tree nodes? (C++)

I'm working on an octree implementation where the tree nodes are templated with their dimensional length (as a power of 2):
template<long N>
struct node_t {
enum { DIM = 1 << N };
node_t<N+1> * parent;
node_t<N-1> * children[8];
long count;
}
And specialized for N = 0 (leaves) to point to data.
struct node_t<0> {
enum { DIM = 1 };
node_t<1> * parent;
data_t data;
long count;
}
(Aside: I suppose I probably also need a specialization for N_MAX that excludes a parent pointer, or else C++ will generate types of increasing N ad finitum? But that's not really relevant to my question.)
I'd like to create a function that steps along a ray in the 3D space that my octree occupies, so ostensibly I could just keep a pointer to the root node (which has a known type) and traverse the octree from the root at every step. However, I would prefer a more 'local' option, in which I can keep track of the current node so that I can start lower in the tree when possible and thus avoid unnecessarily traversing the upper nodes of the octree.
But I don't know what that type pointer could be (or any other way of implementing this) so that I don't experience slicing.
I'm not tied down to templates, as the dimension can simply be implemented as a long const. But then I don't know how to make it so that the leaves have a different child type than inodes.
Thanks for your help!
Update
The reason I'd like to do it this way rather than something similar to this is because of the count variable in each node: if the count is 0, I'd like to jump through the whole cube, rather wasting time going through leaves that I know to be empty. (This is for a raytracing voxel engine.)
As much as I love templates, your code might actually be simpler with:
class node {
node* parent; // NULL for root node
long dim;
long count;
virtual rayTrace(Ray) = 0;
};
class leafNode : node {
data_t data;
virtual rayTrace(Ray);
};
class nonLeafNode : node {
vector<node*> children;
virtual rayTrace(Ray);
};
This has the advantage that the tree can be whatever depth you want, including some subtrees can be deeper than others. It has the downside that dim must be computed at runtime, but even that has the silver lining that you can make it a double if your tree gets really big.

Stackoverflow exception when traversing BST

I have implement a link-based BST (binary search tree) in C++ for one of my assignment. I have written my whole class and everything works good, but my assignment asks me to plot the run-times for:
a. A sorted list of 50000, 75000, and 100000 items
b. A random list of 50000, 75000, and 100000 items
That's fine, I can insert the numbers but it also asks me to call the FindHeight() and CountLeaves() methods on the tree. My problem is that I've implemented the two functions using recursion. Since I have a such a big list of numbers I'm getting getting a stackoverflow exception.
Here's my class definition:
template <class TItem>
class BinarySearchTree
{
public:
struct BinarySearchTreeNode
{
public:
TItem Data;
BinarySearchTreeNode* LeftChild;
BinarySearchTreeNode* RightChild;
};
BinarySearchTreeNode* RootNode;
BinarySearchTree();
~BinarySearchTree();
void InsertItem(TItem);
void PrintTree();
void PrintTree(BinarySearchTreeNode*);
void DeleteTree();
void DeleteTree(BinarySearchTreeNode*&);
int CountLeaves();
int CountLeaves(BinarySearchTreeNode*);
int FindHeight();
int FindHeight(BinarySearchTreeNode*);
int SingleParents();
int SingleParents(BinarySearchTreeNode*);
TItem FindMin();
TItem FindMin(BinarySearchTreeNode*);
TItem FindMax();
TItem FindMax(BinarySearchTreeNode*);
};
FindHeight() Implementation
template <class TItem>
int BinarySearchTree<TItem>::FindHeight()
{
return FindHeight(RootNode);
}
template <class TItem>
int BinarySearchTree<TItem>::FindHeight(BinarySearchTreeNode* Node)
{
if(Node == NULL)
return 0;
return 1 + max(FindHeight(Node->LeftChild), FindHeight(Node->RightChild));
}
CountLeaves() implementation
template <class TItem>
int BinarySearchTree<TItem>::CountLeaves()
{
return CountLeaves(RootNode);
}
template <class TItem>
int BinarySearchTree<TItem>::CountLeaves(BinarySearchTreeNode* Node)
{
if(Node == NULL)
return 0;
else if(Node->LeftChild == NULL && Node->RightChild == NULL)
return 1;
else
return CountLeaves(Node->LeftChild) + CountLeaves(Node->RightChild);
}
I tried to think of how I can implement the two methods without recursion but I'm completely stumped. Anyone have any ideas?
Recursion on a tree with 100,000 nodes should not be a problem if it is balanced. The depth would only be maybe 17, which would not use very much stack in the implementations shown. (log2(100,000) = 16.61). So it seems that maybe the code that is building the tree is not balancing it correctly.
I found this page very enlightening because it talks about the mechanics of converting a function that uses recursion to one that uses iteration.
It has examples showing code as well.
May be you need to calculate this while doing the insert. Store the heights of nodes, i.e add an integer field like height in the Node object. Also have counters height and leaves for the tree. When you insert a node, if its parent is (was) a leaf, the leaf count doesnt change, but if not, increase leaf count by 1. Also the height of the new node is parent's height + 1, hence if that is greater than the current height of the tree, then update it. Its a homework, so i wont help with the actual code
Balance your tree occasionally. If your tree is getting stackoverflow on FindHeight(), that means your tree is way unbalanced. If the tree is balanced it should only have a depth of about 20 nodes for 100000 elements.
The easiest (but fairly slow) way of re-balancing unbalanced binary tree is to allocate an array of TItem big enough to hold all of the data in the tree, insert all of your data into it in sorted order, and delete all of the nodes. Then rebuild the tree from the array recursively. The root is the node in the middle. root->left is the middle of the left half, root->right is the middle of the right half. Repeat recursively. This is the easiest way to rebalance, but it is slowish and takes lots of memory temporarily. On the other hand, you only have to do this when you detect that the tree is very unbalanced, (depth on insert is more than 100).
The other (better) option is to balance during inserts. The most intuitive way to do this is to keep track of how many nodes are beneath the current node. If the right child has more than twice as many "child" nodes as the left child, "rotate" left. And vice-versa. There's instrcutions on how to do tree rotates all over the internet. This makes inserts slightly slower, but then you don't have occassional massive stalls that the first option creates. On the other hand, you have to constantly update all of the "children" counts as you do the rotates, which isn't trivial.
In order to count the leaves without recursion, use the concept of an iterator like the STL uses for the RB-tree underlying std::set and std::map ... Create a begin() and end() function for you tree that indentifies the ordered first and last node (in this case the left-most node and then the right-most node). Then create a function called
BinarySearchTreeNode* increment(const BinarySearchTreeNode* current_node)
that for a given current_node, will return a pointer to the next node in the tree. Keep in mind for this implementation to work, you will need an extra parent pointer in your node type to aid in the iteration process.
Your algorithm for increment() would look something like the following:
Check to see if there is a right-child to the current node.
If there is a right-child, use a while-loop to find the left-most node of that right subtree. This will be the "next" node. Otherwise go to step #3.
If there is no right-child on the current node, then check to see if the current node is the left-child of its parent node.
If step #3 is true, then the "next" node is the parent node, so you can stop at this point, otherwise go the next step.
If the step #3 was false, then the current node is the right-child of the parent. Thus you will need to keep moving up to the next parent node using a while loop until you come across a node that is a left-child of its parent node. The parent of this left-child node will then be the "next" node, and you can stop.
Finally, if step #5 returns you to the root, then the current node is the last node in the tree, and the iterator has reached the end of the tree.
Finally you'll need a bool leaf(const BinarySearchTreeNode* current_node) function that will test whether a given node is a leaf node. Thus you counter function can simply iterate though the tree and find all the leaf nodes, returning a final count once it's done.
If you want to measure the maximum depth of an unbalanced tree without recursion, you will, in your tree's insert() function, need to keep track of the depth that a node was inserted at. This can simply be a variable in your node type that is set when the node is inserted in the tree. You can then iterate through the three, and find the maximum depth of a leaf-node.
BTW, the complexity of this method is unfortunately going to be O(N) ... nowhere near as nice as O(log N).