(Nonlinear (6-31) : Structures GTU-Sem. 3-Comp/T) Binary Tree
(Nonlinear (6-31) : Structures GTU-Sem. 3-Comp/T) Binary Tree
3-Comp/T)
(NonlinearData Structure)...Page no. (6-31)
Date
Element
Binary Tree
45
56
39
12
4s)
78
Post-order Traversal
2,6,7,9,8, 5, 15, 10
for the followin9
***
tree
.Construct a binary search
inorder and postorder
Sequence. Also do the
:
traversal for the same
34, 78, 54, 67, 10, 32, 54
45, 56, 39, 12, GTU-S-14, 7 Marks
,81 - - - - - * * * *
***-------.
Tech-Neo
Publications.A SACHIN SHAH Venture
(G3-01)
GTU-NewSylabus w.e.f.academicyear 19-20)
*
Data Structures (GTU-Sem.
3-Comp/T) (Nonlinear Data Structure)..Page no. (6-20
32
45
(56)
67
81 18
10 (34) 67 89
50
40
39
(50
32 4)
80
(50
20 (A0 (80)
89
89
10 (89 (A0) 80
0
SHAHVent
(GTU-New Syllabus w.e.f. academic year 19-20) (G3-01) Tech-Neo Publications...A SAC
ructures (GTU-Sem. 3-Comp/1T) (Nonlinear Data Structure)....Page no. (6-33)
D a t a
70
50 (50)
(80)
(20) (80)
50)
In Order -0 10 20 30 40 50 60 70 80 90
80)
Post Order 100 30 20 40 70 60 90 8050
10 (20 In Order - 0 10 30 0 40 50 70 60 80 90
1) 14 2) 10
60
40 (80 10
3) 17 4) 12
90 (20
1
(10)
12
60
5)10 6) 11
(80
20 60 10
(90
10
10)
U-New Syllabus w.e.f..academic year 19-20) (G3-01) Tech-Neo Publications...A SACHIN SHAH Venture
Data Structures (GTU-Sem. 3-Comp/IT) (Nonlinear Data Structure no. (6-3A
7) 20 8) 12
14 11) 20
(10) (10 (
(12) 10
12) 8
10 19)
(10
(1) (10
10) 25
13) 22
10) (1)
10
10) 19 (12) (
(1) 10
12 (18) (
20
3) 30 4) 60
14) 11
30
(10)
60)
0
5)15 6) 75
2 ( (5
(20 19
(1) (2 60
15) 23 7) 35
(10 30
8) 55
(10) 25
(20
****
) 45
2) 70
60
20
STJ-NeW Syllabus
SylR w.e.f. academic year 19-20) (G3-01) Tech-Neo Publications ..A SACHIN SHAH Venture
Data Structures (GTU-Sem. 3-Comp/TD (Nonlinear Data Structure)....Page
10) 85
no. (6-36
5) 12 6) 15
45
(30)
(10) (14
1 (35 60
20 (85 7) 16 8) 7
11) 80
14
(30
13) (60
9)9
20
(14)
Preorder: 45,30,15.20.35.70.60,55,75,85,80
Inorder: 15,20.30,35,45.5560,70.75,80,85
Postorder: 20,15,35,30,55.60,80,85,75,70,45
- - .
GTU-W-18,7 Marks
2) 6
1) 11
(8) (
3) 14 4) 8
(2
(14) 1
Ventue
SHAH
(GTU-New Syllabus w.e.f. academic year 19-20) (G3-01) Al Tech-Neo Publications A SACHIN
D a t e
(GTU-Sem. Comp/T)
S t r u c t u r e s( G T U . S s
*******************---- -----.
, In a binary search tree, the deletion operation is
performed with O(log n) time complexity. Deleting a 1
node from Binary search tree has following three
cases
New Syllabus e.f. academic year 19-20) (G3-01 Tech-Neo Publications .MA SACHIN SHAH Venture
Data Structures (GTU-Sem. 3-Comp/IT) (Nonlinear Data Structure)...Page no ls.
Step 3 Swap both deleting node and node which found
in above step.
if (found == FALSE)
if the node to deleted
is not found
if(x->Lehild!=NULL &&x->Rchild!=NULL
If the node to be
parent X;
Replace 12 with 19. Notice, that only values are deleted has two
nodes. Now have two nodes with the
XSucc = x -> Rchild; children
replaced, not we
same value.
while ( xsucc -> Lchild != NULL)
parent XSucc ;
Xsucc= xsucc -> Lchild ;
x XsucC
Ventue
(GTU-New Syllabus
w.e.f. academic year 19-20) (G3-01)
Tech-Neo Publications...ASACHIN SHAH
ctures (GTU-Sem. 3-Comp/IT)
D a t aS t r u c t u r e s ( G T U - S e n
n
(Nonlinear Data Structure)....Page no. (6-39)
Lchild == NULL && x -> Rehild== NULL
if(x count = count + inorder ( sr - > Lehild )
f(parent-> Rchild== x)
else * * * - - - -
free (x)
return, LECTURE -
Syllabus Covered
if(x->Lehild != NULL && x -> Rchild== NULL) Complexity of Binary Search Tree
() Program to perform operations on Tree
If the node to be deleted
has only left child (Gi) Conversion of General Tree to Binary Tree
6.7.5 Complexicy of Binary Search Tree
i f (parent > Lchid == x)
else
Consider following BST
parent-> Rchild= x ->> Lehild;
free (x )
return
case
struct nodebt *Lchild ;
preorder (bt ) ;
int nodedata;
break:
struet nodebt *Rchild;
case 5:
void insertbt (struet nodebt **, int );
postorder (bt );
void deletebt ( struct nodebt **, int ) ;
break;
void searchbt (struct nodebt **, int, struct nodebt **,
struct nodebt **, int * ) :
default:
void inorder(struct nodebt *); printf("nInvalid choice..")
void preorder (struct nodebt* );
void postorder ( struct nodebt *):
(GTU-New Syllabus w.e.f. academic year 19-20) (G3-01) Tech-Neo Publications...A SACHINSHAH
D a tSa
If new nodedata is
if(parent -> Rchild== x)
less, traverse to
left else to right parent > Rchild= NULL;
else
else
If the node to be deleted
parent X parent-> Rchild= x -> Lchild;
has two children
xSuce = x -> Rehild;
free(x);
while (xsucc -> Lehild != NULL) return
parent XSucC;
Xsuce = xsucc-> Lchild; void searchbt ( struct nodebt **root, int n, struct nodebt
**parent, struct nodebt **s, int *found )
X-nodedata =
xsucc -> nodedata struct nodebt "temp;
X XSucc;
temp= *root;
found = FALSE
W
Syllabus w.ef. academic year 19-20) (G3-01) Tech-Neo Publications ..A SACHIN SHAH Venture
(NonlinearData Structure). Page no (c
Data Structures (GTU-Sem. 3-Comp/T) 6-42
Output
parent NULL;
If the node to be L CiTREEL.exe
while ( temp!= NULL) deleted is found
Insert
nodedata n
Delete
if temp -> ==
3: In-or der
4: Pre-order
5: Post-order
found TRUE; 6: Exit choice : 1
Select your
s = temp;
returm
temp= temp -> Lehild; UQ. With a suitable example, explain steps for
else conversion of a general tree into a binary tree
temp temp -> Rehild; GTU- S-16,7 Marks
Traverse a binary A general tree can be changed into an equivalent
search tree in a
binary tree. This conversion process or technique
LDR (Left-Data
i
void inorder ( struct nodebt *sr) Right) fashion called the natural correspondence between general and
binary trees.
Traverse a binary
if(sr!= NULL) search tree in a
LRD (Left-Right
Data) fashion
postorder (sr -> Lehild ) ;
boolean
These trees can represent expressions that contain both Itcan be evaluated as
unary and binary operators.
5*4 2 0
Each node of a binary tree, and hence of a binary
100 - 20 = 80
expression tree, has zero, one, or two children
This restricted structure simplifies the processing of 20+80 100
expression trees.
Result is 100
6. The leaves of binary expression
a tree are operands, Example 2
such as constants or variable names, and the other
Consider another Tree as follows:
nodes contain operators.
7. These particular trees happen to be binary, because all
of the operations are binary, and although this is the
simplest case, it is possible for nodes to have more than
two children.
8. It is also possible for a node to have only one child, as
is the with the unary minus operator.
case
a-3 b (2X-Y)f3
Fig. 6.9.1 3 b 2 x-y
(a) (b)
GTU-New abus w.e.f. academic year19-20) (G3-01) Tech-Neo Publications.M.A SACHIN SHAH Venture
Data Structures
(Nonlinear Data Structure)..Page no. (6-44
(GTU-Sem. 3-Comp/lT)
(c)
(d)
(d)
GQ. Draw the tree structure for the following expressions
GO. structure for the following expressions
Draw the tree
2y 3z 4 4a +5b :(2a+5b (-7y (4 Marks)
6c d+4z) (4 Marke)
Stepwise construction of tree
x+2y+ 3z - 4- 4a
+5b +6c
(b)
2 a 5 b 7
(c)
O
6c 7d
(c) (d)
Ventun
w.e.f. academic year 19-20) (G3-01) Tech-Neo Publications...A SACHI SHAH
(GTU-New Syllabus
aStructures (GTU-Sem. 3-Comp/IT) (NonlinearData Structure)....Page no. (6-45)
Data
(e)
11a+7b+5c 3a+4b+8c GQ. Draw the tree structure for the following expression
b) (5x+7y)/(3x +5y +3z
(4 Marks)
8c
5x+7y (3x + 5y + 32)
11a 76 3a 4b
O-
(c)
d) 0-0
STU-tNew e.f. academic year 19-20) (G3-01) Tech-Neo Publications .MA SACHIN SHAH Venture
Data Structures (GTU-Sem. 3-Comp/IT (Nonlinear Data Structure)...Page
no. (6-46
UQ. Obtain the expression tree from the following post| a 6.10.1 AVL Tree / Height Balanced Trea
fix representation
ab+cde+**. GTU -S-14, 2 Marks - - - - -
Given expression is : ab+ cde + ** UQ. What is the meaning of height balancerd
tree ? How rebalancing is done in height balan ed
tree? OR Write short note on
: Height Balaneancec
Tree. OR Explain AVL tree with the help of
example
also show insertion and deletion with
the
help of an example. OR Explain AVL tree with
example. OR Explain insert and delete operations
AVLtrees with suitable examples. OR Discuss height
balance tree.
(GTU-New Syllabus w.e.f. academic year 19-20) (G3-01) Tech-Neo Publications ..A SACHIN SHAH V
Structures
TU-Sem. 3-Comp/IT) (Nonlinear Data Structure). Page no. (6-47)2
Data
Fig. 6.10.1
In F
tree is abinary search tree and in this tree Operations on AVL Tree
and ever node satisfies the balance factor -
each
is said to be AVL tree. Ua. Explain insert and delete operations in AVL trees
condition. So this
tree an
0 possible cases
All leaf nodes has BF as
ht(20) - ht(36)
25
Venture
= 3 - 3 =0
Tech-Neo
Publications...A SACHIN SHAH
(G3-01)
19-20)
New Syllabus w.e.f. academic year
Data Structures (GTU-Sem. 3-Comp/lT) (Nonlinear Data Structure)Page no. (6-4p
Insern 3, 2 and 1
AVL Rotations
MAR)-2
RR
MAY-1 MAY)0
2
MAR NOV MAY*
1
AUG AUG
NOV
APR) (APR (MAR
JAN AN
(MAY+2 MAR
o
AUG
CNOV AUG MAY
+1
DEC,JUN,FEB
MAR2 MAR
FEB
5. JUL
MAR2 JAN
DEC MAY1
MA DEC+1 MAR)0
AuG JAN VOV CAUu FEBJUN MAY
JUL)
New
Syllabus w.e.f. academic year 19-20) (G3-01) Tech-Neo Publications ...A SACHIN SHAH Venture
Data Structures (GTU-Sem. 3-Comp/lT) (Nonlinear Data Structure)...Page no
6-50)
Insert After insertions After Balancing
SEP
JAN JAN
6.10.2 B-Trees 6. Property #6: All the key values within a node musthe
in Ascending Order.
n a binary search tree, AVL Tree, etc., every node can
For example, B-Tree of Order 4 contains maximum
have only one value (key) and maximum of two
3 key values in a node and maximum 4 children for a
children but there is another type of search tree called
node.
B-Tree in which a node can store more than one values
(keys) and it can have more than two children.
Example
B-Tree was developed in the year of 1972 by Bayer 30 70
and McCreightwith the name Height Balanced m-way
Search Tree. Later it was named as B-Tree. 26 4050J
Definition of B-Tree
B-Tree can be defined as follows:
********** ************* * *****o
Fig.6.10.7: B-Tree
Ua. Define B-Tree. GTU-S-16, 1 Mark
Operations on B-Tree
B-Tree is a self-balanced search tree with multiple
keys in every node and more than two children for every The following operations are performed on a B-Tree...
The search operation is performed as follows.. (V) As discussed above, to insert a new key, we go down
Read the search element from the user from root to leaf. Before traversing down to a node, we
Slep
Compare. the search element with first key first check if the node is full. If the node is full, we
Step2 value of root node in the tree.
split it to create space.
1f both are matching. then display "Given node
Step3 found!!" and terminate the function.
Algorithm to Insert node in B-Tree
en4: If both are not matching, then check whether
search element is smaller or larger than that key (1) Initialize x as root.
value.
(2) While x is not leaf, do following
If search element is smaller, then continue the
Step5 search process in left subtree. (a) Find the child of x that is going to be traversed
If search element is larger, then compare with next. Let the child be y.
Step6
next key value in the node and repeat step
same (6) Ify is not full, change x to point to y.
3, 4, 5 and 6 until we found exact match or
comparison completed with last key value in a (c) Ify is full, split it and change x to point to one of
leaf node. the two parts of y. If k is smaller than mid key in
Step 7 If we completed with last key value in a leaf y, then set x as first part of y. Else second part of
node, then display "Element not found" and key from y to its
y. When we split y, we move a
terminate the function.
parent x.
2. Insertion Operation in B-Tree (3) The loop in step 2 stops when x is leaf. x must have
space for 1 extra key as we have been splitting all
i) A new key is always inserted at leaf node. Let the key nodes in advance. So simply insert k to x.
to be inserted be k. Like BST, we start from root and
Let us understand the algorithm with an example tree
traverse down till we reach a leaf node.
of minimum degree "t' as 3 and a sequence of integers
(i) Once we reach a leaf node, we insert the key in that
10, 20, 30, 40, 50, 60, 70, 80 and 90 in an initially
leaf node. Unlike BSTs, we have a predefined range on
empty B-Tree.
number of keys that a node can contain. So before
Initially root is NULL. Let us first insert 10.
inserting a key to node, we make sure that the node has
Insert 10
extra space. How to make sure that a node has space
available for key before the key is inserted? We use an
operation called splitChild() that is used to split a child
of a node. See the following diagram to understand Let us now insert 20, 30, 40 and 50. They all will be
split. inserted in root because maximum number of keys a
i) In the following diagram, child y of x is being split into node can accommodate is 2*t- 1 which is 5.
two nodes y and z. Insert 20,30,40 and 50
iv) Note that the splitChild operation moves a key up and
10 20 30 40 50
this is the reason B-Trees grow up unlike BSTs which
grow down. Let us nowinsert 60. Since root node is full, it will first
X
X split into two, then 60 will be inserted into the
50 100 200 Functionality of
splitChild)
50 80 100200 appropriate child.
Insert 60
S570 80 85 90
1 12 T3
10 20 40 50
T4 T5 T6 T1 T2 T3 T4 T5 T6
allowed to have fewer than the minimum number t-l1 and all of z into y, so that x loses both k and the
(b) F
deleted: case 1
LECTURE-9
Syllabus Covered
c, GM 2-3 Trees
or a single node
A 2-3 Tree is a null tree (zero nodes)
node tree with
the
one node) or a multiple
(d) G deleted: case 2c tree (only
following properties:
three children.
Each interior node has
two or
1
leaf has the same length.
Each path from the root to a
2.
PP
P: Pointer to first child.
Pointer to second
child.
Pa:
Pointer to third child.
P:
height Smallest key that is of second child.
descendent
(e') tree shrinks in k:
descendent ofthird
child.
Smallest key that is
k:
2. Leaf Node
E,L,PTX
12
Fig.6.10.9: Trees
Venture
Publications.A SACHIN SHAH
Tech-Neo
lumic vear 19-20) (63-01)
Data Structures (GTU-Sem. 3-Comp/lT) (Nonlinear Data structure) .... Page no
Up till now we have seen various linear data
6-54
(
(B) Example of weight balanced tree 2. The edges (connections) which connect the node
In the Fig. 6.10.10 we can observe that the letters (A) Definition of Graph
represent values of nodes while the numbers represent va. With figure, explain the following terms: Graph
weights of nodes.
The tree is ordered using values just like a general
GTU-S-15,S-16, 1Mark
--*°~-**---.
Vertices
before G.
Similarly, in the right sub-tree, N is the highest-
Mumbai
weighted node that comes after G.
INTRODUCTION TO GRAPH (FOR
6.11
Pune
READING)
1. We know that there are various data structures Fig. 6.11.2: Real life example ofGraph
linear and while
available which are mainly categorized as
In Fig. 6.11.2 the vertices represents the cilues
non-linear data structures. edges represents the road between two cities.
Venture
(GTU-New Syllabus w.e.f. academic year 19-20) (63-01) Tech-Neo Publications...A SACHIN SHAF
tures(GTU-Sem. 3-Comp/T) (Nonlinear Data Structure)..Page no. (6-55)
Dala
Stn
A, B, C, D & are
same.
and Nagpur
Pune, Mumbai, Nasik
ig. 6.11.1 -
Venture
Publications...A SACHIN SHAH
vertices. Tech-Neo
(63-01)
19-20)
New Syllabus w.e.f. ademic year