Lecture 09 - Tree
Lecture 09 - Tree
Objectives
Idea for Tree
What is Tree?
Tree Terminology
Binary Tree
TreeTraversing & Examine various Tree
operations
Learn how to implement a Tree as a linked list
Discover Tree applications
Programming Task for Tree structure
February 2, 2025
+ 3
Recommended Readings
Page – 280: Tree - Chapter 8 - (Data Structure & Algorithm in Java
6th Edition) By GoodRich
Page – 365: Binary Tree - (Data Structure & Algorithm in Java 2nd
Edition) By Robert Lafore
February 2, 2025
+ 4
February 2, 2025
+ What is Tree? 5
February 2, 2025
+ What is Tree? 6
Except
node
the root node, every node
has a parent and zero or more
child nodes.
√
Each linear list is trivially a tree
X
Not a tree: cycle A→A
X X
Not a tree: two non-connected parts,
A→B and C→D→E
Not a tree: cycle B→C→E→D→B February 2, 2025
+ Student Slide (Put √ or X for Tree/non-Tree) 8
February 2, 2025
+ Student Slide (Put √ or X for Tree/non-Tree) 9
X X √
√ X X√ X
February 2, 2025
+ Tree Terminology 10
Root – The node at top of the Tree, there is only one root node in a Tree
Parent – Any node (except root), that has one edge out to another node
Child – Any node that have one edge in from the Parent node
Subtree
Leaf – A node with no children
February 2, 2025
+ Tree Terminology 11
February 2, 2025
+ Tree Terminology 12
February 2, 2025
Level
+ Students Slide 13
B C
D E F G
I
B is Parent of :
G is Child of :
Depth of H:
Internal Nodes:
Height:
Leaf: February 2, 2025
Level
+ Students Slide 14
A
0
B C
1
H
2
D E F G
I 3
B is Parent of : D, E
G is Child of : C
Size of Tree : 9
F is Sibling of: G, H
Depth of H: 2
Internal Nodes: 3
D, E, F, G, I Height: 3
Leaf: February 2, 2025
+ 15
Binary Tree
February 2, 2025
+ Binary Tree 16
February 2, 2025
+ Other Kind of Binary Tree 17
February 2, 2025
+ Binary Tree ADT 18
February 2, 2025
+ Binary Tree Implementation - Node Structure 19
Parent
(Address)
Left Right
(Addres Element (Address
s) (Store Data) )
February 2, 2025
+ Binary Tree Representation using Node 20
Parent
Left Right
Element
Left Parent
Right Parent
Element Left Right
Element
Parent
Left Right
Element Parent
Parent Left Right
Left Right
Element
Element Parent
Left Right
Element
February 2, 2025
+ Tree Operations: Traversing 21
February 2, 2025
+ Tree Operations: Depth First Traversal 23
Since a binary tree has three “parts”, there are six possible
ways to traverse the binary tree:
root, left, right root, right, left
left, root, right right, root, left
left, right, root right, left, root
February 2, 2025
+ Tree Operations: Depth First Traversal 24
February 2, 2025
+ Binary Tree Operations: Depth First – 25
Preorder
Preorder (root, left, right)
February 2, 2025
+ Binary Tree : Preorder Traversal 26
Method:
1. if (bt == null) return;
2. Display(bt.value);
3. PreOrderPrint(bt.leftChild);
4. PreOrderPrint(bt.rightChild)
InOrder
Inorder (left, root, right)
February 2, 2025
+ Binary Tree : InOrder Traversal Algorithm / 28
Java Code
Algorithm: InOrderPrint
Input: a binary tree node bt
Output: display the tree content in a InOrder style
Method:
1. if (bt == null) return;
2. InOrderPrint(bt.leftChild);
3. Display(bt.value);
4. InOrderPrint(bt.rightChild)
PostOrder
Postorder (left, right, root)
February 2, 2025
+ Binary Tree : PostOrder Traversal Algorithm 30
/ Java Code
Algorithm: PostOrderPrint
Input: a binary tree node bt
Output: display the tree content in a PostOrder style
Method:
1. if (bt == null) return;
2. PostOrderPrint(bt.leftChild);
3. PostOrderPrint(bt.rightChild)
4. Display(bt.value);
February 2, 2025
+ Students Slide 32
February 2, 2025
+ Students Slide inorder traversal 34
A/B*C*D+E
Arithmetic Expression Using BT infix expression
+ preorder traversal
+**/ABCDE
* E prefix expression
* D postorder traversal
AB/C*D*E+
postfix expression
/ C
February 2, 2025
+ Students Slide inorder traversal 35
A/B*C*D+E
Arithmetic Expression Using BT infix expression
+ preorder traversal
+**/ABCDE
* E prefix expression
* D postorder traversal
AB/C*D*E+
postfix expression
/ C
February 2, 2025
+ Type of Binary Tree: Binary Search Tree 36
(BST)
Binary Search Tree – A Binary Search Tree structure used
for searching. The following are some of the properties of BST
Every node is ordered by some key data field(s)
For every node in the tree, its key is greater than its left child’s
key and less than its right child’s key
The BST is not allow to store duplicate values.
February 2, 2025
+ BST Operations - Insertion 37
Adding a new node to Binary Tree can be divided into two stages:
search for a place to put a new element
insert the new element to that place
1. IF the new value == current node value THEN duplicate found and
exit
2. IF new value < current node's value, THEN go to the left subtree,
Following this simple rule, the algorithm reaches a node, which has no left or right subtree.
February 2, 2025
+ BST Operations - Insertion 38
February 2, 2025
+ BST Operations - Insertion 39
Insert 4 to the tree
February 2, 2025
+ BST Operations - Insertion 40
February 2, 2025
+ Student Slide – BST Insertion 41
Insert 19 to the tree
Insert -2 to the tree
Insert 6 to the tree
Insert 1 to the tree
Insert 18 to the tree
Insert 12 to the tree
Insert 3 to the tree
Insert 0 to the tree February 2, 2025
+ BST Operations - Insertion Algorithm: Insertion 42
Input: KeyVal
Output: Update Tree if Insertion
success
newNode = new Node(KeyVal, null, null,
null)
isEmpty(
Yes root = newNode, size=size + 1 Return
)
No
Current = root
Parent = Current
keyVal ==
Yes Display(“Duplicate found”); Return
current.value
No
No size = size + 1
February 2, 2025
+ BST Operations - Insertion 43
Algorithm: Insertion
Input: KeyVal
Output: Update Tree if Insertion success
Method:
loop
7. [End If]
February 2, 2025
+ BST Operations - Searching 45
February 2, 2025
+ Student Slide – BST Searching 46
findMin =
findMax =
Method:
1. IF (Node== null) THEN display(“Searching fail”); return null
2. IF (KeyVal == Node.value) THEN return Node
3. IF (KeyVal < Node.value) THEN return (Find(Node.left, keyVal))
ELSE return
(Find(Node.Right, keyVal))
February 2, 2025
+ BST Operations - Deletion 48
February 2, 2025
+ BST Operations - Deletion 49
February 2, 2025
+ BST Operations - Deletion 50
February 2, 2025
+ Student Slides – BST Deletion (redraw the Tree) 51
February 2, 2025
+ Tree Operations – BST Deletion (draw the Tree) 52
February 2, 2025
+ BST Operations - Deletion 53
Current.Left ==
null
AND Yes Parent = Current.Parent.
Current.Right ==
null
No Parent.value >
Yes Parent.Left=null
Current.value()
No
Case – II
Parent.Right = null
February 2, 2025
+ BST Operations - Deletion 54
Case – II
Current.Left == null
OR Yes Parent = Current.Parent
Current.Right ==
null
Parent.Value > Current.Lef
No Current.Value
Yes Parent.Left =
t != null Yes Current.Left
No
No
Case – III Parent.Left=Current.Right.
Current.Right !=
null
Yes Parent.Right = Current.Right
No
Parent.Right = Current.Left
February 2, 2025
+ BST Operations - Deletion 55
Case – III
Case 3: The node has 2 children
minNode =
findMin(Current.getRight())
Current.setValue(minNode.getValue
())
Delete(minNode,
minNode.getValue())
End
February 2, 2025
+ Tree Operations - Delete 56
Algorithm: Delete
Input: Node of a binary search tree, KeyVal
Output: return update Tree
Method:
Set Current = find(root, keyVal)
// Case-I: Leaf node
1. IF (Current.Left == null AND Current. Right == null) THEN
Set Parent = Current.Parent.
IF (Parent.value > Current.value) THEN Set Parent. Left =
null.
ELSE Set Parent.Right = null [ End IF ]
Section)
BST Node
Structure:
Parent
(Addres
(Address)
Right
(Address)
Left
s)
Value
(Store Data)
February 2, 2025
+ Tree Implementation in Java (Lab 59
Section)
February 2, 2025
+ Tree Implementation in Java (Lab 60
Section)
Create
Object
February 2, 2025
+ Tree Implementation in Java (Lab 61
Section)
The Outputs:
February 2, 2025
+ Tree Applications 62
1. File System
February 2, 2025
+ BST Tasks 64
February 2, 2025
+ BST Operations– Searching Minimum 65
value
public binaryNode findMin(binaryNode TreeNode){
while(TreeNode.getLeft() != null )
TreeNode = TreeNode.getLeft();
return TreeNode;
February 2, 2025
+ BST Operations– Searching Maximum 66
value
public binaryNode findMax(binaryNode TreeNode){
while(TreeNode.getRight() != null )
TreeNode = TreeNode.getRight();
return TreeNode;
February 2, 2025
+ 67
February 2, 2025