Trees, Binary Trees, BSTs Flashcards

1
Q

What is the “depth” of a node x. Depth is always relative to…

A

Length of path from the root of the TREE to that node. Depth is always relative to the root of the tree

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

The depth of a node x is also its…

A

level in the tree

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

Siblings or cousins of a tree have the same…(starts with a d)

A

depth

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

What is the height of a node x

A

Height of any node x in a tree is the number of edges of the longest path from that node to a leaf node.

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

What is the height of any leaf node?

A

0

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

What is the height of a tree?

A

Height of root node

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

What is the only requirement for a binary tree

A

All nodes can have at most two children

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

What is a full binary tree?

A

a binary tree in which every node has two or zero children Another definition is: a binary tree in which every interior node has exactly two children

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

What is a complete binary tree?

A

A binary tree where all levels except possibly the last level are completely filled and as far left as possible

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

What is the max number of leaves you can have in a complete binary tree?

A

2^h where h is the height of the root

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

What is a perfect binary tree?

A

A binary tree where all levels are completely filled

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

True or false: a perfect binary tree is also a complete tree

A

True

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

How many nodes are there in a perfect tree with height h?

A

2^(h+1)-1 or 2^(levels in the tree) - 1

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

What is the height of a perfect binary tree with n nodes?

A

h = log_2(n+1) - 1

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

What is the equation for the height of a complete binary tree?

A

floor of log_2(n) where n = # of nodes

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

Time complexity of inserting, searching, and removing an element is proportional to its _____ in a BST. Thus, time complexity can be said to be O of ______ in terms of the tree’s _____

A

tree height. h, height

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

What is the minimum possible height in a BST?

A

h = floor of log_2(n)

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

What is the maximum worst case possible height in a BST?

19
Q

How do you know if a binary tree is balanced?

A

A binary tree is balanced if for EACH node the difference of the left and right subtree is not more than some number k (usually 1)

20
Q

What is the height of a leaf node?

21
Q

What is the height of an empty tree?

22
Q

How do you determine the balance factor of a node?

A

height of right subtree - height of left subtree

23
Q

What is the value you get when a node is right heavy?

A

positive number greater than 1

24
Q

What is the value you get when a node is left heavy?

A

negative number less than -1

25
What is the value you get when a node is evenly balanced?
0
26
What is the time complexity of search, insertion, and removal in AVERAGE case in a BST?
All of them are O(log n)
27
What is the WORST CASE time complexity of search, insertion, and removal in a BST? What would a BST look like in this case?
O(n). BST would like like a linked list
28
What are the properties required for a BST?
Nodes can have at most 2 children Values in the right subtree/child are greater than or equal to the root node values in the left subtree/child are less than the root node value
29
Pseudocode for finding height of a tree
find\_height(root): if root is None: return - 1 left\_height = find\_height(root.left) right\_height = find\_height(root.right) return max(left\_height, right\_height) + 1
30
What is breadth first/level-order traversal? traversal in a binary tree?
Breadth first visits every node from left to right, level by level.
31
Explain depth first traversal
Depth first visits the depth of one subtree before the other subtree
32
What are the three types of depth first traversal?
1. pre-order traversal 2. in-order traversal 3. post-order traversal
33
Explain pre-order traversal. What's the order of processing data?
Root, left subtree, right subtree
34
Explain in-order traversal. What's the order of processing data?
Left subtree, root, right subtree
35
Explain post-order traversal. What's the order of processing data?
Left subtree, right subtree, root
36
Which traversal method gives you a sorted order of the values of the binary search tree?
in order traversal
37
What is the time complexity of level order traversal?
O(n)
38
What's the pseudocode for level order traversal?
queue() queue.push(root\_of\_tree) while queue is not empty: curent\_node = queue.pop() if current\_node.left\_child is not none: queue.push(left\_child) if current\_noderight\_child is not none: queue.push(right\_child)
39
Whats the pseudocode for pre-order traversal?
DynamicArray() # for storing the values we visit def pre\_order\_trav(current\_node, dynamic\_array): if current\_node: dynamic\_array.push(current\_node.data) self. pre\_order\_trav(current\_node.left) self. pre\_order\_trav(current\_node.right) return dynamic\_array
40
What is the pseudocode for in-order traversal?
DynamicArray() # for storing the values we visit def in\_order\_trav(current\_node, dynamic\_array): if current\_node: self.in\_order\_trav(current\_node.left) dynamic\_array.push(current\_node.data) self.in\_order\_trav(current\_node.right) return dynamic\_array
41
What is the pseudocode for post-order traversal?
DynamicArray() # for storing the values we visit def post\_order\_trav(current\_node, dynamic\_array): if current\_node: self. pre\_order\_trav(current\_node.left) self. pre\_order\_trav(current\_node.right) dynamic\_array.push(current\_node.data) return dynamic\_array
42
What are the three cases to consider when removing a node from a BST?
Case 1: Leaf node or node with no children Case 2: Node with one child Case 3: Node with three children
43
Inserted elements into a BST are always inserted as \_\_\_\_\_\_
leaves