Binary Search Tree Flashcards

1
Q

Three rules of binary search tree

A
  1. 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).
  2. The values of each node in the left subtree must be less than the value of the root.
  3. The values of each node in the right subtree must be greater than the value of the root.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe steps to insert an element into the tree

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you delete a node with both left and right children?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In order binary tree traversal

A

Visit(print) the left branch, then current node, then right branch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Pre order traversal of binary tree

A

Visit current node before child nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Post order traversal binary tree

A

Visit current node after visiting child nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly