Binary Trees Flashcards

1
Q

What are ancestor nodes?

A

nodes that are between the current node and the root node, including the root node

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

What is the root node?

A

the very first node of the tree?

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

What is a grandparent node?

A

the parent node of the parent node of the current node

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

What are leaf nodes?

A

nodes without any children

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

What is a binary tree?

A

a data structure made up of nodes, where each node has at most two children nodes

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

How do you calculate the depth of a node?

A

The amount of parent nodes between the current node and the root node, including the current node

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

What is the height of a tree?

A

the height of its root node. the amount of its children node layers.

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

What is a complete binary tree?

A

every level except possible the last, is completely filled and all the nodes in the last level 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 full binary tree?

A

a tree in which every nodes has either 0 or 2 children

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

How do we implement a Binary Tree?

A

We create a class Node. The Node class will have three attributes: value, left, right. We create a class BinaryTree. WIthin the BinaryTree class, we use the Node class to create new nodes in our binary tree

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

Tree traversal

A

is the process of visiting each node in the tree data structure exactly once. There are multiple ways to traverse a free: depth-first or breadth-first order.

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

What are the Three ways to traverse a tree in depth-first oder?

A

In-order, pre-order, post-order

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

Explain pre-order depth-first tree traversal

A

follows the order: Root -> Left -> Right

checks if the current node is empty, displays the value of the current node, recursively calls the pre-order function on the left subtree, then recursively calls the pre-order function of the right subtree

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

Explain in-order depth-first

A

similar to pre-order depth-first, except it follows the order of: Left -> Root -> Right

make recursive call to the left child, concat the value of the current node with traversal, make a recursive call to the right subtree

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

Explain post-order depth-first

A

make recursive call to the left subtree and the right subtree before concatenating the value of the current node with traversal

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

Explain level-order breadth-first

A

we leverage a queue. We enqueue the root node. We dequeue from the queue and add it to the Traversal. We enqueue the children of the node we dequeued. We dequeue first node in the queue. Add it to the Traversal. And enqueue its children. And we repeat

17
Q

Explain reverse level-order traversal

A

we leverage a queue and a stack. It’s similar to the level-order except we enqueue the right child before the left child. And after we dequeue the node from the queue we push it onto the stack. After, we check if the node has children, if so we enqueue the children. We repeat this process until the queue becomes empty. At which point, we pop the elements from the stack, appending to the traversal string to achieve the reversed result

18
Q

Calculate the height of a tree

A

break down the problem using recursion and traverse thru the left and right subtree of a node to calculate the height of that node. Calculate the max of the two heights and add one to get the height of the tree