Binary Tree Flashcards

1
Q

Nodes

A

the elements themselves

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

Edges

A

Links between nodes

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

Parent vs Child

A

Parent is the top node, children are under it

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

Inner Node

A

Nodes that have both parent and child

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

Leaf node

A

node with no children

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

What do levels start at

A

root node is level 0

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

Rules of Binary Trees

A

Each node has at most 2 children

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

Balanced vs Not Balanced vs Perfect

A

Balanced - difference between the height of the left subtree and the right subtree is at most 1

Perfect - All parents have 2 children, all leaves are at the same level

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

Preorder, Inorder & Postorder Traversal

A

Preorder - root -> left subtree -> right subtree
Inorder - left subtree -> root -> right subtree
Postorder - left subtree -> right subtree -> root

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

Binary Search Tree

A

Every node has a key

All keys in the left subtree are less than the key
All keys in the right subtree are greater than the key

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

Complexity of Search for BST

A

Best Case: O(1) if it’s root
Worst Case: O(depth)

If its balanced - its O(log n), if its unbalanced its O(n)

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

How to insert a node in a BST

A

if current is None
current = create_node
elif key < current.key
current.left = recursive_call
elif key > current.key
current.right = recurisve_call
else: (meaning key == current.key)
raise Error (duplicate)
return current

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

Complexity of insert BST

A

Best case is O(1) - occurs when adding to left subtree and all elements are on right side or vice versa

Worst Case is O(Depth)*comp(< & >) - depends if its balanced or not

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

Deleting a node

A
  • If it has no children - just remove the link
  • If it has one child - then just update the corresponding side
  • if it has two children - you need to find the nodes successor - i.e. the node with the next largest key - do this by moving one step to the right until u find a leaf - this can then substitute the deleted node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to collect the leaves of the tree

A

If current is not None:
if current is a leaf: add to list
else: go left and go right recursive

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

What data structure should be used for a BST iterator

A

A stack