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

Tree

The document provides an overview of tree data structures, defining key concepts such as parent, child, root, leaf nodes, and various types of binary trees. It explains properties of binary trees, their representation in memory, and traversal methods including pre-order, in-order, and post-order. Additionally, it discusses the construction of general trees to binary trees and the algorithms for tree traversal, both recursively and non-recursively.

Uploaded by

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

Tree

The document provides an overview of tree data structures, defining key concepts such as parent, child, root, leaf nodes, and various types of binary trees. It explains properties of binary trees, their representation in memory, and traversal methods including pre-order, in-order, and post-order. Additionally, it discusses the construction of general trees to binary trees and the algorithms for tree traversal, both recursively and non-recursively.

Uploaded by

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

Data Structures

Tree

Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: [email protected]
Non-linear Data Structures
A data structure is said to be non-linear if its elements form a hierarchical
relationship where data items appear at various levels.
Trees and Graphs are widely used non-linear data structures. Tree and
graph structures represent hierarchical relationships between individual
data elements.
Trees can be defined recursively as:
A tree is a finite set of nodes such that:
1. There is a distinguished node called root node
2. The remaining nodes are partitioned into n>=0 disjoint sets T1, T2 ,…,
Tn where each of these sets is a tree. The sets T1, T2,…,Tn are the
subtrees of the root.
Basic Terminologies in Tree Data Structure:
Parent Node: The node which is a predecessor of a node is called the parent node of that
node. {B} is the parent node of {D, E}.
Child Node: The node that is the immediate successor of a node is called the child node of
that node. Examples: {D, E} are the child nodes of {B}.
Root Node: The topmost node of a tree or the node that does not have any parent node is
called the root node. {A} is the root node of the tree. A non-empty tree must contain
exactly one root node and exactly one path from the root to all other nodes of the tree.
Leaf Node or External Node: The nodes that do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P, G} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
Descendant: A node x is a descendant of another node y if and only if y is an ancestor of y.
Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
Level of a node: The count of edges on the path from the root node to that node. The root
node has level 0.
Internal node: A node with at least one child is called an Internal Node.
Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
Subtree: Any node of the tree along with its descendant.
Binary Tree
A binary tree is a finite set of nodes that is:
1. Either empty or
2. Consists of a distinguished node called root and the remaining nodes
are partitioned into two disjoint sets T1 (called left subtree) and T2 (called
right subtree) and both of them are binary trees.
Property 1: The maximum number of nodes on any level i is 2i where i ≥0.

Property 2: The maximum number of nodes possible in a binary tree of


height h is 2h-1.

Proof:
If we sum up the maximum number of nodes possible on each level then we
can get the maximum number of nodes possible in the binary tree. First level
is 0 and last level is h-1. By using property 1, the total number of nodes
possible in a binary tree of height h is given by:
h −1
n =  2i
i =0

n = 1 + 21 + 22 + ... + 2h −1
2( h −1) +1 − 1
n=
2 −1
n = 2h − 1
Property 3: The minimum number of nodes possible in a binary tree of
height h is equal to h.

Property 4: If a binary tree contains n nodes, then its maximum height


possible is n and the minimum height possible is log 2 (n + 1)  .
Proof: There should be at least one element on each level, so the height
cannot be more than n. A binary tree of height h can have a maximum 2h-1
nodes (from property 2). So, the number of nodes will be less than or equal
to this maximum value.
n  2h − 1
2h  n + 1
h  log 2 ( n + 1)
h  log 2 (n + 1)  (h is an integer)
So the minimum height possible is log 2 (n + 1)  .
Property 5: In a non-empty binary tree, if n is the total number of nodes and
e is the total number of edges then e=n-1.

Property 6: For any non-empty binary tree, if n0 is the number of nodes


with no child and n2 is the number of nodes with 2 children, then n0 = n2+1.

Total Number of binary tree is generated by using n nodes (without


label nodes) : = 2nCn/(n+1)

Total Number of binary tree is generated by using n nodes (labelled


nodes) : = 2nCn/(n+1)*n!

Total Number of binary tree of maximum height generated by using n


