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

Unit 5 Trees & Its Operation

Uploaded by

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

Unit 5 Trees & Its Operation

Uploaded by

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

Unit 5- Trees

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

Tree Binary Tree


3
Trees- Terminology
• Root node- Top node
• Sub-trees- the tree below root node
• Leaf node- the node with no child node
• Path- the sequence of consecutive edges
• Ancestor node- an ancestor of a node is any
predecessor node on the path from root to that node.
• Descendant node A descendant node is any successor
node on any path from the node to a leaf node.
• Level number-root node is at level 0, children of the
root node are at level number 1.

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.

The pre-order traversal of the tree is given as A, B, C.

NLR traversal algorithm (Node-Left-Right)


Algorithm for pre-order traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE-> DATA
Step 3: PREORDER(TREE-> LEFT)
Step 4: PREORDER(TREE-> RIGHT)
[END OF LOOP]
Step 5: END
Pre-order Traversal

10

5 30

2 25 45
Example: Pre-order traversal

•TRAVERSAL ORDER: A, B, D, G, H, L, E, C, F, I, J,and K


•TRAVERSAL ORDER: A, B, D, C, D, E, F, G, H, and I
Pre-order Function in C
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
struct Node *root = NULL;

void preorder(struct Node *root)


{
if(root != NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}
In-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. Traversing the left sub-tree,
•2. Visiting the root node,
•3. Traversing the right sub-tree.

The in-order traversal of the tree is given as B, A, C.

LNR traversal algorithm (Left-Node-Right)


In-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

•TRAVERSAL ORDER: G, D, H, L, B, E, A, C, I, F, K, and J


•TRAVERSAL ORDER: B, D, A, E, H, G, I, F, and C
In-order Function in C
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
struct Node *root = NULL;

void inorder(struct Node *root)


{
if(root != NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
Post-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. Traversing the left sub-tree,
•2. Traversing the right sub-tree.
•3. Visiting the root node,

The in-order traversal of the tree is given as B, C,A.

LRN traversal algorithm (Left-Right-Node)


Algorithm for post-order traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: POSTORDER(TREE-> LEFT)
Step 3: POSTORDER(TREE-> RIGHT)
Step 4: Write TREE-> DATA
[END OF LOOP]
Step 5: END
Post-order Traversal

10

5 30

2 25 45
Example: Post-order traversal

•TRAVERSAL ORDER: G, L, H, D, E, B, I, K, J, F, C, and A


•TRAVERSAL ORDER: D, B, H, I, G, F, E, C, and A
Perform in-order, pre-order and 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

You might also like