0% found this document useful (0 votes)
19 views50 pages

L5 1 2 Apr - Tree Part 1

Uploaded by

Punnag Choudhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views50 pages

L5 1 2 Apr - Tree Part 1

Uploaded by

Punnag Choudhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Lost in the Forest

FM131 Programming and Data Structures


1 APR 2024
Logistics
How are you ?
Exam

Stacks
Trees
What is a graph?
Nodes

Edges
- Directed
- Undirected
What is a graph?
Nodes

Edges
- Directed
- Undirected

Cycles
What about a Tree?
A tree is defined as a graph
without any undirected cycles (i.e.,
cycles that would come about if
we replaced directed edges with
undirected edges) nor
any unconnected parts.
Trees with Special Cases?
A tree with no nodes is a valid tree called
the "null" (or "empty") tree

A tree with a single node (and no edges) is


also a valid tree
Trees with Special Cases?
Rooted Tree Un Rooted Tree
Think 2 mins – (NO TALKING)
Which of the following are valid trees? (Note all that apply)
Take a vote
1. A B C D E

2. A B C E

3. A B D E

4. B C E
Answer A: This graph has no undirected loops (no edges at all), so it's
a valid tree
1. A B C D E
B: Even though this graph is visualized in a counterintuitive
way, it has no undirected loops, so it's a valid tree
2. A B C E
C: This graph has no undirected loops, so it's a valid tree

3. A B D E D: Even though this graph has no directed loops, it does have


an undirected loop, so it's not a tree

4. B C E E: This graph has no undirected loops, so it's a valid tree


Trees examples?
What is a Tree?
For a graph T to be considered a tree, it must follow all of the following constraints:
•T is connected and has no undirected cycles (i.e., if we made T's directed edges
undirected, there would be no cycles)

•T is acyclic, and a simple cycle is formed if any edge is added to T

•T is connected, but is not connected if any single edge is removed from T

• There exists a unique simple path connecting any two vertices in T


What is a Tree?

If T has n vertices (where n is a finite number), then the previous statements


are equivalent to the following two conditions:

•T is connected and has n −1 edges

•T has no simple cycles and has n −1 edges


Rooted Binary Trees
What is a A Rooted Binary Tree?

Rooted binary trees, are rooted trees with the


following two restrictions:

• All nodes except the root have a parent

• All nodes have either 0, 1, or 2 children


Think 2 mins – (NO TALKING)
Which of the following are valid rooted binary trees? (Select all that apply)
Take a vote
1. A B C D E

2. A B C E

3. A B D E

4. B C E
Answer A: We explicitly defined the empty graph as a valid rooted
binary tree

1. A B C D E B: Even though this graph is visualized counterintuitively, it


has a single root (1) with two children (2 and 4), and 2 has a
single child (3), so all nodes have 0, 1, or 2 children
2. A B C E
C: This is a valid rooted tree, but it's not binary (1 has 3
children)
3. A B D E
D: The only node has 0 children, so it's a valid binary tree

4. B C E E: All nodes have 0, 1, or 2 children, so it's a valid binary tree


Traversing Binary Trees
Traversing A Rooted Binary Tree?
Pre-order - (V L R)

In-order - (L V R)

Post-order - (L R V )

Level-order - In Levels
Traversing A Rooted Binary Tree?
Pre-order - (V L R)

In-order - (L V R)

Post-order - (L R V )

Level-order - In Levels
Pre-Order (V L R)
In-Order (L V R)
Post-Order (L R V)
Think 4 mins – (NO TALKING)
In what order will the nodes of the following tree be visited in a pre-order traversal?
Take a vote
1. 7 3 4 1 8 5 9 6 2 0

2. 0 1 2 3 4 5 6 7 8 9

3. 0 1 3 7 4 2 5 8 6 9

4. 7 3 1 4 0 5 8 2 6 9
Answer

1. 7 3 4 1 8 5 9 6 2 0

2. 0 1 2 3 4 5 6 7 8 9

3. 0 1 3 7 4 2 5 8 6 9

4. 7 3 1 4 0 5 8 2 6 9
Think 4 mins – (NO TALKING)
Label the correct orders
A. 7 3 4 1 8 5 9 6 2 0

B. 0 1 2 3 4 5 6 7 8 9

C. 0 1 3 7 4 2 5 8 6 9

D. 7 3 1 4 0 5 8 2 6 9
Take a vote
A. 7 3 4 1 8 5 9 6 2 0

B. 0 1 2 3 4 5 6 7 8 9

