Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
35 views86 pages

Module 5

The document discusses trees and binary trees. It defines trees and binary trees, and covers their key terminology like nodes, edges, root, leaf nodes, and more. It also discusses different ways to represent trees and binary trees, including array representation and linked representation. Properties of binary trees are covered, like the maximum number of nodes and the relationship between leaf nodes and degree-2 nodes.

Uploaded by

Harsha Rampur
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)
35 views86 pages

Module 5

The document discusses trees and binary trees. It defines trees and binary trees, and covers their key terminology like nodes, edges, root, leaf nodes, and more. It also discusses different ways to represent trees and binary trees, including array representation and linked representation. Properties of binary trees are covered, like the maximum number of nodes and the relationship between leaf nodes and degree-2 nodes.

Uploaded by

Harsha Rampur
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/ 86

TREES

PART 1

Availaible at VTU HUB (Android App)


TREES
A tree is a finite set of one or more nodes such that
 There is a specially designated node called root.
 The remaining nodes are partitioned into disjoint set T1,T2…,Tn, where
each of these sets is a tree. T1,T2…,Tn are called as the subtrees of the
root and n >= 0

 Example: Consider elements in nodes are A,B,C,D,E,F,G,H,I,J,K,L,M

Availaible at VTU HUB (Android App)


Terminology
 Node: The item of information
 Edge: A link from node N of a Tree
to a successor is called an edge drawn as a line.
 Degree: The number of subtrees of a node
 Degree of node A is 3, c is 1, k is 0
 Degree of a tree: The maximum of the degree of the nodes in the tree.
Degree of the tree shown is 3
 Terminal nodes (or leaf): nodes that have degree zero or node with no
successor .K,L,F,G,M,I,J are leaf nodes
 Non terminal nodes: nodes that have degree greater than zero are said
to be non terminal nodes. A,B,C,D,E,H are non terminal nodes
 Parent and Children: Suppose N is a node in T with successors S1,S2
and S3, then N is called the Parent (or father) of S1,S2and S3. Here,
S1,S2 and S3 are called as children (or Sons) of N.
 Siblings: Children of the same parent are said to be siblings or brothers
 Root of tree is that node that
Availaible hasHUB
at VTU no(Android
parental
App) node
 Path: A sequence of consecutive edges from node N to a
node M is called a path.
 Ancestors of a node: All the nodes along the path from the
root to that node.
 Descendants of node: All the nodes along the path from
that node to terminating nodes
 The level of a node: defined by letting the root be at level
one. If a node is at level L, then it’s children are at level
L+1.
 Height (or depth): The maximum level of any node in the
tree

Availaible at VTU HUB (Android App)


Example
 A is the root node
 B is the parent of E and F
 C and D are the sibling of B
 E and F are the children of B
 K, L, F, G, M, I, J are terminal nodes or external nodes or
leaves
 A, B, C, D, E, H are internal nodes
 The level of E is 3
 The height (depth) of the tree is 4
 The degree of node B is 2
 The degree of the tree is 3
 The ancestors of node M is A, D, H
 The descendants of node D is H, I, J, M

Availaible at VTU HUB (Android App)


Representation of Trees
 There are several ways to represent a given tree such as:
1. List Representation
2. Left Child- Right Sibling Representation
3. Representation as a Degree-Two tree

Figure (A)

Availaible at VTU HUB (Android App)


List Representation:
 The tree can be represented as a List. The tree of figure (A) could
be written as the list.
 (A (B (E (K, L), F), C (G), D (H (M), I, J) ) )
The information in the root node comes first.
The root node is followed by a list of the subtrees of that node.
Tree node is represented by a memory node that has fields for the data
and pointers to the tree node's children

Availaible at VTU HUB (Android App)


Availaible at VTU HUB (Android App)
 Since the degree of each tree node may be different, so
nodes with a varying number of pointer fields are used.
 For a tree of degree k, the node structure can be represented
