Binary Search Trees Flashcards
what is a binary search tree?
1 - binary tree (at most have 2 children)
2 - left subtrees are lesser than the parent
3 - right subtrees are greater than the parent
where do u start in a BST?
we always start at the root of the tree
what to do when u want to delete a node with two children?
you must search for the smallest bigger leaf than the node u want to remove and replace it with that node and then delete it
what to do with duplicate values?
it depends on the application and if u insert it it would be better to insert it into the right of the node which makes it a stable tree
what is the structure of a BST?
it has a linked list structure
what does each node need?
1 - a value
2 - a parent
3 - a left child
4 - a right child
what are the types of traversal in BST?
1 - PreOrder (DFS)
2 - PostOrder (DFS)
3 - InOrder
4 - Level Order (BDS)
what is a PreOrder Traversal?
visit yourself
then visit all your left subtree
then visit all your right subtree
what is a PostOrder Traversal?
then visit all your left subtree
then visit all your right subtree
visit yourself
what is a InOrder Traversal?
then visit all your left subtree
visit yourself
then visit all your right subtree
what is a Level Order Traversal?
it’s breadth first search where u can’t use recursion u must use a queue or a list
u add an element to the queue and go and remove it and when u remove it u add the children to the list as long as the queue is not empty
why is BST better than binary search on an array?
cuz in an array insertion and removal is harder
how to search in a BST?
1 - compare for the current element if they are equal return
2 - if it’s smaller go to the left
3 - if it’s bigger go to the right
what is the airport reservation system problem?
u have an airport with landing zones and u want the airplanes to request for landing in T time to the set R and keep track of K (available time to land) and u want to delete from R set when a plane lands all of that in log(N)
how to find min()
go to the left until there is no left
O(H) where h is the height