Binary Search Tree Flashcards
Three rules of binary search tree
- Each node can have up to two children: a left child, a right child, both, or neither. Each child is a node as well, and is the root of its corresponding subtree (e.g., the root’s left child is the root of the left subtree).
- The values of each node in the left subtree must be less than the value of the root.
- The values of each node in the right subtree must be greater than the value of the root.
Describe steps to insert an element into the tree
Compare value to root to decide into which side of the tree value should be inserted.
If value > root, check for root’s right child. If root has a right child, perform insert(value, root.right_child). Otherwise, make value into root’s right child.
If value < root, check for root’s left child. If root has a left child, perform insert(value, root.left_child). Otherwise, make value into root’s left child.
How do you delete a node with both left and right children?
Find a replacement by going through the left child then finding the maximum element
https://github.com/appacademy/sf-job-search-curriculum/blob/master/algorithms/binary_search_trees/bst_reading.md
In order binary tree traversal
Visit(print) the left branch, then current node, then right branch
Pre order traversal of binary tree
Visit current node before child nodes
Post order traversal binary tree
Visit current node after visiting child nodes