as below figure. Each child field is used to point to a subtree.

Availaible at VTU HUB (Android App)


Left Child-Right Sibling Representation
 The below figure show the node structure used in the left child-right

Example:
 In Figure (A), the leftmost child of A is B, and the leftmost child of D is H.
 The closest right sibling of B is C, and the closest right sibling of H is I.
 Choose the nodes based on how the tree is drawn. The left child field of
each node points to its leftmost child (if any), and the right sibling field
points to its closest right sibling (if any).
 Figure (D) shows the tree of Figure (A) redrawn using the left child-right
sibling representation. sibling representation
Availaible at VTU HUB (Android App)
Figure A

Availaible at VTU HUB (Android App)


Representation as a Degree-Two Tree
 To obtain the degree-two tree representation of a tree, simply rotate
the right-sibling pointers in a left child-right sibling tree clockwise by
45 degrees. This gives us the degree-two tree displayed in Figure (E).
 In the degree-two representation, a node has two children as the left
and right children.

Figure (E): degree-two representation

Availaible at VTU HUB (Android App)


BINARY TREE (BT)
 Definition: A binary tree T is defined as a finite set of nodes such that,
 T is either empty or
 T consists of a root and two disjoint binary trees called the left sub tree and
the right sub tree.
 Examples to Binary trees

 The difference between a tree and BT are


 There is no tree having zero nodes , but there is an empty BT
 In BT we distinguish order of children left and right while in trees we do not have
the order

Availaible at VTU HUB (Android App)


Different kinds of Binary Tree
 1.Strictly Binary tree : If every non terminal has degree two, then
Such BT is called as Strictly binary tree
 2. Skewed Binary Tree : It is a tree consisting of only left subtree or
only right subtree.
 A tree with only left subtrees is called Left Skewed Binary Tree.
 A tree with only right subtrees is called Right Skewed Binary Tree.

Strictly BT Availaible at VTU HUBLeft Right skewed BT


skewed
(Android BT
App)
 3. Full Binary Tree
 A full binary tree of depth „k‟ is a binary tree of depth k
having 2k – 1 nodes, k ≥ 1.
 A fully binary tree is a strictly BT in which all the leaf nodes
are at same level.

Availaible at VTU HUB (Android App)


 4. Complete Binary Tree (Almost complete BT)
 A binary tree T with n nodes and depth K is said to complete
BT if all the nodes at level less than K-1 has two sons and
every right descendant at level d must have left son and every
left descendent of node is either leaf or has two sons.

Complete BTs Not Complete BTs


Availaible at VTU HUB (Android App)
5. Extended Binary Trees or 2-trees
 An extended binary tree is a transformation of any binary tree into a complete
binary tree.This transformation consists of replacing every null subtree of the
original tree with “special nodes.”The nodes from the original tree are then
internal nodes, while the special nodes are external nodes.
 The following tree is its extended binary tree. The circles represent
internal nodes, and square represent external nodes.
 Every internal node in the extended tree has exactly two children,
and every external node is a leaf. The result is a complete binary tree.
 For instance, consider the following binary tree.

Availaible at VTU HUB (Android App)


PROPERTIES OF BINARY TREES
 Lemma 1: [Maximum number of nodes]:
 (1) The maximum number of nodes on level i of a binary tree is 2i-1, i ≥ 1.
 (2) The maximum number of nodes in a binary tree of depth k is 2k -1, k ≥ 1.
 Proof:
(1) 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 2i-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. 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, that is=2*2i-2= 2i-1

Availaible at VTU HUB (Android App)


Availaible at VTU HUB (Android App)
Lemma 2: [Relation between number of leaf nodes and degree-2 nodes]:
For any nonempty 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)
 Every node except root has branch leading to it. If B is the number of
branches, then n =B + 1.
 All branches stem from a node of degree one or two. Thus,
B =n 1+ 2n2.
 Hence, we obtain
n = B + 1= n 1+ 2n2 + 1 (2)
 Subtracting Eq. (2) from Eq. (1) and rearranging terms, we get
n0 = n2 +1

Availaible at VTU HUB (Android App)


 Consider the figure:

Here, For Figure (a) n2=0, n0= n2+1= 0+1=1


Therefore, the total number of leaf node=1

Here, For Figure (b) n2=4, n0= n2+1= 4+1=5


Therefore, the total number of leaf node=5

Availaible at VTU HUB (Android App)


BINARY TREE REPRESENTATION
 The storage representation of binary trees can be classified as
1. Array representation
2. Linked representation.

 Array representation:
A tree can be represented using an array, which is called
sequential representation.
The nodes are numbered from 1 to n, and one dimensional array
can be used to store the nodes.
Position 0 of this array is left empty and the node numbered i is
mapped to position i of the array.

Availaible at VTU HUB (Android App)


Array representation for the trees

•For complete binary tree the array


representation is ideal, as no space is wasted.
• For the skewed tree less than half the array is
utilized.

Availaible at VTU HUB (Android App)


Array representation for the trees

Availaible at VTU HUB (Android App)


Linked representation:
The problems in array representation are:
 It is good for complete binary trees and full binary tree because
more memory is wasted for skewed and other binary trees.
 The insertion and deletion of nodes from the middle of a tree
require the movement of many nodes to reflect the change in level
number of these nodes.
These problems can be easily overcome by linked representation
Each node has three fields,
 LeftChild - which contains the address of left subtree
 RightChild - which contains the address of right subtree.
 Data - which contains the actual information

Availaible at VTU HUB (Android App)


Representation of node:
Struct node {
int data;
struct node* leftChild;
struct node * rightChild;
};
typedef struct node *treepointer;
Treepointer start =NULL;

Availaible at VTU HUB (Android App)


Linked Representation of BT

Availaible at VTU HUB (Android App)


BINARY TREE TRAVERSALS
 Visiting each node in a tree exactly once is called tree traversal
 Traversal can be performed by performing three operation
 L stands for moving left
 R stands for moving right
 V for visiting node
Six possible combinations are- LVR,LRV,VLR,VRL,RVL and RLV
If we adopt convention that we traverse left before right then we get
three traversals- LVR, LRV, VLR which are named as Inorder,
Postorder and Preorder respectively.
There is natural correspondence between these traversals and infix,
postfix and prefix expressions.

Availaible at VTU HUB (Android App)


1. Inorder: (LVR)
 Inorder traversal calls for
 Moving down the tree toward the left until you cannot go
further.
 Then visit the node
 Move one node to the right and continue. If no move can be
done, then go back one more node.
 The inorder traversal of a binary tree can be recursively
defined as
 Traverse the left subtree in inorder.
 Visit the root.
 Traverse the right subtree in inorder.

Availaible at VTU HUB (Android App)


 void inorder(treepointer ptr)
{
if (ptr)
{
inorder (ptr→leftchild);
printf (“%d”,ptr→data);
inorder (ptr→rightchild);
}
}
Inorder traversal of tree shown gives H D I B E A F C G
Availaible at VTU HUB (Android App)
2. Preorder: (VLR)
 The Preorder traversal of a binary tree can be recursively defined as
 Visit the root
 Traverse the left subtree in preorder.
 Traverse the right subtree in preorder

 Recursive C function for preorder


 void preorder (treepointer ptr)
{
if (ptr)
{
printf (“%d”,ptr→data)
preorder (ptr→leftchild);
preorder (ptr→rightchild);
}
}
Preorder traversal of tree shown gives A B D H I E C F G
Availaible at VTU HUB (Android App)
3. Postorder: (LRV)
 The Postorder traversal of a binary tree can be recursively defined as
 Traverse the left subtree in postorder.
 Traverse the right subtree in postorder.
 Visit the root

 Recursive C function for postorder Traversal


 void postorder(treepointer ptr)
{
if (ptr)
{
postorder (ptr→leftchild);
postorder (ptr→rightchild);
printf (“%d”,ptr→data);
}
}
Postorder traversal of tree shown gives H I D E B F G C A
Availaible at VTU HUB (Android App)
Iterative inorder Traversal:
 Inorder, preorder and postorder traversals can be developed using iterative
