Section 10: Tree Recursion, Traversals, and Iterators (109–120)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

14 Terms

1
New cards

Recursive calculation of binary tree size

1+size(left)+size(right)1 + \text{size}(\text{left}) + \text{size}(\text{right}), where an empty tree has a base size of 00.

2
New cards

Recursive calculation of binary tree height

1+max⁡(height(left),height(right))1 + \max(\text{height}(\text{left}), \text{height}(\text{right})), relative to the base height of an empty tree.

3
New cards

Time complexity of recursive binary tree size calculation

O(n)O(n), because every node in the tree must be visited.

4
New cards

Time complexity of recursive binary tree height calculation

O(n)O(n), because every node in the tree may need to be visited.

5
New cards

Auxiliary space complexity of recursive binary tree traversal

O(h)O(h), where hh is the tree height, due to the call stack frames allocated during recursion.

6
New cards

Order of nodes visited in a preorder traversal

Root→Left→Right\text{Root} \rightarrow \text{Left} \rightarrow \text{Right}

7
New cards

Order of nodes visited in an inorder traversal

Left→Root→Right\text{Left} \rightarrow \text{Root} \rightarrow \text{Right}

8
New cards

Order of nodes visited in a postorder traversal

Left→Right→Root\text{Left} \rightarrow \text{Right} \rightarrow \text{Root}

9
New cards

Preorder iterator stack mechanism

Pop and visit the top node, then push its right child followed by its left child onto the stack.

10
New cards

Reason a preorder iterator pushes the right child before the left child

Because a stack operates as last-in, first-out (LIFO), pushing the right child first ensures the left child is popped and processed next.

11
New cards

How an inorder iterator prepares the next node

Pushes nodes onto the stack while traversing down the left subtree until reaching a node without a left child.

12
New cards

Inorder iterator procedure after popping and visiting a node

If the popped node has a right subtree, move to that right child and cascade down its leftmost path, pushing each node encountered onto the stack.

13
New cards

Decision rule for exploring a right subtree in a postorder iterator

After popping a node, check the parent node on top of the stack; if the popped node was the parent's left child, traverse and process the parent's right subtree before visiting the parent.

14
New cards

Function of next() in a binary tree iterator

Returns the next element according to the chosen traversal order and updates the iterator's internal state.