Unit 5 Trees & Its Operation
Unit 5 Trees & Its Operation
Introduction to Trees
1
Learning Outcomes
• To be able to
• Explain how data is represented in a tree
• Define various terminologies of a tree
2
Tree Data Structure
• Tree
• Nodes
• Each node can have 0 or more children
• A node can have at most one parent
• Binary tree
• Tree with 0–2 children per node
4
Reflection
• Specify
10 • 1. Root node
• 2. Leaf node
5 30
• 3. Ancestor node of 25
2 25 45 • 4. Descendant node of 30
• 5. Level number of 2
• 6. Level number of 25
5
Binary Tree
• each node has 0, 1, or at the most 2 children
• Key property
• Value at node
• Smaller values in left subtree
• Larger values in right subtree
9
2 45
6
Binary tree- Example
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
a. b. c.
7
Binary tree- Node representation
• struct node {
• struct node *left;
• int data;
• struct node *right;
• };
8
TRAVERSING A BINARY TREE
•Traversing a binary tree is the process of visiting each node in the tree exactly once in a systematic way.
•Unlike linear data structures in which the elements are traversed sequentially,
tree is a nonlinear data structure in which the elements can be traversed in many ways.
•There are different algorithms for tree traversals
1. Pre-order Traversal
2. In-order Traversal
3. Post-order Traversal
Pre-order traversal
•To traverse a non-empty binary tree in pre-order, the following operations are
performed recursively at each node. The algorithm works by:
•1. Visiting the root node,
•2. Traversing the left sub-tree, and finally
•3. Traversing the right sub-tree.
10
5 30
2 25 45
Example: Pre-order traversal
10
5 30
2 25 45
Algorithm for In-order traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: INORDER(TREE-> LEFT)
Step 3: Write TREE-> DATA
Step 4: INORDER(TREE-> RIGHT)
[END OF LOOP]
Step 5: END
Example: In-order traversal
10
5 30
2 25 45
Example: Post-order traversal
2 45
1 30 55
Example
•Construct BT from given data-
•Inorder
sequence: D B E A F C
Preorder sequence: A B D E C F
Binary Search Trees- Search
26
Search Operation
• Binary search tree (BST) stores keys in the nodes in a way so that searching, insertion and deletion
can be done efficiently.
Binary search tree property
• For every node X, all the keys in its left subtree are smaller than the key value in X, and all the
keys in its right subtree are larger than the key value in X
27
Search
Search 25
10
5 30
2 25 45
28
Search function
Search(struct node *tree, int val)
{ if(nodeptr==NULL)
struct node *ptr, *nodeptr, *parentptr; Print(“Data not found”);
if(tree==NULL) Else if(nodeptr->data==val)
Print(“tree empty”);} Print(“Data found”);
else }
{
nodeptr=tree;
while(nodeptr!=NULL && nodeptr->data!=val)
{
if(val<nodeptr–>data)
nodeptr=nodeptr–>left;
else
nodeptr = nodeptr–>right;
}
29
BST Insert
• Step 1: IF TREE = NULL
Allocate memory for TREE
SET TREE -> DATA = ITEM
SET TREE -> LEFT = TREE -> RIGHT = NULL
ELSE
IF ITEM < TREE -> DATA
Insert(TREE -> LEFT, ITEM)
ELSE
Insert(TREE -> RIGHT, ITEM)
[END OF IF]
[END OF IF]
• Step 2: END
30
Binary Search Trees-Delete
31
Delete operation
• The delete function deletes a node from the binary search tree.
• However, utmost care should be taken that the properties of the binary search tree are not violated and nodes
are not lost in the process.
32
Delete
Three cases:
(1) the node is a leaf
• Delete it immediately
10 10
5 30 Delete 2 5 30
2 25 45 25 45
33
Delete
Three cases:
(2) the node has one child
• Replace the node with its child
6 6
2 Delete 4 2 8
8
4 1 3 45
1 45
3
34
Delete
(3) the node has 2 children
• 1) replace the node with the minimum element at the right subtree (inorder
successor)
• 2) delete the minimum element by invoking case 1 or 2.
6 6
2 Delete 2 3 8
8
4 1 4 45
1 45
3
35
Delete
8
8
3 11 Delete 11
3 12
9 14
1 5 9 14
1 5
6 10 12 15 6 10 13 15
13
36
Delete Algorithm
p=tree; 8
q=null;
/*search for the node with the key val, set p to point
to the node and q to its parent, if any. */ 3 11
while (p!=null && p->data != val)
{ q=p;
p=(val < p->data ) ? p->left : p->right; 9 14
1 5
} //end while
if (p==null) { print (“val not found”); return;}
/* set the variable rp to the node that will replace node p.*/
/* first two cases: the node to be deleted has at most 6 10 12 15
one child node.*/
if (p->left ==null)
rp= p->right; 13
else if (p->right==null)
rp=p->left;
37
Delete Algorithm
else {
/* third case : node p has two child nodes.
Set rp to the inorder successor of p and f
8
to the father of rp*/
f=p;
rp= p->right; 3 11
s= rp->left;
while (s!=null)
{ f=rp; 9 14
1 5
rp= s;
s= rp->left;
} 6 10 12 15
/* rp is the inorder successor of p */
if(f!=p) {
/* p is not the parent of rp and rp==f->left */ 13
f->left = rp->right;
38
Delete Algorithm
/* remove node rp from its current position and replace it with the right son of
node rp*/ 8
rp-> right= p->right;
rp->left= p->left;
} 3 11
if (q==null)
9 14
tree= rp; 1 5
else
(p == q->left) ? q->left= rp : q->right = rp;
freenode (p); 6 10 12 15
return;
13
39
THANK YOU
40