0% found this document useful (0 votes)
50 views21 pages

DS Module-4 Notes

The document discusses different types of binary trees including their definitions, properties, representations and traversal methods. It covers topics like full binary trees, complete binary trees, extended binary trees, strictly binary trees and almost complete binary trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views21 pages

DS Module-4 Notes

The document discusses different types of binary trees including their definitions, properties, representations and traversal methods. It covers topics like full binary trees, complete binary trees, extended binary trees, strictly binary trees and almost complete binary trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

18CS32 DSA Notes

Module-4

Trees: Terminology, Binary Trees, Properties of Binary trees, Array and linked Representation of Binary
Trees, Binary Tree Traversals - Inorder, postorder, preorder; Additional Binary tree operations. Threaded
binary trees, Binary Search Trees – Definition, Insertion, Deletion, Traversal, Searching, Application of
Trees-Evaluation of Expression, Programming Examples

Introduction

Definition of Tree:
A tree is a finite set of one or more nodes such that
 There is a special node called the root.
 The remaining nodes are partitioned into n ≥ 0 disjoint sets T1, ⋯, Tn, where each of these
sets are called the subtrees of the root.

Example: Consider the tree given below

Figure 4.1 A sample Tree

The tree has 13 nodes and has one character as its information. A tree is always drawn with its root
at the top. Here the node A is the root.

 Degree of a node: The number of subtrees of a node is called its degree.


o Example : The degree of A = 3, C = 1, and of F = 0.
 Degree of a Tree :The degree of a tree is the maximum degree of the nodes in the tree. The
tree shown in the example has degree 3.
 Leaf or terminal nodes or external nodes: Nodes that have degree zero is called the leaf
nodes
o Example : {K,L,F,G,M,I,J} is the set of leaf nodes
 Non-terminal nodes/internal nodes : Nodes that have at least a degree one or two. (nodes
other than the leaf nodes )
o Example : {B,C,D,E,F,H,A} is the set of Non-terminal nodes
 Siblings : Children of the same parent are said to be siblings
o Example : {H,I,J} are siblings.
 The ancestors: all the nodes along the path from the root to that node.
o Example : The ancestors of M are A, D, and H.
 The level of a node is .The root is considered be at level one[1]. If a node is at level l, then
its children are at level l + 1.
 The height or depth is maximum level of any node in the tree. Thus, the depth of the tree in
the example is 4.

Dept. of ISE, SVIT Page 1


18CS32 DSA Notes

Binary Trees
The Abstract Data type

Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two
disjoint binary trees called the left subtree and the right subtree.

Objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right
Binary_Tree.

Functions: for all bt,bt1,bt2 ∈ BinTree, item ∈ element

BinTree Create() ::= creates an empty binary tree


Boolean IsEmpty(bt) ::= if (bt == empty binary tree)
return TRUE else return FALSE
BinTree MakeBT(bt1, item,bt2) return a binary tree whose left
::= subtree is bt1, whose right
subtree is bt2, and whose root
node contains the data item.
BinTree Lchild(bt) ::= if (IsEmpty(bt)) return error else
return the left subtree of bt.
Element Data(bt) ::= if (IsEmpty(bt)) return error else
return the data in the root node ofbt.
BinTree Rchild(bt) ::= if (IsEmpty(bt)) return error else
return the right subtree of bt

Difference between binary tree and tree.

Binary tree Tree


Empty tree exist No tree exist with zero nodes
Order of the child is considered Order of the child is not considered
Each node can be partitioned to only two Each node can be partitioned to T1,T2…..Tn
disjoint subtrees disjoint subtrees

Properties of Binary Tree

Lemma 1: [Maximum number of nodes]


.
1. The maximum number of nodes on level i of a binary tree is 2i-1, i ≥ 1
Proof:
The proof is by induction on i.

Induction Base: The root is the only node on level i = 1. Hence, the maximum number of
nodes
on level i = 1 is 2 i-1 = 20 = 1.

Induction Hypothesis: Let i be an arbitrary positive integer greater than 1. Assume that the
maximum number of nodes on level i - 1 is 2i-2.

Induction Step:
The maximum number of nodes on level i - 1 is 2i-2 by the induction hypothesis.

Dept. of ISE, SVIT Page 2


18CS32 DSA Notes

