coding a binary search tree
Date : March 29 2020, 07:55 AM
hope this fix your issue The problem is with how you're recursing. You're recursing with max_heap(key--); which effectively means "first call max_heap(key); and then set key = key - 1;". This gives you an endless loop since each call to your function is made with the same key that was originally provided. You will get better results if you pre-decrement (max_heap(--key);). That being said, this is not a good problem to solve with recursion. A while loop would be much better.
|
Tips on solving binary tree/binary search tree problems using recursion (Java)
Date : March 29 2020, 07:55 AM
help you fix your problem First off, always remember that you're working on a BST, not an unsorted binary tree or non-binary general tree. That means at all times you can rely on the BST invariant: every value in left subtree < this < every value in right subtree. (equality included on one of the sides in some implementations). Example where this is relevant: in BST searching, if the value you're trying to find is less than this, there's no point looking in the right subtree for it; it's not there. Other than that, treat it like you would any recursion problem. That means, for a given problem:
|
Coding remove() function for a Binary Tree
Tag : cpp , By : user157064
Date : March 29 2020, 07:55 AM
hope this fix your issue Instead of moving the value to the last leaf and then delete the leaf, you should delete it when you are moving the values up since it is at this moment you will find the leaf and will know his exact position. What I would suggest is to test if the left or right child of the current node is a leaf and if it is, delete it since you already move the value of it in the current node. std::shared_ptr<BinaryNode<ItemType>> BinaryNodeTree<ItemType>::
moveValuesUpTree(std::shared_ptr<BinaryNode<ItemType>>subTreePtr) {
if(subTreePtr) {
if(!subTreePtr->isLeaf()) {
if(subTreePtr->getLeftChildPtr()) {
subTreePtr->setItem(subTreePtr->getLeftChildPtr()->getItem());
if(subTreePtr->getLeftChildPtr()->isLeaf())
//Delete left child here
else
moveValuesUpTree(subTreePtr->getLeftChildPtr());
} else if(subTreePtr->getRightChildPtr()) {
subTreePtr->setItem(subTreePtr->getRightChildPtr()->getItem());
if(subTreePtr->getRightChildPtr()->isLeaf())
//Delete right child here
else
moveValuesUpTree(subTreePtr->getRightChildPtr());
} // end if
} // end if
} // end if
return subTreePtr;
} // end moveValuesUpTree
|
Coding an iterator to traverse binary tree
Date : March 29 2020, 07:55 AM
|
Balance Binary tree Coding
Date : March 29 2020, 07:55 AM
|