Binary trees Flashcards

1
Q

Preorder reversal - code and traversal order?

A

Preorder (D,L,R)

void Preorder(Node *root) {
	if (root == null) return;
	printf("%c ", root->data);
	Preorder(root->left);
	Preorder(root->right);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In-order reversal - code and traversal order?

A

In-order (L,D,R)

void Inorder(Node *root) {
	if (root == null) return;
	Preorder(root->left);
	printf("%c ", root->data);
	Preorder(root->right);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Postorder (L,R,D) - code and traversal order?

A

Postorder (L,R,D)

void Postorder(Node *root) {
	if (root == null) return;
	Preorder(root->left);
	Preorder(root->right);
	printf("%c ", root->data);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the time and space complexity for depth-first traversal?

A
  • Time complexity = O(n) for all cases
  • Space complexity = O(h) — height of the tree.
    Worst O(n),
    Best/average O(log n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the depth-first traversal algorithms?

A
  • Preorder
  • Inorder
  • Postorder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the breadth-first traversal algorithm?

A
  • Level-order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the recipe for level order traversal?

A

LEVEL ORDER RECIPE (Queue (FIFO)):

  1. Create queue with root node as the first item in queue
  2. While queue NOT empty -> while loop
  3. Dequeue front element (current), add left & right to end of queue
#include
void LevelOrder(Node *root) {
	if (root == null) return;
	queue Q;
	Q.push(root);
	while(!Q.empty()) {
		Node* current = Q.front();
		if(current->left != null) 
			Q.push(current->left);
		if(current->right != null)
			Q.push(current->right);
		Q.pop();
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the time and space complexity of level order traversal?

A

Time complexity = O(n) — all cases irrespective of tree size

Space-complexity = O(1) — best, O(n) — worst/average

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

C++ syntax: how do you import queue?

A

include

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

C++ syntax: what’s q.empty() do?

A

Returns boolean true if queue has 0 items

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

C++ syntax: what’s q.pop() do?

A

Removes first/next item

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

C++ syntax: what’s q.front() do?

A

Returns first/next item of the queue without removing it.

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

How do you find the height of a tree recursively with code?

A
int FindHeight(struct Node *root) {
	if (root == NULL) return -1;
	return max(
		FindHeight(root->left),
		FindHeight(root->right)
	) + 1;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the 4 areas in memory allocation?

A
  • Heap
  • Stack
  • Global/static
  • Code (text)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s the difference between height count and depth count?

A

Depth count = the leaf nodes or bottom of the tree

Height = top of the tree or root node (e.g. height = 0 at the leaf nodes, height = 3 at root)

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