nodes: = 2n-1
Types of Binary Tree
1. Strictly Binary Tree
A Binary Tree is a strictly binary tree if each node in the tree is either a leaf
node or has exactly two children i.e. there is no node with one child.

Property 7: A strictly binary tree with n non-leaf nodes has n+1 leaf nodes.
Property 8: A strictly binary tree with n leaf nodes always has 2n-1 nodes.
Remark: Number of Leaf nodes = Number of Internal nodes + 1
2. Extended Binary Tree
If in a binary tree, each empty subtree (NULL link) is replaced by a special
node then the resulting tree is an extended binary tree or 2-tree.
So we can convert a binary tree to an extended binary tree by adding special
nodes to leaf nodes and nodes that have only one child.
The special nodes added to the tree are called external nodes and the
original nodes of the tree are internal nodes.

Internal nodes

External nodes
The path length for any node is the number of edges traversed from that
node to the root node.
The path length of a tree is the sum of the path lengths of all the nodes of
the tree.
The internal path length of a binary tree is the
sum of the path lengths of all internal nodes
which is equal to the sum of levels of all internal
nodes.
Internal path length (I): 0+1+1+2+2+3 = 9
The external path length of a binary tree is the
sum of the path lengths of all external nodes
which is equal to the sum of levels of all
external nodes.
External path length (E): 2+2+3+3+3+4+4 = 21

Property 9: In an extended binary tree, if E is the external path length, I is the


internal path length and n is the number of internal nodes then E = I+2n.
3. Full Binary Tree
A binary tree is a full binary tree if all the levels have a maximum number
of nodes, i.e. if the height of the tree is h, then it will have 2h-1 nodes.

Full Binary Tree


4. Complete Binary Tree
A complete binary tree is a binary
tree in which every level, except
possibly the last, has to be filled and
all nodes are as far left as possible.

Complete Binary Tree

Property 10: If the height of a complete binary tree is h, h ≥1, then the
minimum number of nodes possible is 2h-1 and the maximum number of nodes
possible is 2h -1.
Proof: The number of nodes will be maximum when the last level also
contains maximum nodes i.e. all levels are full, and so total nodes will be 2h -1
from property 2.
The number of nodes will be minimal when the last level has only one node. In
this case the total nodes will be
Total nodes in a full binary tree of height (h-1) + one node
i.e. (2h-1 -1)+1 = 2h-1.
5. Balanced Binary Tree
A balanced binary tree is a binary tree
in which the height of the left and the
right sub-trees of every node may
differ by at most 1.
Remark: AVL Tree and Red-Black Tree are
well-known data structures.

6. Degenerate Binary Tree


Degenerate Binary Tree is a Binary
Tree where every parent node has
only one child node.
Remark: The height of a Degenerate Binary
Tree is equal to the total number of nodes in
that tree.
Representation of Binary Trees in Memory
There are two common methods used for representing a binary tree
1. Sequential representation using Array
2. Linked representation using Linked List
1. Sequential representation using Array
A one-dimensional array can be used
to represent a binary tree in the
memory. Here, we consider that the
root is present at index 0. Assume
tree[N] is an array representing a
binary tree. If ‘i’ be index, then

1. The root node is stored at index 0, i.e. root is at tree[0].


2. Left child is stored at tree[2*i+1].
3. Right child is stored at tree[2*i+2].  (i − 1) 
4. Its parent will be at index floor((i-1)/2) i.e.  2  .
Drawbacks of Array Representation: While representing a binary tree in the form of an
array, most of the memory blocks in the middle will be left blank or unused if the tree is
not a complete binary tree.
2. Linked representation using Linked List
A binary tree can be represented by using a linked list, where each node is
divided into 3 parts:
• Info: it is used to store the information
• Left: It is a pointer that holds the address of the left child
• Right: It is a pointer that holds the address of the right child

The structure for tree node can be declared as


struct node{
char data;
struct node *lchild;
struct node *rchild;
};
Construction of General Tree to Binary Tree
It is convenient to represent a general tree to binary tree in a program,
because in the case of a general tree, the number of edges connected from a
node at any given time is unpredictable.
Therefore the node space has to be managed in such a way that each node is
allowed a variable number of sub-tree pointers.

The procedure is:


• Insert edges connecting siblings from left to right at the same level.
• Delete all edges of a parent to its children except to its left most
offspring.
• Rotate the resultant tree 45o to mark clearly the left and right sub-trees.
Traversal of Binary Tree

The process of visiting each node of the tree exactly once is called tree
traversal. The traversal of the binary tree involves three basic activities such
as:
1. Visiting the root
2. Traverse the left sub-tree
3. Traverse the right sub-tree
These three basic activities are done in a different order which follows:
1. Pre-order Traversal (NLR)
a. Visit the root (N)
b. Traverse the left subtree of root in
preorder(L)
c. Traverse the right subtree of root in
preorder(R)

The Pre-order traversal is: ABDECFG


2. In-order Traversal (LNR)
a. Traverse the left subtree of root in
preorder(L)
b. Visit the root (N)
c. Traverse the right subtree of root in
preorder(R)

The In-order traversal is: DBEAFCG

3. Post-order Traversal (LRN)


a. Traverse the left subtree of root in
preorder(L)
b. Traverse the right subtree of root in
preorder(R)
c. Visit the root (N)

The In-order traversal is: DEBFGCA


The above tree traversal methods can be implemented in two ways:
1. Recursive tree traversal
2. Non-Recursive tree traversal
1. Recursive tree traversal
void preorder(struct node * ptr) void inorder(struct node * ptr)
{ {
if(ptr = = NULL) if(ptr = = NULL)
return; return;
printf(“%d”, ptr->info); inorder(ptr->lchild);
preorder(ptr->lchild); printf(“%d”, ptr->info);
preorder(ptr->rchild); inorder(ptr->rchild);
} }

void postorder(struct node * ptr)