Since each node in a binary tree has a maximum degree of 2, the maximum number of nodes
on level i is two times the maximum number of nodes on level i-1,
i.e 2* 2i-2 = 2i-1
Hence Prooved

2. The maximum number of nodes in a binary tree of depth k is 2k - 1, k ≥ 1.


The maximum number of nodes in a binary tree of depth k is

Lemma 2: [Relation between number of leaf nodes and degree-2 nodes]: For any non empty binary
tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0 = n2 + 1.

Proof:
Let n1 be the number of nodes of degree one and n the total number of nodes. Since all
nodes in T are at most of degree two, we have
n=n0+n1+n2 ---------(1)

If we count the number of branches in a binary tree, we see that every node except the root has
a branch leading into it.

If B is the number of branches, then n = B + 1 -------- (2)

All branches stem out from a node of degree one or two.


Thus, B = n1 + 2n2. Hence, we obtain --(-3)
Sum of the branches that stem out of a node(outdegree) is always equal to the sum of the
branches that stem into a node(indegree). Therefore Substitutuing eq(3) in eq(2)
n=B+1
n= n1 + 2n2 +1 (4)
Subtracting Eq. (4) from Eq. (1) and rearranging terms, we get
n0 = n2 + 1

Definition

Full Binary Tree: A full binary tree of depth k is a binary tree of depth k having 2k - 1 nodes, k ≥ 0.

Example

Figure 4.6 Full Binary tree of depth 4

Dept. of ISE, SVIT Page 3


18CS32 DSA Notes

The nodes are numbered in a full binary tree starting with the root on level 1, continuing with the
nodes on level 2, and so on. Nodes on any level are numbered from left to right.

Complete binary tree : A binary tree is complete if the number of nodes in each level i except
possibly the last level is 2i-1. The number of nodes in the last level appears as left as possible.

Example: A complete tree T11 with 11 nodes is shown below. This is not a full binary tree.

Complete Binary Tree

Extended Binary tree


 A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or
2 children.
 The nodes with 2 children are called internal nodes and the nodes with 0 children are called
external nodes. Sometimes the nodes are distinguished in diagrams by using circles for internal
nodes and squares for external nodes
 The term extended binary tree comes from the operation where tree T is converted into a 2-
tree by replacing each empty subtree by new node and the new tree is a 2-tree . The nodes in
the original tree T are internal nodes in the extended tree and the new nodes are the external
nodes in the extended tree.

Strictly Binary Tree is a tree where every non leaf node in a binary tree has non empty left and
right subtrees.A strictly binary tree with n leaves always contain 2n-1 nodes

Example:

Example For a Strictly Binary Tree

Dept. of ISE, SVIT Page 4


18CS32 DSA Notes

Almost Complete Binary Tree: A binary tree of depth d is an almost complete binary tree if

a. A node n at level less than d-1 has two sons


b. For any node n in the tree with a right descendant at level d, n must have a left son and every
left descendant of n is either a leaf at level d or has two sons
Example

Almost Complete Binary Tree.

Binary Tree representations


Array Representation: If a complete binary tree with n nodes is represented sequentially,
then for any node with index i, 1 ≤ i ≤ n, we have

 parent (i) is at [i / 2] if i ≠ 1. If i = 1, i is at the root and has no parent.


 leftChild (i) is at 2i if 2i ≤ n. If 2i > n, then i has no left child.
 rightChild (i) is at 2i + 1 if 2i + 1 ≤ n. If 2i + 1 > n, then i has no right child.

Example

Array representation of tree

Dept. of ISE, SVIT Page 5


18CS32 DSA Notes

This representation can be used for any binary tree. In most cases there will be a lot of unutilized
spaces. For complete binary tree such as

Linked Representation
Disadvantage of array representation
 The array representation is good for complete binary trees but, it wastes a lot of space for
many other binary trees.
 Insertion and deletion of nodes from the middle of a tree require the movement of potentially
many nodes to reflect the change in level number of these nodes.
 These problems can be overcome easily through the use of a linked representation.

Each node has three fields, leftChild, data, and rightChild.


A node can be defined as:
struct node
{
int data;
struct node * leftChild;
struct node * rightChild;
};

Typedef struct node TreeNode;

A node in a tree can be represented as follows

