Trees Flashcards

1
Q

Explain binary trees

A

In general, nodes in trees can have any number of children. However, for binary trees every node has two or fewer children. The number of children each node has in a tree is called the branching factor, and each node has exactly one parent. The branching factor for a binary tree is 2.

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

What are leaves?

A

Nodes pointing to no other nodes are called leaves.

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

What is the depth of a node?

A

Nodes pointing to no other nodes are called leaves.

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

What is the node height?

A

The node height is the number of nodes from between the original node and the deepest leaf that is a descendent.

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

What is the tree height?

A

The tree height is the node height of the root, i.e. the number of branches along the longest path in the tree.

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

What is the largest number of nodes a binary tree can have that has a tree height of 5?

A

2n - 1 // n being 6 because of including the root node

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

What is a full binary tree?

A

A full binary tree is a binary tree in which each node has exactly zero or two children.

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

What is a complete tree?

A

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

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

What is a traversal?

A

so that you can navigate the tree and find all the data it contains. This is known as a traversal, since it traverses the whole binary tree.

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

What is the code for a pre-order traversal?

A
def traverse(tree):
    if tree:
        print(tree.getRootVal())
        traverse(tree.getLeftChild())
        traverse(tree.getRightChild())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the code for an in-order traversal?

A
def traverse(tree):
    if tree:
        traverse(tree.getLeftChild())
        print(tree.getRootVal())
        traverse(tree.getRightChild())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the code for a post-order traversal?

A
def traverse(tree):
    if tree:
        traverse(tree.getLeftChild())
        traverse(tree.getRightChild())
        print(tree.getRootVal())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is DFS?

A

DFS, is the strategy that goes down a branch all the way until a leaf is reached, processes said branch, and then moves on to a different branch.

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

Is the depth-first search always the way to go? For what cases might there be a better solution?

How about if you would like to see how many connections there are between you and another person on Facebook?

A

BFS, is the counterpart to DFS in tree traversing techniques. It is a search algorithm that traverses down a tree using a queue as its data array, where elements are visited in a FIFO, first-in first-out, mechanism.

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

How is BFS often used?

A

This type of search is often used when looking for the “nearest” solution.

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

What is another example of BFS?

A

finding the shortest route from point A to point B (e.g. directions from “Google Maps”)

17
Q

As we’ve seen, binary trees can be used for storing data; however, how can we store data in a binary tree in a useful manner, i.e. so that we can find and look up data efficiently?

A

In a BST, for any parent (including the root) all elements in any left sub-tree must be smaller than the parent, and all elements in any right sub-tree must be larger than any element in the left sub-tree.

18
Q

Which of the following trees will have a better worst case runtime?

A balanced BST with a depth of 20
An unbalanced BST with a depth of 18

A

Although the second one is unbalanced, the number of comparisons needed will be determined by the depth of the tree. (Worst case, it traverses all the way to the bottom of the tree.) Since B has a smaller depth than A, it will have a better worst case runtime.

19
Q

What is a tree rotation?

A

A tree rotation is an operation on a binary tree that changes the structure of the tree without affecting the order of the elements (as read in by an in-order traversal).

20
Q

What do tree rotations do?

A

Tree rotations change the height of the tree by moving up larger subtrees.

21
Q

What is an AVL tree?

A

An AVL tree, named after its inventors, Adelson-Velskii and Landis, is a self-balancing BST that applies a tree rotation automatically.