{
if(ptr = = NULL)
return;
postorder(ptr->lchild);
postorder(ptr->rchild);
printf(“%d”, ptr->info);
}
2. Non-Recursive tree traversal
void nrec_preorder(struct node *root)
Pre-order Traversal {
1. Push the root node on the stack struct node *ptr = root;
2. Pop a node from the stack if(ptr = = NULL)
3. Visit the popped node {
4. Push the right child of the visited printf(“Tree is empty”);
node on the stack return;
5. Push the left child of the visited }
node on the stack push_stack(ptr);
6. Repeat steps 2,3,4,5 till the stack while(!stack_empty())
is not empty. {
ptr = pop_stack();
printf(“%d”, ptr->info);
if(ptr->rchild!=NULL)
push_stack(ptr->rchild);
if(ptr->lchild!=NULL)
push_stack(ptr->lchild);
}
printf(“\n”);
}
If we apply the algorithm to the tree, the steps would be:
• Push 50
• Pop 50: Visit 50
Push right and left child of 50: Push 60. Push 40
• Pop 40: Visit 40
40 has no right child so push its left child: Push 30
• Pop 30: Visit 30
Push right and left child of 30: Push 35. Push 25
• Pop 25: Visit 25
25 has no right child so push its left child: Push 20
• Pop 20: Visit 20
20 has no child so nothing is pushed
• Pop 35: Visit 35
Push right and left child of 35: Push 36. Push 33
• Pop 33: Visit 33
33 has no child so nothing is pushed
• Pop 36: Visit 36
36 has no child so nothing is pushed
• Pop 60: Visit 60
60 has no left child so push its right child: Push 70
• Pop 70: Visit 70
Push right and left child of 70: Push 80. Push 65 Hence pre-order traversal is:
• Pop 65: Visit 65
65 has no child so nothing is pushed 50 40 30 25 20 35 33 36 60 70 65 80
• Pop 80: Visit 80
80 has no child so nothing is pushed Stack is empty
In-order Traversal void nrec_inorder(struct node *root)
1. Initially ptr is assigned the address {
of the root node. struct node *ptr = root;
2. Move along the leftmost path if(ptr = = NULL)
{
rooted at the node pointed by ptr,
printf(“Tree is empty”);
pushing all the nodes in the path return;
on the stack. Stop when we reach }
the leftmost node i.e. a node that while(1)
has no left child, it is not pushed {
on the stack. Now ptr points to this while(ptr->lchild!=NULL)
leftmost node. {
3. If the node pointed by ptr has no push_stack(ptr);
ptr = ptr->lchild;
right subtree, then visit it and pop
}
another one from the stack. Now while(ptr->rchild==NULL)
keep on popping and visiting the {
nodes till anode is popped that has printf(“%d”, ptr->info);
a right subtree. Now ptr points to if(stack_empty())
this node that has a right subtree. return;
If the stack becomes empty then ptr = pop_stack();
the traversal is finished. }
printf(“%d”, ptr->info);
4. Visit the node pointed by ptr, and
ptr = ptr->rchild;
now ptr is assigned the address of }
its right child. printf(“\n”);
5. Go to step 2 }
Post-order Traversal void nrec_postorder(struct node *root)
1. Initially ptr is assigned the address of {
struct node *q, *ptr = root;
the root node.
if(ptr = = NULL)
2. Move along the leftmost path rooted at {
the node ptr pushing all the nodes in printf(“Tree is empty”);
the path on the stack. Stop when we return;
reach the leftmost node i.e. a node that }
has no left child, it is not pushed on the q = root;
stack. Now ptr points to this leftmost while(1)
node. {
while(ptr->lchild!=NULL)
3. If ptr has no roght subtree or ots right
{
subtree has been traversed then visit push_stack(ptr);
the node and pop another one from the ptr = ptr->lchild;
stack. Now keep on popping and }
visiting the nodes till a node is popped while(ptr->rchild==NULL || ptr->rchild ==q)
that has a right subtree which has not {
been traversed. Now ptr points to this printf(“%d”, ptr->info);
node that has an untraversed right q = ptr;
if(stack_empty())
subtree. If the stack becomes empty
return;
then the traversal is finished. ptr = pop_stack();
4. Push the node pointed by ptr, and now }
ptr is assigned the address of its right push_stack(ptr);
child. ptr = ptr->rchild;
5. Go to step 2 }
Level-order Traversal
1. Insert the root node in the queue
2. Delete a node from the front of the queue and visit it
3. Insert the left child of the visited node in the queue at the end
4. Insert the right child of the visited node in the queue at the end
5. Repeat steps 2,3,4 till the queue is not empty.

The level-order traversal is ABCDEFG.


Creation of Binary Tree from In-order and Pre-order traversals

The Pre-order traversal is: A B D H E C F I G J K


The In-order traversal is: D H B E A I F C J G K
In pre-order traversal, the first node is the root node.
Hence, A is the root of the binary tree.
From In-order traversal, we see that nodes to the left of
root node A are nodes D, H, B, E so these nodes form the
left subtree of A, similarly nodes I, F, C, J, G, K form the
right subtree of A.

D H B E I F C J G K
Pre-order : B D H E Pre-order : C F I G J K
In-order : D H B E In-order : I F C J G K

left subtree of A right subtree of A


A

B C

D H E I F J G K
Pre-order : D H Pre-order : F I Pre-order : G J K
In-order : D H In-order : I F In-order : J G K
left subtree of B left subtree of C right subtree of C

B C

D E F G

H I J K
Creation of Binary Tree from In-order and Post-order traversals

The Post-order traversal is: H D E B I F J K G C A


The In-order traversal is: D H B E A I F C J G K
In post-order traversal, the last node is the root node.
Hence, A is the root of the binary tree.
From In-order traversal, we see that nodes to the left of
root node A are nodes D, H, B, E so these nodes form the
left subtree of A, similarly nodes I, F, C, J, G, K form the
right subtree of A.

D H B E I F C J G K
Post-order : H D E B Post-order : I F J K G C
In-order : D H B E In-order : I F C J G K

left subtree of A right subtree of A


A

B C

D H E I F J G K
Post-order : H D Post-order : I F Post-order : J K G
In-order : D H In-order : I F In-order : J G K
left subtree of B left subtree of C right subtree of C

B C

D E F G

H I J K
Creation of Binary Tree from Pre-order and Post-order traversals

The Pre-order traversal is: A B D E H I C F G


The Post-order traversal is: D H I E B F G C A
In first node of pre-order and last node of post-order
traversal i.e. A is considered as root of the binary tree.
Find the node succussing the root node in pre-order, say x1
and the node preceding the root node in post-order say x2.
• If x1 = x2, then it can be taken as left or right child of root and which may not result
unique tree.
• If x1 != x2, then x1 is taken as left child i.e. B and x2 is taken as right child i.e. C of
the root node.
Find the position of x2, i.e. ‘C’ in pre-order and position of x1, i.e. ‘B’ in the post order.
• Consider two sets of pre-order and post-order traversal of left and right sub-tree of root.
• The first set consists of nodes that are present after x1 and before x2 in pre-order
traversal i.e. DEHI and the nodes present before x1 in post-order i.e. DHIE.
• The second set consists of nodes that are present after x2 in pre-order traversal i.e. FG
and the nodes present after x1 and before x2 in post-order traversal i.e. FG.
A

B = x1 B C C = x2

D H E I G F
Pre-order : D E H I Pre-order : F G
Post-order : D H I E Post-order : F G

Find the node succussing the root node in pre-order, say x1 (i.e. D) and the node
preceding the root node in post-order say x2 (i.e. E).
• If x1 != x2, then x1 is taken as left child i.e. D and x2 is taken as right child i.e. E of
the root node.
Find the position of x2, i.e. ‘E’ in pre-order and position of x1, i.e. ‘D’ in the post order.
• Consider two sets of pre-order and post-order traversal of left and right sub-tree of root.
• The first set consists of nodes that are present after x1 and before x2 in pre-order
traversal i.e. NULL and the nodes present before x1 in post-order i.e. NULL.
• The second set consists of nodes that are present after x2 in pre-order traversal i.e. HI
and the nodes present after x1 and before x2 in post-order traversal i.e. HI.
A

B C
G F
D = x1 D E E = x2 Pre-order : F G
Post-order : F G
H I
Pre-order : HI
Post-order : HI

Follow the same procedure to get the final binary tree.


Height of Binary Tree
int height(struct node *ptr)
{
int h_left, h_right;
if(ptr = = NULL)
return 0;
h_left = height(ptr->lchild);
h_right = height(ptr->rchild);
if(h_left > h_right)
return 1+h_left ;
else
return 1+h_right;
}
Expression Tree
It is a binary tree, which stores an arithmetic expression. The leaf nodes of the
expression tree are always operands and all other internal nodes are the operators,
including the root.

arithmetic expression: 4 / ( 2 - ( - 8 * 3 ) )
Construction of Expression Tree:

First of all, we will do scanning of the given expression into left to the right
manner, then one by one check the identified character:
1. If a scanned character is an operand, we will apply the push operation and push it
into the stack.
2. If a scanned character is an operator, we will apply the pop operation into it to
remove the two values from the stack to make them its child, and after then we will
push back the current parent node into the stack.

postfix notation is: a b + c d e + * *


Binary Search Tree

A binary search tree is a binary tree that may be


empty and if it is not empty then it satisfies the
following properties:
1. All the keys in the left subtree of root are less
than the key in the root
2. All the keys in the right subtree of root are greater
than the key in the root
3. Left and right subtrees of root are also binary
search trees.
Traversal in Binary Search Tree

The different traversals for the binary search


tree are:
Pre-order: 30, 20, 10, 15, 25, 23, 39, 35, 42

In-order: 10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42

Post-order: 15 , 10 , 23 , 25, 20, 35, 42, 39, 30

Level order: 30, 20, 39, 10, 25, 35, 42, 15, 23

Remark: Note that the in-order traversal of a binary search tree gives us all keys of
that tree in ascending order.
Searching in a Binary Search Tree

1. Compare the element with the root of the tree.


2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on root, if so then
move to the left sub-tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.
Non-recursive code for searching a key in Binary Search Tree

struct node *search_nrec(struct node *ptr, int skey)


{
while(ptr!=NULL)
{
if(skey < ptr->info)
ptr = ptr->lchild; /* move to left child*/
else if(skey > ptr->info)
ptr = ptr->rchild; /* move to right child*/
else /*skey found*/
return ptr;
}
return NULL;
}
Recursive code for searching a key in Binary Search Tree
struct node *search_rec(struct node *ptr, int skey)
{
if(ptr==NULL)
{
printf(“key not found\n”);
return NULL;
}
else if(skey < ptr->info) /* search in left subtree*/
return search_rec(ptr->lchild, skey);
else if(skey > ptr->info) /* search in right subtree*/
return seach_rec(ptr->rchild, skey);
else /*skey found*/
return ptr;
}
Insertion in a Binary Search Tree

1. We start at the root and move down the tree.


2. If the key to be inserted is equal to the key in the node then there is nothing
to be done as duplicate keys are not allowed.
3. If the key to be inserted is less than the key of the node then we move to
left child.
4. If the key to be inserted is greater than the key of the node then we move
to right child.
5.We insert the new key when we reach a NULL left or right child.
6. END
Prepared by: Dr Deepak Gupta, CSED, MNNIT Allahabad, India

Example of creating a binary search tree


Now, let's see the creation of binary search tree using an example.

Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

Step 1 - Insert 45.

Step 2 - Insert 15.


As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 - Insert 79.


As 79 is greater than 45, so insert it as the root node of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.

Step 5 - Insert 10.


10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.
Step 6 - Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.

Step 7 - Insert 12.


12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree of 10.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.

Step 9 - Insert 50.


50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of 55.
Struct node *insert_nrec(struct node *root, int ikey)
{
struct node *tmp, *par, *ptr; /*par: parent*/
ptr = root;
par = NULL;
while(ptr!=NULL)
{
par = ptr;
if(ikey < ptr->info)
ptr = ptr->lchild;
else if(ikey > ptr->info)
ptr = ptr->rchild;
else
{
printf(“Duplicate key”);
return root;
}
}
tmp = (struct node *)malloc(sizeof(struct node));
tmp->info = ikey;
tmp->lchild = NULL;
tmp->rchild = NULL;
if(par = = NULL)
root = tmp;
else if(ikey < par->info)
par->lchild = tmp;
else
par->rchild = tmp;
return root;
}
Struct node *insert_rec(struct node *ptr, int ikey)
{
if(ptr = =NULL)
{
ptr = (struct node *)malloc(sizeof(struct node));
ptr->info = ikey;
ptr->lchild = NULL;
ptr->rchild = NULL;
}
else if(ikey < ptr->info)
ptr->lchild = insert_rec(ptr->lchild, ikey);
else if(ikey > ptr->info)
ptr->rchild = insert_rec(ptr->rchild, ikey);
else
printf(“Duplicate key”);
return ptr;
}
Deletion in a Binary Search Tree

Given a BST, the task is to delete a node in this BST, which can be broken
down into 3 cases:
Case 1. Delete a Leaf Node in BST
Case 2. Delete a Node with Single Child in BST
Deleting a single child node is also simple in BST. Copy the child to the node and
delete the node.

Case 3. Delete a Node with Both Children in BST


Here we have to find the inorder successor of the node. The data of the inorder
successor is copied to the node and then the inorder successor is deleted from the tree.
struct node *del_rec(struct node *ptr, int dkey)
{
struct node *tmp, *succ;
if(ptr = =NULL)
{
printf(“dkey not found”);
return(ptr);
}
if(dkey < ptr->info) /* delete from left subtree*/
ptr->lchild = del_rec(ptr->lchild, dkey);
else if(dkey > ptr->info) /* delete from right subtree*/
ptr->rchild = del_rec(ptr->rchild, dkey);
else
{ /* key to be deleted is found*/
if(ptr->child!=NULL && ptr->rchild!=NULL) /* 2 children*/
{
succ = ptr->rchild;
while(succ->lchild)
succ = succ->lchild;
ptr->info = succ->info;
ptr->rchild = del_rec(ptr->rchild, succ->info);
}
else
{
temp = ptr;
if(ptr->lchild!=NULL) /* only left child*/
ptr = ptr->lchild;
else if(ptr->rchild!=NULL) /* only right child*/
ptr = ptr->rchild;
else
ptr = NULL;
free(tmp);
}
}
return ptr;
}

You might also like