How to find and return bottommost(Deepest Node) node of a binary tree? binary search tree?
By : R. Casey
Date : March 29 2020, 07:55 AM
should help you out Even with a balanced tree you'll have to check every subtree to find the deepest node, like: code :
struct RESULT{
Node *node;
int level;
};
RESULT getDeepest( Node *root ){
if( root == NULL ){
RESULT result = {NULL, 0};
return result;
}
RESULT lResult = getDeepest( root>left );
RESULT rResult = getDeepest( root>right );
RESULT result = lResult.level < rResult.level ? rResult : lResult;
++ result.level;
if( result.node == NULL )
result.node = root;
return result;
}

Binary Tree (Not Binary Search Tree) Creating New Node and Children
By : jhoga1956
Date : March 29 2020, 07:55 AM
hop of those help? In createNewNode() function, you just create a new node and leave it without relating them to each other! You should bind it to left or right pointer. This is what you should do:

Filling empty Binary tree as Binary search tree without changing structure (Node linkage)
By : Michelle
Date : March 29 2020, 07:55 AM
wish helps you Not only is your solution correct, it's impossible to do better (in the asymptotic sense), assuming that only < or > comparisons are allowed between data items. Your solution involves sorting the data, which takes time O(n log n), and then inserting it into the tree in an inorder traversal, which takes time O(n), for an overall time complexity of O(n log n). Notice that after building a binary search tree, we can read out all its data in sorted order using an inorder traversal  that is, solving the interviewer's problem can be used to sort any given sequence of data elements.

Why does my Binary search tree crash when I delete a Node that doesn't exist in the tree?
By : MediaLinkersus
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Because your code references child.value before checking to see if child is null. You have this loop: code :
while(element.compareTo(child.value) != 0)
{
if(child == null)
{
System.out.println("Element not found.");
return;
}
else if(element.compareTo(child.value) < 0)
{
parent = child;
child = child.leftChild;
}
else
{
parent = child;
child = child.rightChild;
}
}
while (child != null && element.compareTo(child.value) != 0)
{
// do stuff here
}
// moved from inside the loop
if(child == null)
{
System.out.println("Element not found.");
return;
}

Binary Search Tree insert function failing to add a new node to the tree
By : Gizmo
Date : March 29 2020, 07:55 AM
around this issue insertNode2 doesn't correctly perform an insertion as expected because of the line node = TreeNode(val), which makes a purely local assignment to node. This new object is never set to its parent .left or .right property and is lost when the function returns. The root node will not be modified in any run of this function. Either use the alreadyworking insertNode1, or add a return node statement to insertNode2 and make an assignment in the parent function call scope to the new child. code :
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BinarySearchTree:
@staticmethod
def p(root, depth=0):
if root:
print(" " * depth + str(root.val))
BinarySearchTree.p(root.left, depth + 2)
BinarySearchTree.p(root.right, depth + 2)
@staticmethod
def insert(node, val):
if not node:
return TreeNode(val)
elif node.val > val:
node.left = BinarySearchTree.insert(node.left, val)
else:
node.right = BinarySearchTree.insert(node.right, val)
return node
if __name__ == "__main__":
root = TreeNode(5)
for n in [2, 1, 3, 7, 9, 6]:
BinarySearchTree.insert(root, n)
BinarySearchTree.p(root)
5
2
1
3
7
6
9