functions explicitly making use of stack function.
 In the iterative inorder traversal, left nodes pointers are pushed into stack
until a null node is reached, the node is then removed from the stack and
displayed, and the node‟s right child pointer is stacked. The traversal then
continues with the left child.The traversal is complete when the stack is empty.

Inorder traversal of tree: H D I B E A F C G

Availaible at VTU HUB (Android App)


Expression Trees

 Ordered tree may be used to represent general expression


 Expression tree is a binary tree in which all operands are leaf
nodes and operators are non leaf nodes.
 Root is also an operator
 There is natural correspondence between these traversals and
infix, postfix and prefix expressions.
 For a given infix expression we write expression tree.
 Preorder and Postorder traversal of that expression tree
gives prefix and postfix expressions

Availaible at VTU HUB (Android App)


Expression Trees- Examples

Availaible at VTU HUB (Android App)


Expression Trees- Examples

Availaible at VTU HUB (Android App)


Expression Trees- Examples

Availaible at VTU HUB (Android App)


Level-Order traversal:
 The nodes in a tree are numbered starting with the root on level
1, then level 2 and so on.
 Visiting the nodes using the ordering suggested by the
node numbering is called level ordering traversing.
 Firstly visit the root, then the root‟s left child, followed by the
root‟s right child. Thus continuing in this manner, visiting the
nodes at each new level from the leftmost node to the rightmost
node.

 Level order traversal: 1 2 3 4 5

Availaible at VTU HUB (Android App)


working
 Level order traversal uses Queue data structure.
 Initially , insert the pointer to root to the queue. The
function operates by deleting the node at the front of
the queue, printing the nodes data field and adding the
nodes left and right children pointers to the queue.
 The nodes children are at the next lower level and we
insert the left child before the right child. Hence
function prints out the nodes using the ordering
schemes

Availaible at VTU HUB (Android App)


level order traversal of a binary tree

Expression is

Level order traversal : +*E*D/CAB

Availaible at VTU HUB (Android App)


Traversal without stack
 By adding parent field to each node we can trace back up to
root and down again. Hence it is possible to do inorder,
preorder and postorder traversal without stack by using
parent field of node.
 By representing binary tree as threaded binary tree , it is
possible to traverse a binary tree without stack

Availaible at VTU HUB (Android App)


THREADED BINARY TREE
 In binary tree, there are n+1 null links out of 2n total links.
 In the linked representation of any binary tree, there are
more null links than actual pointers. These null links are
replaced by the pointers, called threads, which points to
other nodes in the tree.
 To construct the threads use the following rules:
Let ptr represents a node.
1. If ptr→leftChild is null, then replace the null link with a
pointer to the inorder predecessor of ptr.
2. If ptr →rightChild is null, replace the null link with a
pointer to the inorder successor of ptr.

Availaible at VTU HUB (Android App)


1. If ptr→leftChild is null, then replace the null link with a pointer to
the inorder predecessor of ptr.

2. If ptr →rightChild is null, replace the null link with a pointer to the
inorder successor of ptr.

Availaible at VTU HUB (Android App)


Example
Consider the binary tree as shown in below figure:

Figure A: Binary Tree

Figure B: Threaded tree


corresponding to Figure A

In above figure the new threads are drawn in broken lines. This tree
has 9 node and 10 0-links which has been replaced by threads.
In Figure B two threads have been left dangling: one in the left child of H, the other in
the right child of G.
Availaible at VTU HUB (Android App)
Structure of node in threaded BT
 When trees are represented in memory, it should be able to
