Trees Flashcards

1
Q

What is a complete binary tree?

A

BinTree with all levels full, except bottom level. Last level is filled starting from the left.

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

What is a full binary tree?

A

BinTree where all nodes have 2 children, other than the leaves

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

how is the height of a tree calculated?

A

the max level of all nodes

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

how is the level of a node calculated?

A

numbers of edges from root node!

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

What are the 2 properties of a (min/max) binary heap?

what are the 2 properties referred to as?

A
  1. [shape property] complete binary tree structure
  2. [heap property] for min: parents always <= children
    for max: parents always >= children
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When representing bin-heaps with arrays, which index do you start with for the root node?

A

start with index 1, instead of index 0

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

How do you find the index of a child?

A

My child is 2x me

left child = 2p
right child = 2p + 1

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

How do you insert a new value into the binheap list?

A

append to end of list, then switch values to maintain heap property

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

How do you maintain heap property once you’ve inserted new value to end of list?

A

Compare value with value of parent;

for min BinHeap, if child smaller than parent, switch child and parent, etc.

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

When we remove the root node (min/max value), how do we preserve the heap property?

A

move the last item in the list to the front of list, then percolate down

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

How do you percolate down to preserve heap property?

A

compare parent to 2 children, switch with the child that is larger than parent and other child (max heap)

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

What’s the binary search tree (BST) property?

A

all left subtree values to a node are smaller than the value in the node
all right subtree values to a node are greater than the value in the node

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

How do preorder, postorder and inorder BST traversal work?

A

Do this recursively, starting with the root node & its children

Preorder: parent - left child - right child
Inorder: left child - parent - right child
Postorder: left child - right child - parent

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

For the BST, what’s an algorithm for finding a node corresponding to a particular value?
(1st pseudo code, 2nd Python code)

A

check if first node’s value is EQUAL to the value, if yes, return the node:
else: if node’s value is larger than the value we’re finding, recursively perform function on left child
elif:
do opposite:
else: return None

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

What’s an algorithm for inserting a node with a value into an existing BST?
(1st pseudo code, 2nd Python code)

A

1st:
check if the value is the same as the value of the current node (we can only insert unique values?)
if yes, return (do nothing)

2nd:
check if the value is smaller than the value in the current node
if it is, check if a left child exists:
if yes, recursively call inserting function, using that left child as current node i.e. self.left.insert(value)
if no, make new left child BST object with self.left = BST(value, parent = self)

repeat for checking if the value is larger than the value in the current node

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

What’s an algorithm for creating a BST using elements from a list?
(1st pseudo code, 2nd Python code)

A

def create(a_list):

1st make root node BST object using the first element
2nd iteratively use the insert function for every other element in the given list

17
Q

What’s an algorithm for deleting a value in a BST, without any children?

A

2 (bigger) cases:

a) not deleting a value from a root node

i) check if a node has a parent:
if yes => check if the root node is left the child or right child
if left, set self.parent.left = None
if right, set self.parent.right = None

b) deleting value from root node

simply set root node’s value to None (self.value = None)

18
Q

What’s an algorithm for deleting a value in a BST, with one child?

A

don’t want to remove root node bc otherwise we have no BST

Make set a variable ‘child’ to self.left or self.right

  1. Check if value is root node or not

If not root node:
see if current is parent’s left or right child
set child to parent’s left or right child

If root node, replace root node value with the child, using a method: def replace(self, other)

19
Q

What’s an algorithm for deleting a value in a BST, with two children?

A

Find the inorder successor node
- go to the right child, go down to find node with maximum of 1 child
connect successor’s child to successor’s prev parent

20
Q

What’s an efficient way to construct a BinHeap from a list?

A

Start half-way through the list (all leaves, or one edge from a leaf)
Percolate down for all nodes, until you reach the root node