Lecture 3 (Tree Data Structure (2) )
Lecture 3 (Tree Data Structure (2) )
Lecture 3
Tree Data Structure (2)
A. S. M. Sanwar Hosen
Email: sanwar@wsu.ac.kr
Date: 27 March, 2024
Tree Traversal
❑ Definition: Traversal is a process to visit all the nodes of a tree and may access their values.
The types of the traversals are:
✓ Depth-First Traversal/Search (DFS): A traversal or searching algorithm in data structures that explores all
the nodes by going forward if possible or uses backtrack. There are three different types of DFS:
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
✓ Breadth-First Traversal/Search (BFS): A traversal or searching algorithm in data structures that explores
all the nodes at a current level prior to moving on the next level. There is only one type of BFS:
1. Level-order Traversal
2
Depth-First Search (DFS) (1)
❑ Inorder Traversal: In this traversal method, the left subtree is visited first, then the root node
and later the right subtree. Every node may represent a subtree itself. The process goes on
until all the nodes are visited.
Tree Data Structure
root
0. Start from root A 2 The traverse of the right subtree
A likewise the left subtree
1. Move to its left subtree B 1
6
2. B is traversed in-order B C
2
5
3 4
1 D E F G 3
✓ Output: The set of the traversed nodes is: {D --> B --> E --> A --> F --> C --> G}
3
Depth-First Search (DFS) (2)
❑ Preorder Traversal: In this traversal method, the root/parent node is visited first, then left
subtree and finally the right subtree. Every node may represent a subtree itself. The process
goes on until all the nodes are visited.
D 4 E F G 3
2
5. Move to the node C of right subtree of root node A
4. Move to the node E of right subtree of node B
✓ Output: The set of the traversed nodes is: {A --> B --> D --> E --> C --> F --> G}
4
Depth-First Search (DFS) (3)
❑ Postorder Traversal: In this traversal method, the left subtree is visited first, then right subtree
and finally the root node. Every node may represent a subtree itself. The process goes on until
all the nodes are visited.
D 3 E F G
4. Move to the right subtree of root node A
3. Move to the node E of right subtree of node B
✓ Output: The set of the traversed nodes is: {D --> E --> B --> F --> G --> C --> A}
5
Breadth-First Search (BFS)
❑ Level-order Traversal: In this traversal method, all the nodes of a tree are visited, one level at a
time, from the root to the bottom level.
✓ Output: The set of the nodes in the tree traversed are {A --> B--> C --> D --> E --> F --> G}
6
Insertion in Binary Tree
❑ Insertion in a binary tree in level order: Inserts a node into the binary tree at the first position
available in level order.
An example: inserts a node E into the following binary tree.
Level 1 B C B C
Level 2 D F G D E F G
7
Deletion in Binary Tree (1)
❑ Deletion a node (a leaf node): Deletes a leaf node from a binary tree simply.
An example: deletes the leaf node(s) in the following binary tree.
B C B C
D E F G Delete the leaf nodes
8
Deletion in Binary Tree (2)
❑ Deletion a node (not a leaf node): Deletes a non leaf node from a binary tree. The process is
as follows:
✓ Starting at root, find the deepest and rightmost node in a binary tree and the node which we want to
delete.
✓ Replace the deepest rightmost node’s data to be deleted one.
✓ Then delete the deepest rightmost node.
An example: deletes the node B in the following binary tree.
Step 1 root Step 2 root Step 3 root
A A A
B C G G
C C
D E F G D E F D E F
G
A node to be deleted is B Replacing node B’s data with Delete the deepest
the data of deepest node G node G
9
Insertion in Binary Search Tree (BST)
❑ Insertion in a BST: Inserts a node always into the leaf in a BST with satisfying the condition.
An example: inserts a node 15 into the following BST.
10 40 10 40
5 20 35 50 5 20 35 50
15
Find the position of the leaf node
where the new node to be Insert the node into the leaf
inserted node
10
Deletion in Binary Search Tree (BST) (1)
❑ Deletion in BST: Deletes the specified node from a BST. The process is varied on the different
types of deletions of the nodes.
✓ Case I (delete a leaf node): Deletes the leaf node from the tree simply.
An example: deletes a leaf node 20 from the following BST.
root root
30 30
10 40 10 40
5 20 35 50 5 35 50
11
Deletion in Binary Search Tree (BST) (2)
✓ Case II (delete a node with a single child): The node to be deleted lies has a single childe node. The
process is as follows:
1. Replace that node with its child node.
2. Remove the child node from its original position.
An example: deletes a node has a single child from the following BST.
10 40 10 50
5 20 50 5 20
Find the node(s) has single child, Delete the node 40, replace it by the child
node 40 is to be deleted node 50, and remove the child node
12
Deletion in Binary Search Tree (BST) (3)
✓ Case III (delete a node with two children): The node to be deleted has two children. The process is as
follows:
1. Find the in-order successor of that node.
2. Replace the node with the in-order successor and satisfies either of the following two conditions.
- minimum element of right subtree
- maximum element of left subtree
3. Remove the in-order successor from its original position.
An example: delete a node has two children from the following BST.
Step 1 root
Step 2 root
30
20
10 40
5 35
5 20 35 50
50
Find the nodes to be deleted (30, Replace the nodes by its in-order
13
10 and 40 are to be deleted) successors, and remove the successors
Binary Search Tree Construction in Python (1)
❑ Insert elements in a Binary Search Tree (BST)
14
# In an inorder traversal, the Binary Search
Binary Search Tree Traversal in Python (2) Tree is traversed as follows:
1. Traverse the left subtree
2. Visit the parent/root node
❑ Inorder Traversal 3. Traverse the right subtree
15
# In a Preorder Traversal, the Binary Search
Tree is traversed as follows:
Binary Search Tree Traversal in Python (3) 1. Visit the root node
2. Traverse the left subtree
❑ Preorder Traversal 3. Traverse the right subtree
16
# In a Postorder Traversal, the Binary
Search Tree is traversed as follows:
Binary Search Tree Traversal in Python (4) 1. Traverse the left subtree
2. Traverse the right subtree
❑ Postorder Traversal 3. Visit the root node
17