Each node x in the binary tree has a key key(x).Nodes other than the root have a parent p(x).Nodes may have a left child left(x) and/or a right child right(x). ps:ALL POINTERS!Unlike in the Heap.
Characteristics:for any node x, for all nodes y in the left subtree of x, key(y) ≤ key(x). For all nodes y in the right subtree of x key(y) ≥ key(x).
Insert
As the picture shows below🤓
Under the problem,we need to do the “Within K minutes Check” before inserting.If doesn’t follow,then stop the insertion.
Find Exists : find(val)
Follow left and right pointers until you find it or hit NIL.
Find the minimum element in a BST : findmin()
Just go left until you can’t.
Complexity
All the operations above have an O(h) time complexity. ps: h is the height of BST
We may find a problem: somehow in a tricky (or abstract) way of insertion, the BST may turn to a List.So the complexity will be O(n),but we hope O(logn).😣😣😣
We’re gonna talk about it next time in the AVL Chapter😋😋😋 Balanced BSTs to the rescue in the next lecture!
Find the next larger element: next_larger(x)
IF right child is not NIL,return minimun(x -> right) else y = parent(x)
while y not NIL and x = right(y)
x = y
y = parent(y)
return y;
How many planes are scheduled to land at times ≤ t?
Algorithm: 1.Walk down tree to find desired time( find t pos ) 2.Add in nodes that are smaller 3.Add in subtree sizes to the left( record the size of the subtrees)