leftchild Data rightchild dat

Leftchild Rightchild
Node Representation

With this node structure it is difficult to determine the parent of a node, If it is necessary to be able to
determine the parent of random nodes, then a fourth field, parent, may be included in the class
TreeNode

Linked Representation of Skewed Tree

Dept. of ISE, SVIT Page 6


18CS32 DSA Notes

Linked Representation of Complete Tree

Binary Tree Traversals


 Traversing a tree is visiting each node in the tree exactly once.
 When a node is visited, some operation (such as outputting its data field) is performed on it.
 A full traversal produces a linear order for the nodes in a tree.
 When traversing a tree each node and its subtrees must be treated in the same fashion.
 Based on this there are three types of traversals inorder, preorder and postorder traversals.

Inorder Traversal
 Inorder traversal move down the tree toward the left until we can go no farther.
 Then "visit" the node,
 move one node to the right and continue.
 If we cannot move to the right, go back one more node and continue
 A precise way of describing this traversal is by using recursion as follows

void inorder(TreeNode * ptr)


{/* inorder tree traversal */
if (ptr)
{
inorder(ptr→leftChild);
printf("%d",ptr→data);
inorder(ptr→rightChild);
}
}

Preorder Traversal
 visit a node
 traverse left, and continue.
 When you cannot continue, move right and begin again or move back until you can move
right and resume."

void preorder(TreeNode *ptr)


{/* preorder tree traversal */
if (ptr)
{
printf("%d", ptr→data);
preorder(ptr→leftChild);
preorder(ptr→rightChild);

Dept. of ISE, SVIT Page 7


18CS32 DSA Notes

}
}

Postorder Traversal
 traverse left, and continue.
 When you cannot continue, move right and traverse right as far as possible
 Visit the node

void postorder(TreeNode *ptr)


{/* postorder tree traversal */
if (ptr)
{
postorder(ptr→leftChild);
postorder(ptr→rightChild);
printf("%d",ptr→data);
}
}

Example:

Inorder Traversal: DGBAHEICF


Preorder Traversal: ABDGCEHIF
Postorder Traversal: GDBHIEFCA

Expression Tree: An expression containing operands and binary operators can be represented
by a binary tree.

Representation and traversal of expression tree

A node representing an operator is a non leaf. A node representing an operand is a leaf. The root of
the tree contains the operator that has to be applied to the results of evaluating the left subtree and the
right subtree.

Dept. of ISE, SVIT Page 8


18CS32 DSA Notes

Example1: A+ B * C can be represented as follows

When the binary expression trees are traversed preorder we get the preorder expression. When we
traverse the tree postorder we get the postorder expression . When we traverse it inorder we get the
inorder expression.

For Example : consider the traversals for the tree given above

Inorder traversal giving rise to infix expression A+B*C


Preorder traversal giving rise to prefix expression +A*BC
Postorder traversal giving rise to postfix expression ABC*+

Example2: A+(B-C)*D$(E*F) is represented as follows

Inorder traversal giving rise to infix expression A+(B-C)*D$(E*F)


Preorder traversal giving rise to prefix expression : +A*-BC$D*EF
Postorder traversal giving rise to postfix expressionABC-DEF*$*+

Dept. of ISE, SVIT Page 9


18CS32 DSA Notes

Evaluation of Expression Tree

Example:

Result = 3

Additional Binary Tree operations


4.4.1. Copying Binary Trees

This function returns a pointer to an exact copy of the original tree.

TreeNode* Copy(TreeNode *original)


{
TreeNode * temp;
if (original!=NULL)
{
Temp=(TreeNode*)malloc(sizeof(TreeNode));
Temp->leftchild= copy(original->leftchild);
Temp->rightchild= copy(original->rightchild);
Temp->data=original ->data;
retrun temp;
}
return Null;
}

Testing Equality
 Equivalent Binary trees have the same structure and the same information in the
corresponding nodes.
 Same structure means every branch in one tree corresponds to a branch in the second tree that
is the branching of the trees is identical.
 This function returns true if the two trees are equivalent and false otherwise.

int equal(TreeNode *first,TreeNode *second)