C. 0 1 3 7 4 2 5 8 6 9

D. 7 3 1 4 0 5 8 2 6 9

1. A - Post, B - In, C Level & D - Pre 2. A - Post, B - Level, C- In & D - Pre


3. A - Post, B - Level, C Pre & D - In 4. A - Post, B - Pre, C - Level & D - In
Answer
A. 7 3 4 1 8 5 9 6 2 0

B. 0 1 2 3 4 5 6 7 8 9

C. 0 1 3 7 4 2 5 8 6 9

D. 7 3 1 4 0 5 8 2 6 9

1. A - Post, B - In, C Level & D - Pre 2. A - Post, B - Level, C- In & D - Pre


3. A - Post, B - Level, C Pre & D - In 4. A - Post, B - Pre, C - Level & D - In
Think 2 mins – (NO TALKING)
What is the worst-case time complexity of performing a pre-order, in-
order, post-order, or level-order traversal on a rooted binary tree with n nodes?
(All four traversals have the same worst-case time complexity)
Take a vote
What is the worst-case time complexity of performing a pre-order, in-
order, post-order, or level-order traversal on a rooted binary tree with n nodes?
(All four traversals have the same worst-case time complexity)

1. O(1)

2. O(n)

3. O(log n)

4. O(n log n)
Take a vote
What is the worst-case time complexity of performing a pre-order, in-
order, post-order, or level-order traversal on a rooted binary tree with n nodes?
(All four traversals have the same worst-case time complexity)

1. O(1)

2. O(n) Each traversal performs a O(1) operation


for each of the n nodes, yielding a O(n)
3. O(log n) algorithm overall

4. O(n log n)
Binary Search Trees
What is a A Binary Search Tree?

1. Rooted binary trees


2. Every node is larger than all nodes in its left
subtree
3. Every node is smaller than all nodes in its
right subtree
Think 2 mins – (NO TALKING)
Which of the following are valid Binary Search Trees? (Note all that apply)
Take a vote
1. A B

2. A B D

3. A

4. D

5. A D
Answer
1. A B

2. A B D

3. A
A: This is a valid BST

4. D B: 32 is in the right subtree of 42, but it is smaller than 42

C: This is not a binary tree

5. A D D: 45 is in the left subtree of 42, but it is larger than 42


Think 2 mins – (NO TALKING)

What is the space complexity of a Binary Search Tree with n elements?


Take a vote

What is the space complexity of a Binary Search Tree with n elements?

1. O(1)

2. O(n)

3. O(log n)

4. O(n log n)
Take a vote

What is the space complexity of a Binary Search Tree with n elements?

1. O(1)

2. O(n) Each node takes a constant amount of


space, and we create exactly one node per
3. O(log n) element, so it is O(n) overal

4. O(n log n)
Find Algorithm for BST-
1. Start at the root.

2. If query == current, success!

3.Otherwise, if query > current, traverse right and go to #2

4.Otherwise, if query < current, traverse left and go to #2

5.If you ever try to traverse left/right but no such child exists, fail!
Find Algorithm for BST-
1. Start at the root.

2. If query == current, success!

3.Otherwise, if query > current, traverse right and go to #2

4.Otherwise, if query < current, traverse left and go to #2

5.If you ever try to traverse left/right but no such child exists, fail!
Think 5 mins – (NO TALKING)
Write a function sortedPrint(Node* node) that recursively prints the labels of all
nodes in the subtree rooted at node in ascending sorted order. Print a single
label per line.
You can assume that the Binary Search Tree structure is valid.

HINT: Does one of the rooted binary tree traversal algorithms (pre-order, in-
order, and post-order) seem useful in this context?
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Pair 3 mins – (TALKING)
Write a function sortedPrint(Node* node) that recursively prints the labels of all
nodes in the subtree rooted at node in ascending sorted order. Print a single
label per line.
You can assume that the Binary Search Tree structure is valid.

HINT: Does one of the rooted binary tree traversal algorithms (pre-order, in-
order, and post-order) seem useful in this context?
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Share 3 mins – (TALKING)
Write a function sortedPrint(Node* node) that recursively prints the labels of all
nodes in the subtree rooted at node in ascending sorted order. Print a single
label per line.
You can assume that the Binary Search Tree structure is valid.

HINT: Does one of the rooted binary tree traversal algorithms (pre-order, in-
order, and post-order) seem useful in this context?
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Solution
Sample Output:
Apple
Cherry
Imbe
Lemon
Mango
Nectarine
Thank you.

You might also like