Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than the values in all the nodes in that node's right subtree.
Write a function that checks if a given binary search tree contains a given value.
For example, for the following tree:
n1 (Value: 1, Left: null, Right: null)
n2 (Value: 2, Left: n1, Right: n3)
n3 (Value: 3, Left: null, Right: null)
Call to contains(n2, 3) should return true since a tree with root at n2 contains number 3.
Compiles and works for the test example.
However, the test fails in Correctness
(for further automatically generated test cases I know not of)
and Performance (for very large trees).
Since I had this issue once before I would really appreciate a thorough explanation to finally fully understand the missing link in my thought pattern.
That is correct! Thanks, that solves the correctness issue.
Leaves the performance issue for large trees. Im not exactly sure when this performance issue occurs.
Could it mean when not in O(log n) but in O(n^2)?
From my opinion the algorithm is in O(log n) because it always searches in one side of the tree thus theoretically reducing the remaining nodes to one half on each iteration.
are you asking theory stuffs?
in theory a binary search tree is binary, and find the item in log(n) rounded up (or +1 if you like).
but not all trees are binary. If you don't rebalance them, consider the less left, greater right algorithm. Now insert in order: 1,2,3,4,5,... 10
your tree is now a linked list and performs in O(N), as every value followed the greater right rule.
you have to rebalance it, find the central node (5 here) and rebuild off that.
in practice, it depends on what you want. A big tree that changes little, but is searched a lot, is better stuffed into a vector (even if you take the [index] of the vectors and use those as 'pointers' to make a 'tree' out of it). A big tree that changes a lot needs some thought and design to make it perform at the max, as page faults and memory problems plague standard pointer trees but vectors are frustrating with inserts/updates. You have to think through what the data structure will DO most of its life and make THOSE operations as efficient as can be. The classic structures are a starting point, but often you need a hybrid if you want speed.
Your understanding is correct. It throws away 1/2 each time if balanced. The more out of balance it gets, the less it is like lg(n) and it slowly becomes O(n).