{
If (first==NULL && second==NULL)
return true;

if(first !=NULL && second!=NULL && first->data==second->data &&

Dept. of ISE, SVIT Page 10


18CS32 DSA Notes

equal(first-> leftchild ,second->leftchild) &&


equal(first->rightchild,second->rightchild))
return true;

return false;
}

The Satisfiability problem


 Conisder the set of formulas that we can construct by taking the variables x1,x2,……xn and
operators (AND) V (OR) and  (NOT)
 The variables can hold any one of the two possible values true or false.
 The set of expressions that can be formed using these variables and operators is defined by
the following rules.
1. A variable is an expression
2. If x and y are expressions then x, xy, xy are expressions
3. Order of evaluation first  then  then .
4. Parenthesis can be used to alter the normal order of evaluations.

Defintion: The satisfiability problem for formulas of the propositional calculus asks if there is an
assignment of values to the variables that causes the value of the expression to be true.

Example: Representation of the expression (x1  x2)  (x1  x3)  x3 as a binary tree



 

x3
 


x3
x1 

X1
X2

Propositional formula in a binary Tree

Inorder Traversal of the tree is x1   x2  x1  x3  x3 this is the infix form of the expression.

Note: The node containing  has only a right branch since  is a unary operator.

Dept. of ISE, SVIT Page 11


18CS32 DSA Notes

To determine satisfiability (x1,x2,x3) must take all possible combinations of true or false values and
check the formula for each combination. For n variables ther are 2 n possible combinations of true=t
and false=f.

Example: For n=3 the eight combinations are (t,t,t),(t,t,f),(t,f,t),(t,f,f),(f,t,t), (f,t,f), (f,f,t),(f,f,f).

The node structure for the satisfaibility problem is given as follows

leftchild Data value rightchild

The node structure can be defined as follows

typedef enum {not,and, or, true, false } logical;


struct node
{
Struct node *leftchild;
Logical data;
Short int value;
Structnode * rightchild;
};
Typedef struct node TNODE;

Algorithm to determine satisfiability

For ( all 2n possible combinations)


{
Generate the next combination;
Replace the variables by their values;
Evaluate the root by traversing it in postorder;
If (root->value)
{
Printf(<combination>);
Return;
}
}
Printf(“ No satisfiable combination”);

Analysis: This algorithm will take O(g.2n ) or exponential time, where g is the time to substitute
values for x1,x2, ......... xn and evaluate the expression.

Postorder Evaluation function: To evaluate an expression, the tree is traversed in postorder. When
a node is visited the value of the expression represented by the left and right sub trees of a node are
computed first. So the recursive postorder traversal algorithm is modified to obtain the function that
evaluates the tree.

Void postorderEval(TNODE *node)