distinguish between threads and pointers. This can be done by
adding two additional fields to node structure, ie., leftThread and
rightThread

 If ptr→leftThread = TRUE, then ptr→leftChild contains a


thread, otherwise it contains a pointer to the left child.
 If ptr→rightThread = TRUE, then ptr→rightChild contains a
thread, otherwise it contains a pointer to the right child.

Availaible at VTU HUB (Android App)


Node Structure:
 The node structure is given in C declaration
struct node{
short int leftThread;
short int rightThread;
char data;
struct node* leftChild;
struct node * rightChild;
}
 typedef struct node *threadPointer ;

Availaible at VTU HUB (Android App)


The complete memory representation
for threaded binary 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.This is true for all threaded trees. Here the problem of the
loose threads is handled by pointing to at
Availaible the head
VTU HUB node calledApp)
(Android root.
Inorder Traversal of a Threaded Binary Tree
 By using the threads, an inorder traversal can be performed
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.
 The function insucc ( ) finds the inorder successor of any
node in a threaded tree without using a stack.

Availaible at VTU HUB (Android App)


Program: Inorder traversal of a threaded binary tree
 To perform inorder traversal make repeated calls o insucc ( )
function
 void tinorder (threadedpointer tree)
{
threadedpointer temp = tree;
for(; ;){
temp = insucc(temp);
if (temp == tree) break;
printf(“%c”, temp→data);
}
}

Availaible at VTU HUB (Android App)


Program: Finding inorder successor of a node

 threadedpointer insucc(threadedPointer ptr)


{
threadedpointer temp;
temp = ptr→rightChild;
if (!ptr→rightThread)
while (!temp→leftThread)
temp = temp→leftChild;
return temp;
}

Availaible at VTU HUB (Android App)


Tracing : Inorder traversal of a threaded binary tree

void tinorder (threadedpointer tree)


{
threadedpointer temp = tree;
for(; ;){
temp = insucc(temp);
if (temp == tree) break;
printf(“%c”, temp→data);
}
}
Inorder :HDIBEAFCG

Availaible at VTU HUB (Android App)


Inserting a Node into a Threaded Binary Tree
 Case : The insertion of r as the right child of a node s
 The cases for insertion are:
1. If s has an empty right subtree, then the insertion is
simple and diagrammed in Figure

Availaible at VTU HUB (Android App)


Inserting a Node into a Threaded Binary Tree
 2. If the right subtree of s is not empty, then this right subtree is made
the right subtree of r after insertion. When this is done, r becomes the
inorder predecessor of a node that has a leftThread == true field, and
consequently there is a thread which has to be updated to point to r. The
node containing this thread was previously the inorder successor of s.

Availaible at VTU HUB (Android App)


Insert right function- /* insert r as the right child of s */
 void insertRight(threadedPointer S, threadedPointer r)
{
threadedpointer temp;
r→rightChild = S→rightChild;
r→rightThread =S→rightThread;
r→leftChild = S;
r→leftThread = TRUE;
S→rightChild = r;
S→rightThread = FALSE;
if (!r→rightThread) {
temp = insucc(r);
temp→leftChild = r;
}
}
Availaible at VTU HUB (Android App)
Constructing Binary for given traversals
Inorder: DGBAHEICF Preorder: ABDGCEHIF

Inorder: DGB A HEICF Preorder: A BDG CEHIF


Left -Root-Right Root - Left - Right

Availaible at VTU HUB (Android App)


Constructing Binary for given traversals
Inorder: DGBAHEICF Postorder: GDBHIEFCA

Inorder: DGB A HEICF Postorder: GDB HIEFC A


Left -Root-Right Left-Right -Root

Availaible at VTU HUB (Android App)


ADDITIONAL BINARY TREE OPERATIONS
1. Copying a Binary tree
 This operations will perform a copying of one binary tree to another.
 C function to copy a binary tree: Code for copying binary tree is slightly modified
version of postorder traversal
treepointer copy(treepointer original)
{
treepointer temp;
if(original) {
temp=(treepointer) malloc (sizeof(*temp));
temp→leftchild=copy(original→leftchild);
temp→rightchild=copy(original→rightchild);
temp→data=original→data;
return temp;
}
return NULL;
}

Availaible at VTU HUB (Android App)


2. Testing Equality
 This operation will determine the equivalence of two binary tree.
Equivalence binary tree have the same structure and the same
information in the corresponding nodes.
 int equal(treepointer first,treepointer second)
{
return((!first && !second) ||(first && second && (first→data==second→data)
&& equal(first→leftchild,second→leftchild)
&& equal(first→rightchild, second→rightchild))
}
This function will return TRUE if two trees are equivalent and FALSE if
they are not.

Availaible at VTU HUB (Android App)


3. The Satisfiability problem
Consider the formula that is constructed by set of variables: x1, x2, …, xn
and operators ^(and), V (or), ¬ (not).
The variables can hold only of two possible values, true or false.
The expression can form 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 V y are expressions
3. Parentheses can be used to alter the normal order of evaluation (¬ > ^ > V)

 Example: x1 V (x2 ^ ¬ x3) If x1 and x3 are false and x2 is true


= falseV (true ^ ¬false)
= falseV true
= true
 The satisfiablity problem for formulas of the propositional calculus asks if
there is an assignment of values to the variable that causes the value of the
expression to be true.
Availaible at VTU HUB (Android App)
Let’s assume the formula in a binary tree
(x1 ^ ¬x2) V(¬ x1 ^ x3) V ¬x3

The inorder traversal of this tree is


x1 ^ ¬x2 V ¬ x1 ^ x3V ¬x3
Availaible at VTU HUB (Android App)
 The algorithm to determine satisfiablity is to let (x1, x2, x3)
takes on all the possible combination of true and false values
to check the formula for each combination.
 For n value of an expression, there are 2n possible combinations of
true and false
 For example 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).

Availaible at VTU HUB (Android App)


Module - 4

Trees – Part II

Availaible at VTU HUB (Android App)


Binary Search Trees (BST)
Definition of binary search tree is a binary tree, may be
empty, if it is not empty then it satisfies the following
properties
 Every node has exactly one key and keys in the tree are
distinct.
 The keys in a nonempty left subtree are smaller than the
key in the root of subtree
 The keys in a nonempty right subtree are larger than the
key in the root of subtree
 The left and right subtrees are also binary search trees

Availaible at VTU HUB (Android App)


Examples
 Figure (a) is not a BST whereas Figure (b) and © are not BST

Availaible at VTU HUB (Android App)


Construction of BST
Given Keys : 14,15,4,9,7,18,3,5,16,20,17

InorderTraversal : 3,4,5,7,9,14,15,16,17,18,20

Availaible at VTU HUB (Android App)


Searching in a binary search tree
 Searching for a node with key value key begins at root.
 If key equals root key search terminates.
 If key is less than root’s key searching continues in the left sub
tree
 If key is greater than root’s key searching continues in the right
sub tree

 Searching function may be written recursively or by iterative


method.

Availaible at VTU HUB (Android App)


Recursive search in BST

Availaible at VTU HUB (Android App)


Iterative search in BST

Availaible at VTU HUB (Android App)


Inserting in to BST
To insert an item with a key value
 We must verify that key is different from those of existing.
 To do that we search tree .
 If search is unsuccessful, then we insert the item at the point
where search is terminated
 Example:

Given BST
Availaible at VTU HUB (Android App)
Insert function
 The function insert uses modified search which is modified
version of function itersearch which searches the BST *node
for the key K.
 If tree is empty or K is present , it returns NULL.
 Otherwise it returns a pointer to the last node of the tree
that was encountered during search.
 The new item is to be inserted as child of this node.

Availaible at VTU HUB (Android App)


Inserting an element into a BST

Availaible at VTU HUB (Android App)


Inserting an element into a BST
 Void insert(tree pointer *node, int num)
{
treepointer ptr, temp= modified_search(*node, num);
if (temp || !(*node))
{
ptr=(treepointer) malloc(sizeof(node));
if (ptr==NULL)
{
printf( “Memory full, No allocation”);
exit(o);
}
ptr->data=num;
ptr-> left_child=ptr-> reght_child=NULL;
if(*node)
if (num< temp->data) temp->left _child = ptr;
else temp->right_child=ptr;
else (*node)=ptr;
}
} Availaible at VTU HUB (Android App)
Deletion from BST
 Three cases should be considered
1. Deletion of leaf node: By just making the corresponding
left or right child of parent to NULL.
2. Deletion of non leaf that has only one child: This is
made by placing single child in place of freed parent.
3. Deletion of non leaf node having two children: In this
case the node to be deleted place is occupied by largest pair
(item) in it’s left subtree or smallest one in it’s right subtree.
Then we proceed to delete this replacing pair(item) from
subtree from which it is taken.

Availaible at VTU HUB (Android App)


Examples :Deletion from BST

Case :1

Before Deletion After Deletion of 80

Case :2
Before Deletion After Deletion of 5

Case :3
Before Deletion
Availaible at VTU HUB (Android App)
After Deletion of 30
Deletion of non leaf node having two children:

Step 1: In this case the node to be deleted 60 place is


occupied by largest pair (item) in it’s left subtree 55.
Step 2: Then we proceed to delete this replacing node 55
from subtree from which it is taken by its only child 52

Availaible at VTU HUB (Android App)


Expression tree for Expression: 5*4+(100 – 20/2)

Answer =110
Availaible at VTU HUB (Android App)
Evaluation of Expression using Tree
 Algorithm : Solve ( tree)
if (tree!=NULL)
if tree. info is operand then
return tree.info
else
Answer =110
A=Solve(tree.leftchild)
B= Solve(tree.rightchild)
return A operator B
where operator is info in tree

Availaible at VTU HUB (Android App)


C – Code to construct a BST
#include<stdio.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef struct node *bptr;
void create(bptr);
bptr getnode();
void preorder(bptr );
void inorder(bptr );
void postorder(bptr );
bptr search(bptr , int );
Availaible at VTU HUB (Android App)
bptr getnode()
{
bptr temp;
temp=(bptr)malloc(sizeof(struct node));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}

Availaible at VTU HUB (Android App)


bptr create(root)
{
int n,i;
bptr temp,res;
printf("enter how many nodes\n");
scanf("%d",&n);
root=getnode();
printf("enter the info\n");
scanf("%d",&root->info);
for(i=1;i<n;i++)
{
printf("enter info\n");
scanf("%d",&num);
res=search(root, num);

Availaible at VTU HUB (Android App)


if(res==NULL)
{
printf("DUPLICATE\n");
continue;
}

temp = getnode();
temp->info=num;
if(temp->info < res->info)
res->lchild=temp;
else
res->rchild=temp;
}
return root;
}

Availaible at VTU HUB (Android App)


bptr search(bptr root, int num)
{
bptr temp=root;
bptr trail=root;
while(temp!=NULL)
{
if(temp->info==num)
return NULL:
else if(num<temp->info)
{
trail=temp;
temp=temp->lchild;
}

Availaible at VTU HUB (Android App)


else
{
trail=temp;
temp=temp->rchild;
}
}
return trail;
}

Availaible at VTU HUB (Android App)


void preorder(bptr root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->lchild);
preorder(root->rchild);
}
}

Availaible at VTU HUB (Android App)


void inorder(bptr root)
{
if(root!=NULL)
{
inorder(root->lchild);
printf("%d\t",root->info);
inorder(root->rchild);
}
}

Availaible at VTU HUB (Android App)


void postorder(bptr root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d\t",root->info);

}
}

Availaible at VTU HUB (Android App)

You might also like