Unit 5 Trees
Unit 5 Trees
TREES
Abstract Data Type
Level
PARENT ROOT NODE 0
NODE
CHILD
PARENT
NODE
NODE Level
1
Level
2
CHILD
LEAF NODE
NODE
Definition of Tree
• A tree is a finite set of one or more nodes
such that:
• All other nodes of the tree will have in degree one and out
degree can be zero or more
• A node that has sub trees is the parent of the sub trees.
• The sub trees of the node are the children of the node.
• The ancestors of a node are all the nodes along the path
from the root to the node.
A Tree Node
O O O
O
Minimum Number Of Nodes
Applicable for
Full Binary tree
Perfect Binary tree
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Array representation.
Linked representation.
Array Representation
Number the nodes using the numbering scheme for
a full binary tree. The node that is numbered i is
stored in tree[i].
a1
2 3
b c
4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree
a1
b 3
7
c
15
d
tree[] a - b - - - c - - - - - - - d
0 5 10 15
Class BinaryTreeNode
{
<type of data> Object element;
BinaryTreeNode *leftChild; // left subtree
BinaryTreeNode *rightChild;// right subtree
// constructors and any other methods
// come here
};
Linked Representation Example
root a
b c
d e
g
f
Left Child
element h
rightChild
Binary Tree Traversal
PREorder:
visit the root
Traverse left subtree
Traverse right subtree
Tree Traversal
POSTorder
Traverse left subtree
Traverse right subtree
visit the root
Inorder
Traverse left subtree
visit the root
Traverse right subtree
Pre-Order Traversal
B E
C D F
A B C D E F
In-Order Traversal
B E
C D F
C B D A E F
Post-Order Traversal
B E
C D F
CDB F E A
Breadth first Traversal
B C
D E F G
A B C D E F G
Find Inorder, Preorder and Postorder traversal of given
example.
Output:
Inorder Output : D → B → E → A → F → C → G
Preorder Output : A → B → D → E → C → F → G
Postorder Output : D → E → B → F → G → C → A
Find Inorder, Preorder and Postorder traversal of given
example.
Output:
17
6 19
Is this Binary Search Tree
17
6 19
3 14
Is this Binary Search Tree
17
19
22
Is this Binary Search Tree
17
22 19
Is this Binary Search Tree
17
6 11
Is this Binary Search Tree
17
6 19
11 15
Is this Binary Search Tree
17
6 19
3 22
Insertion in BST
17
6 19
4 15
Add node - 2
Insertion in BST
17
6 19
4 15
2
Insertion in BST
17
6 19
4 15
Add node - 22
Insertion in BST
17
6 19
4 15
22
Insertion
parent 7
cursor 5 9
4 6 8 10
7
Removing 4replace the
link in the parent with
5 9
null
6 8 10
Removal in BST: Example
7 6
5 9 5 9
4 6 8 10 4 8 10
Removal in BST: Example
parent
parent
7 7
cursor
5 9 cursor 5 9
6 8 10 6 8 10
Removal in BST: Example
7 cursor 7
cursor
5 9 5 9
4 8 10 4 8 10
Threaded Binary Tree
does not have a right child has a THREAD (in actual sense, a link) to
its INORDER successor.
Node structure:
class Node
{
int value;
Node *left, *right;
boolean leftThread, rightThread;
}
Threaded Binary Tree Example
3 8
1 5 7 11
9 13
Threaded Tree Traversal
64
Output
6 1
3 8
1 5 7 11
9 13
3 8
1 5 7 11
9 13
Follow thread to right, print node
Threaded Tree Traversal
66
Output
6 1
3
5
3 8
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Threaded Tree Traversal
67
Output
6 1
3
5
3 8 6
1 5 7 11
9 13
Follow thread to right, print node
Threaded Tree Traversal
68
Output
6 1
3
5
3 8 6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Threaded Tree Traversal
69
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9 13
Follow thread to right, print node
Threaded Tree Traversal
70
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
9 13
Follow link to right, go to
leftmost node and print
8/8/02 Amir Kamil
Threaded Tree Traversal
71
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
Threaded Tree Traversal
72
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
11
9 13 13
1. 20, 10,30,5,16,14,17,15
2. F,B,G,A,D,I,C,E,H
Solution:
Applications of Tree