{
If (node)
{
Dept. of ISE, SVIT Page 12
18CS32 DSA Notes

postorderEval(node->leftchild);
postorderEval(node->rightchild);
switch(node->data)
{
Case not: node->value= ! node->rightchild->value;
Break;
Case and: node->value= node->rightchild->value && node>lefchild->value;
Break;
Case or: node->value= node->rightchild->value || node>lefchild->value;
Break;
Case true: node->value= true; break;
Case false: node->value= false; break;
}
}

Threaded Binary Tree

Threads

A binary tree has more NULL links than pointers. These null links can be replaced by special
pointers, called threads, to other nodes in the tree.

Two way threading


To construct the threads we use the following rules (assume that ptr represents a node):
 If ptr → leftChild is null, replace ptr → leftChild with a pointer to the inorder predecessor of
ptr.
 . If ptr → rightChild is null, replace ptr → rightChild with a pointer to the inorder successor of
ptr.

One way threading: If ptr → rightChild is null, replace ptr → rightChild with a pointer to the
inorder successor of ptr. Ptr->left child remains unchanged.

Note : unless specified we consider threading corresponds to the inorder traversal. To distinguish the
threads from ordinary pointers, threads are always drawn with broken links

Example: consider the binary tree and the corresponding threaded tree given below

Binary tree

Dept. of ISE, SVIT Page 13


18CS32 DSA Notes

Threaded Binary Tree

 When we represent the tree in memory, we must be able to distinguish between threads and
normal pointers.
 This is done by adding two additional fields to the node structure, leftThread and
rightThread.
 Assume that ptr is an arbitrary node in a threaded tree.
o If ptr → leftThread = TRUE, then ptr→ leftChild contains a thread; otherwise it
contains a pointer to the left child.
o Similarly, if ptr → rightThread = TRUE, then ptr → rightChild contains a thread;
otherwise it contains a pointer to the right child.

This node structure is defined as follows


struct node{
short int leftThread;
struct node * leftChild;
char data;
thread node *rightChild;
short int rightThread;
};
typedef struct node ThreadNode;

 In Figure two threads have been left dangling: one in the left child of H, the other in the right
child of G.
 To avoid loose threads, a header node is assumed for all threaded binary trees.
 The original tree is the left subtree of the header node.
 An empty binary tree is represented by its header node as in figure below

leftthread leftchild data Rightchild rightthread


True false

Empty threaded Binary tree

The complete memory representation for the tree is as below

Dept. of ISE, SVIT Page 14


18CS32 DSA Notes

t=True
f= False

Memory representation of threaded Tree

The variable root points to the header node of the tree, while root → leftChild points to the start of
the first node of the actual tree.

4.6.1 Inorder Traversal of a Threaded Binary Tree

By using the threads, we can perform an inorder traversal without making use of a stack.
 For any node, ptr, in a threaded binary tree, if ptr → rightThread = TRUE, the
inorder successor of ptr is ptr → rightChild by definition of the threads.
 Otherwise we obtain the inorder successor of ptr by following a path of left-child links from
the right-child of ptr until we reach a node with leftThread = TRUE.

Finding the inorder successor of a node: The function insucc finds the inorder successor of any
node in a threaded tree without using a stack.
ThreadNode * insucc(ThreadNode *tree)
{
ThreadNode * temp;
temp = tree→rightChild;
if (tree→rightThread==’f’)
while (temp→leftThread==’f’)
temp = temp→leftChild;
return temp;
}

Inorder traversal of a threaded binary tree

 To perform an inorder traversal we make repeated calls to insucc


 This function assumes that the tree is pointed to by the header node's left child and that the
header node's right thread is FALSE.

void Tinorder(ThreadNode * tree)


{
ThreadNode * temp = tree;
for (;;)
{
temp = insucc(temp);
if (temp == tree) break;

Dept. of ISE, SVIT Page 15


18CS32 DSA Notes

printf("%c", temp→data);
}
}

Analysis: The computing time for tinorder is still O(n)

Binary Search Trees

ADT Dictionary
objects: a collection of n > 0 pairs, each pair has a key and an associated item
functions: for all d ∈ Dictionary, item ∈ Item, k ∈ Key, n ∈ integer

DictionaryCreate(max_size) ::= create an empty Dictionary


Boolean IsEmpty(d, n) ::= if (n > 0) return TRUE else return FALSE
Element Search(d, k) ::= return item with key k,
return NULL if no such element.
Element Delete(d, k) ::= delete and return item (if any) with key k;
void Insert(d,item,k) ::= insert item with key k into d.

Definition Binary search tree


A binary search tree is a binary tree. It may be empty. If it is not empty then it satisfies the following
properties:
1) Each node has exactly one key and the keys in the tree are distinct.
2) The keys (if any) in the left subtree are smaller than the key in the root.
3) The keys (if any) in the right subtree are larger than the key in the root.
4) The left and right subtrees are also binary search trees.
5) The root has a key.
Example:

Binary search Tree

Searching a Binary Search Tree

To search for a node whose key is k. We begin at the root of the binary search tree.
 If the root is NULL, the search tree contains no nodes and the search is unsuccessful.
 we compare k with the key in root. If k equals the root's key, then the search terminates
successfully.
 If k is less than root's key, then, we search the left subtree of the root.
 If k is larger than root's key value, we search the right subtree of the root.

Structure of the node can be defined as follows

struct node
{
Struct node *lchild;

Dept. of ISE, SVIT Page 16


18CS32 DSA Notes

struct
{
int item; /* Itype represents the data type of the element*/
int key;
}data;

Struct node *rchild;


};

Typedef struct node TreeNode;

Recursive search of a binary search tree: Return a pointer to the element whose key is k, if there
is no such element, return NULL. We assume that the data field of the element is of type elemenet
and it has two components key and item.

Treenode * search(TreeNode * tree, int k)


