0% found this document useful (0 votes)
31 views

(Slides) 6 - Trees and Traversals

The document introduces tree data structures and tree traversal algorithms. It defines key tree terminology like nodes, edges, root, and leaf nodes. It also describes two common tree traversal algorithms: depth-first search and breadth-first search. Depth-first search prioritizes exploring nodes as deep as possible before backtracking, while breadth-first search explores all nodes at the current depth before moving to the next level. Both algorithms are demonstrated with examples of how they traverse a sample tree.

Uploaded by

Lisa Li
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

(Slides) 6 - Trees and Traversals

The document introduces tree data structures and tree traversal algorithms. It defines key tree terminology like nodes, edges, root, and leaf nodes. It also describes two common tree traversal algorithms: depth-first search and breadth-first search. Depth-first search prioritizes exploring nodes as deep as possible before backtracking, while breadth-first search explores all nodes at the current depth before moving to the next level. Both algorithms are demonstrated with examples of how they traverse a sample tree.

Uploaded by

Lisa Li
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Introduction to Data Structures

Topic 6 – Tree Structures and Traversals


Outline

▪ The Tree Data Structure (Chap. 6.1)


▪ Tree Traversals
▫ Depth-First Search
▫ Breadth-First Search

99
Xingcan
ITEC2620 O
Apr 2021
Tree Data Structure -- Example

Director

Manager Manager

Engineer Engineer Engineer Engineer Engineer

100
Xingcan
ITEC2620 O
Apr 2021
Tree Data Structure -- Introduction

▪ Linear to Non-Linear
1

1 2 3 4 5 6 7
2 3 4 Hierarchical

5 6 7

101
Xingcan
ITEC2620 O
Apr 2021
Tree Data Structure -- Terminologies

1 Node Edge Internal Node Root (unique) Depth (the length to root) = 1
Level = Depth + 1 = 2
1
1 Ancestor of 3

2 3 4
2 Parent of 3
Sibling of 3 Degree (# of children) = 2 Height (the longest downward
5 6
4 3 Child of 2 path to a leaf node) = 1
subtree

Children of 2 Leaf Node (External Node)


• Each node can have many ancestors/siblings/children, but only one parent node.
• The Degree of a tree is the max degree of a node in a tree. 102
Xingcan
ITEC2620 O
Apr 2021
Tree Data Structure – Some Questions

▪ If the number of nodes in a tree is 𝑛, what’s the number of edges? 𝑛 − 1


▪ If the degree of a tree is 𝑑, what’s the max number of nodes in the
level of depth 𝑚? 𝑑𝑚
▪ If the degree of a tree is 2 and the depth is 𝑚, what’s the max
number of nodes in it? 2𝑚+1 − 1
▫ How about the max number of leaf nodes? 2𝑚
▫ How about the internal nodes? 2𝑚 − 1

103
Xingcan
ITEC2620 O
Apr 2021
Tree Data Structure – An Implementation

public class ListNode { public class TreeNode {


int value; int data;
ListNode next; List<TreeNode> children;

public ListNode(int value) { public TreeNode(int data) {


this.value = value; this.data = data;
} children = new ArrayList<>();
} }
}

104
Xingcan
ITEC2620 O
Apr 2021
Tree Traversal

▪ Visit (check and/or update) each node in a tree exactly once.


▪ Recall the list traversal.

1 2 3 4 5 6 7

105
Xingcan
ITEC2620 O
Apr 2021
Tree Traversal

1 1

2 3 4 2 3 4

5 6 7 5 6 7

Depth-First Traverse/Search Breadth-First (Level Order) Traverse/Search

106
Xingcan
ITEC2620 O
Apr 2021
Depth-First Search – The Idea

▪ Starts at the root node and explores as far as possible along each branch
before backtracking.
1
▪ Backtracking:
▫ Search in branches and sub-branches
2 3 4
▫ Remember the visited branches
▫ Go to a previous location 5 6 7
▫ Search the next branch

Depth-First Search

107
Xingcan
ITEC2620 O
Apr 2021
Depth-First Search – The Implementation

▪ Recursion

2 3 4

5 6 7

Depth-First Search

108
Xingcan
ITEC2620 O
Apr 2021
Depth-First Search – The Implementation

▪ Stack
▫ Use a stack to “simulate” recursion
1
1 2 5 6

2 3 4
5

6 6 5 6 7
...
2 7 7 7

3 3 3 3 Depth-First Search

1 4 4 4 4
109
Xingcan
ITEC2620 O
Apr 2021
Breadth-First Search – The Idea

▪ Starts at the tree root and explores all of the sibling nodes at the present
depth prior to moving on to the nodes at the next depth level.
1

2 3 4

5 6 7

Breadth-First Search
(Level Order Search)
110
Xingcan
ITEC2620 O
Apr 2021
Breadth-First Search – The Implementation

▪ Use a queue to order the nodes to be traversed.

7 6 5 4 3 1
2
2 3 4

5 6 7

Breadth-First Search
(Level Order Search)
111
Xingcan
ITEC2620 O
Apr 2021
The Maze Problem

A Block Path

Try to find a path from A to B.


• Depth-first search a path.
• Go back to the previous intersection and go to the
next branch.

112
Xingcan
ITEC2620 O
Apr 2021

You might also like