{
if (tree==NULL) return NULL;
if (k == tree→data.key)
return (tree);
if (k < tree→data.key)
return search(tree→leftChild, k);
return search(tree→rightChild, k);
}

Iterative search of a Binary Search tree

Treenode* iterSearch(TreeNode * tree, int k)

{
while (tree!=null)
{
if (k == tree→data.key)
return (tree);
if (k < tree→data.key)
tree = tree→leftChild;
else
tree = tree→rightChild;
}
return NULL;
}

Analysis of Search(Both iterative and recursive): If h is the height of the binary search tree, then
we can perform the search using either search in O(h). However, recursive search has an additional
stack space requirement which is O(h).

Inserting in to a Binary Search Tree: Binary search tree has distinct values, first we searchthe
tree for the key and if the search is unsuccessful the key is inserted at the point the search
terminated

Dept. of ISE, SVIT Page 17


18CS32 DSA Notes

Example : consider the tree given below

BST Tree Insert 80 Insert 35

Inserting a dictionary pair into a binary search tree

If k is in the tree pointed at by node do nothing. Otherwise add a new node with data = (k, item)

TreeNode* insert(TreeNode *root, int k, int Item)


{
TreeNode * ptr, *lastnode;

lastnode=Modifiedsearch(root,k);
Ptr=(TreeNode*)malloc(sizeof(TreeNode));
ptr→data.key = k;
ptr→data.item = Item;
ptr→leftChild = ptr→rightChild = NULL;

if (root==NULL)
{
root=ptr;
return(root);
}

if(lastnode!=NULL)
{
if (k < lastnode→data.key)
lastnode→leftChild = ptr;
else
lastnode→rightChild = ptr;
return (root);
}

If the element is present or if the tree is empty the function Modifiedsearch returns NULL. If the
element is not present it retrun a pointer to the last node searched.

Modifiedsearch(Treenode *root,int k)

Dept. of ISE, SVIT Page 18


18CS32 DSA Notes

TreeNode *temp,*prev;
temp==node;
prev=NULL;
If(temp==NULL)
return(NULL);
while(temp!=NULL)
{
if(temp->data.key==k)
{
printf(“element already found”);
return(NULL);
}
if(key<temp->data.key)
{
prev=temp;
temp=temp->lchild;
}
else
{
Prev=temp;
Temp=temp->rchild;
}
}
retrun(prev);

Deletion from a binary search tree: Suppose T is a a binary search tree. The function to
delete an item from tree T first search the tree to find the location of the node with the item
and the location of the parent of N and the deletion of the node N depends on three cases:

Case 1: N has no children. Then N is deleted from T by replacing the location of the node N in the
parent(N) by the NULL pointer

Example: Deleting Node 66 with NO children

Deleting node

Case 2: If N has exactly one child. Then N is deleted from T by replacing the location of N in Parent
(N) by the location of the only child of N.

Example: Deleting Node 75 with exactly one children

Dept. of ISE, SVIT Page 19


18CS32 DSA Notes

Deleting node 75

Case 3: N has Two children. Let S(N) denote the inorder successor of N(S(N) does not have a left
child).Then N is deleted from T by first deleting S(N) from T (by using case1 or cae 2) and then
replacing node N in T by the node S(N).

Example: Deleting Node 25 with two children

To delete node 25,


first delete its
inorder successor
33

Now replace the node 25 with its inorder successor 33

Recursive function to delete a node in a BST


TreeNode *delete_element(TreeNode *node, int key)
{
TreeNode * temp;

if (node == NULL)
return node;

if (key < node->data.key)


node->lchild = delete_element(node->lchild, key);
else if (key > node->data.key)
node->rchild = delete_element(node->rchild, key);

Dept. of ISE, SVIT Page 20


18CS32 DSA Notes

else
{
// node with only one child
if (node->lchild == NULL)
{
temp = node->rchild;
free(node);
return temp;
}
else if (node->rchild == NULL)
{
temp = node->lchild;
free(node);
return temp;
}
// node with two children

else
{
temp = node->rchild;
while(temp->lchild!=NULL) //Get the inorder successor
temp=temp->lchild;
node->data.item = temp->data.item;
node->data.key=temp->data.key;
node->rlink = delete_element(node->rchild, temp->data.key);
return node;

}
}}

Dept. of ISE, SVIT Page 21

You might also like