Download Non Linear Data Structure

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
62
Non Linear Data Structure- Trees
So far have studied linear type of data structure such as strings, arrays,
stacks, queues and list. In this topic we will study non-linear data structure
called a tree. Trees are used to represent the hierarchical relationship between
individual data items.
Trees: Trees are non-linear data structure that represents the hierarchical
relationship among individual data items.
E.g.: Family tree, records, an algebraic expression involving operators,
classification of books in library, students record at a typical university, the
hierarchy of institution.
General structure of tree: Consider the example of student records. The
student record have following field:
1) Name of student
2) Address of student
3) Course which student is doing
4) Exam appeared by student
5) Grade obtain by student.
Student
N ame
LName
Course
Mname
Fname
se
se
GradeA
CourseA
Exam A
Exam B
Address
CourseB
CourseC
Exam C
GradeC
Exam A
Exam B
Exam C
Student in at highest level 0(zero) is called as root node called as parent
node
- The name, address and course which are directly rooted to the parent node
are called as child node. The level of child node is one level below (lower)
than root node. Thus name, address, and course are level 1.
- The link between parent node to its child node is called as branch of tree.
- The root node of tree is ancestor of all nodes in the tree.
- A node that has no child is called a leaf. I.e. address, grade, exam are leafs.
- A tree consist of a number of subtrees in above fig. Node name is subset of
tree that represent itself a tree.
If out degree of every node is exactly to m or zero then number of nodes at level
I is mI-1 then the tree is called as full or complete m-ary tree.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
63
Definitions related to trees
Tree
A tree may be defined as a finite set T of one or more nodes such that
(a) there is specially designated node called the root of the tree,
(b) the remaining nodes (excluding root) are partitioned into n >= 0
disjoint sets T1 ,T2 ,T3 and each of these set is a tree in turn. The trees T1 ,T2
,T3 is called the sub-trees (or children) of the root. Note that the definition is
recursive as the children of the root of a tree are tree once again.
A tree consist of a collection of nodes which are connected by directed
arc. A tree contain a unique node called as Root. Root is the only node
which does not posses parent.
A node which points to other nodes is said to be parent of the nodes to
which it is pointing and these nodes in tree are called children of that node
Sibling
Nodes are called as sibling if
they have same parent.
Ancestor
A node is called as ancestor of
another node if it is parent of that
node or the parent of the some
other ancestor of that node. Root is
ancestor of every node in the tree.
Leaf
A node that has no child is called a leaf.
Analysis of the Above Tree
Root
Ravi
Children
Natasha , Kiran , Rahul
Children
Sunny , Honey
Children
Jaggi Sunil
Leaf or terminal
Sunny , Jaggi , Kush ,
node
Sibling
Sunny Honey
Parent
Natasha , Karan , Rahul
Ancestor
Ravi
of Ravi
of Natasha
of Rahul
Sunil
Jaggi , Sunil
Binary tree
It is special kind of tree which can be easily maintained in computer. A
binary tree T is defined as finite set of element called nodes such as
1) T is empty (called the NULL tree or empty tree)
2) T contain a distinguished node R called the root T, and the remaining
nodes of T form an ordered pair of disjoint binary tree T1 & T2.
In binary tree each element cannot have more than two child nodes i.e. has
two subtrees (null or not-null) called as left and right subtree.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
64
R
Root
T1
T2
Subtree
If in a tree out degree of every node is less
than or equal to m then such tree is called as mary tree. If m=2 then the tree is called as binary
tree.
If T does contain root R then two trees T1
and T2 are called respectively as left and right
subtree of R. If T1 is not empty then it is called as
left successor of R and T2 is not empty then it is
called as right successor of R.
Definition : A binary tree is a finite set of element which is either empty(null)
or consist of three adjacent subset. The first subset contain a single element
called the root of the tree and remaining two subsets are called as left subtree
and right subtree.
Analysis of above tree: Fig. Shows binary tree will letter from A through L. the
absence of a branch indicate an empty or null subtree.
1) Root of tree is denoted by A.
2) Left subtree is rooted as B.
3) Right subtree is rooted as C.
4) Left of subtree of root A nodes B,D,E
5) Right subtree of root A nodes C,F,G
Nodes with no successor is called as terminal node.
Depth
( height) and Level of Tree
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
65
Depth of the Tree
The Length of the longest path from root to any node is called as depth of
the tree.
The depth of the binary tree can also be defined as the maximum level of any
leaf in the tree.
Root A is at level 0 , Parent B , C are at level 1 ,D , E are at level 3. and so on.
Root is at level 0 and the level of any other node in the tree is one more than
the level of its parent.
Depth is one more than the level than the largest level number. In above
example level is 3 and depth is 4
Example
In above tree
1) The left subtree of the binary tree rooted a
C and right subtree of a binary tree rooted
at E both are empty.
2) Binary tree rooted at D,F,I and J have
empty right & left nodes.
3) D,F,I,J are called as leaf nodes.
4) The depth of a binary tree is the
maximum level of any leaf in the tree. This
equals the length of the longest path from
root to any leaf.
Example: Represent the following algebraic expressions by means of binary
tree.
E = (a-b) / ((c* d)+e)
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
66
Binary Tree
If the tree have maximum two children or node the it is called as binary tree.
A binary is the tree which is either empty or consist of a root node together
with two children , a left sub tree and a right sub tree.
Some Examples of Binary tree
Strictly binary tree
If every non-leaf node in a
binary tree has non-empty left
and right subtree.
The tree is termed as strictly
binary tree. Meaning that each
node in the tree will have either 0
or 2 nodes.
Complete binary tree
is
Consider any binary tree T. each node of T
can have at most two children. Hence at any
level r of T can have at most 2r nodes. The tree T
said to be complete if all its, levels except
possibly the last have the maximum number of
possible nodes and it at all the nodes at the last
level appears as far as possible.
Complete binary tree is defined as a binary
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
67
tree whose non leaf nodes have non empty left and right subtree and all leaves
are at the same level.
If a binary tree has the property that all elements in the left subtree of a
node n are less than the contents of n and all elements in the right subtree are
greater than the contents of n, such a binary tree is called the binary search
tree.
As the name suggests, they are very useful for searching an element just
as with binary search.
The total number of nodes in a completely binary tree of depth d is equal
to sum of the number of nodes at each level between 0 and d is given by
Nt = 20 + 21 + 22 ---- +2d
= 2*I
to determine the children and parents of node following formulae is used.
For any node K in any complete tree the
Left children = 2 * K.
Right children = 2 * K+1
The parent of K = K /2.
E.g.: the children of a node are 18 and 19 and its parent = 9/2 = 4. The depth
of dn of complete tree Tn with node n is given by
Dn = log2n +1
E.g. : In complete binary Tn and it has n= 1000 000 then depth = 21
Binary Search Tree
A binary search tree is a binary tree which is either empty or contains a node
whose key satisfies the conditions
(a) The key in the left child of a node (if any) preceeds the key in the
parent node.
(b) The key in the right child of a node (if any) succeeds the key in the
parent node.
(c) The left and right subtree of the root are again binary search trees.
Here, we are assuming that there are no duplicates which means that no
two entries in a binary search tree can have equal keys. To accommodate the
duplicates we can modify the definition so that
the key in the right child of a node can be
greater than or equal to the key in the parent
node.
Representation of binary tree in memory
Binary tree represented in memory by two
ways
1) Link representation (Link list using
pointer or arrays)
2) Sequential representation. (Array)
1) Link representation of binary tree:
In linked representation doubly link list is used
for tree storage.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
LPTR
Right child
Field
68
INFO RPTR
Data
Field
left child
Field
Each node contain three fields
1.Data field (info)
2.Left Child Field (Lptr)
3.Right Child Field (Rptr)
LPTR denotes the address of location of left subtree. RPTR denotes the address
of location of right subtree. Empty subtree(leaf) are represented by pointer
value NULL.
C language sample code
Struct bnode {
Char data ;
Struct bnode *left;
Struct bnode *right;
}
main()
{
struct bnode *T , *p, *q;
T = Null;
P = (struct *)malloc(sizeof(struct bnode) ;
p data = ‘A’ ;
p left = Null ;
pright = Null ;
T = p; P T now points to the root node of the tree /
q = T;
p = (struct *)malloc(sizeof( struct bnode) ;
p->data = ‘B’;
p->Ieft = p->right NULL;
q->Ieft = p;
q = p;
p = (struc bNode *)malloc(sizeof(struct Nocie));
p->data =
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
69
p->left = p->right = NULL;
q->left = p;
q = p;
p = (struc bNode *)malloc(sizeof(struct Nocie));
p->data = ‘G’
p->Ieft = p->right = NULL;
q->right = p;
q = T /* q now points to the node ‘B’ /
p = (struct *)malloc(sizeof( struct bnode) ;
p->data = ‘E’;
p->Ieft = p->right NULL;
q->right = p;
q = p;
p = (struc bNode *)malloc(sizeof(struct Nocie));
p->data = ‘H’
p->Ieft = p->right = NULL;
q->left = p;
q=p;
q=T
p = (struct *)malloc(sizeof( struct bnode) ;
p->data = ‘E’;
p->Ieft = p->right NULL;
q->right = p;
q = p;
p = (struc bNode *)malloc(sizeof(struct Nocie));
p->data = ‘F’
p->Ieft = p->right = NULL;
q->left = p;
q=p;
p = (struc bNode *)malloc(sizeof(struct Nocie));
p->data = ‘T’
p->Ieft = p->right = NULL;
q->right = p;
Representation of tree using Arrays ( two dimensional)
For tree representation three parallel arrays are used info, left and right. For
all each nodes N of T will correspond to a location K such that
1) INFO[k] contain data at the node N
2) LEFT[k] contain the location of the left child of node N.
3) RIGHT[k] contain the location of the right child of node N.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
70
4)
Root
5
Available
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
INFO
LEFT
RIGH
T
C
D
8
13
A
E
10
2
3
6
F
B
G
Example of Two dimensional Array representation of Link List
Example of One dimensional Array
representation of Link List
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
71
Advantages of link list storage
1) Most appropriate and efficient , more popular
2) Easy to insert, delete the element.
3) Can be easily altered
in size by using wasted memory space in
reformulation of a binary tree.
Disadvantages
1) Memory space is wasted in NULL pointer.
2) It is difficult to determine parent node from its given node.
3) It is more difficult to offer dynamic storage in language such as COBOL,
PASCAL, BASIC, FORTRAN etc.
2) Sequential representation of binary tree
The tree T can be represented using only single linear array such
representation in memory is called as sequential representation of T(tree).
This representation uses only a single linear array TREE as follows:
1) The root R of T is stored in Tree[1].
2) If node N occupies Tree[k] then its left child is stored in Tree[2* k] and its
right child is stored in tree[2*k+1].
3) NULL is used to indicate a empty tree.
A
B
D
C
E
G
H
F
I
1
2
3
4
5
6
7
8
9
10
11
12
13
A
B
C
D
E
F
G
H
14
15
16
17
18
19
20
21
22
23
24
25
26
I
Fig. Shows Sequential representation of the Tree
the depth of tree will require an array with approximately 2 d+1 elements.
E.g.: For a complete tree, it has 11 nodes & depth = 5 will require
25+1 = 26 = 64 elements.
Advantages of sequential representations:
1) Simple to determine parent node from a given child. If child is at location N
in array then parent node is at N/2 (integer division.)
2) It can be implemented easily in language which supports static allocation of
memories i.e. arrays like basic, Fortran, Pascal.
3) Searching and finding of element is easy.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
72
Disadvantages:
1) A large amount memory is wasted due to partially filled tree.
2) Excessive amount of processing time is required to move the considerable
data up & downwards.
3) Difficult to insert and delete element.
Example : Storage for the Equation E =(a-b)+(c*(d/e)) Sequential Storage :
+
a
X
b
c
/
d
e
1
2
3
4
5
6
7
8
“+”
“-”
“*”
“a”
“b”
“c”
“/”
9
10
11
12
13
14
15
16
“d”
“e”
Operation on tree
There are many operations performed on binary tree structure such
as
1) Traversal
2) Insertion
3) Deletion
4) Searching
5) Copying
Traversing the binary tree
There are three standard ways of traversing a binary tree T with root R.
These three algorithm are
1) Preorder
2) In-order
3) Post-order
1) Preorder Traversing
1) Process the root R
2) Traverse the left subtree of R in preorder
3) Traverse the right subtree of R in preorder.
In preorder traversing root R is processed
before the subtree are traversed. Hence this
algorithm is sometimes also called as Node-leftright or depth first order.
The preorder traversal of tree T is
ABCDEFGHIJK. Note that left subtree of root A
is traversed before the right subtree and both
are traversed after A.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
73
Example
Consider example
[(a+(b-c))] * [(d-e)/(f +g-h)] Infix
[a+(-bc)] * [(-de)/(+fg -h)]
[+a(-bc))] * [(-de)/(-+fgh)]
*[+a(-bc)]0/(-de)(-+fgh)]
*+a - bc / -de + - fgh  Prefix
Algorithm: Preorder traversal of binary tree
Algorithm RPREORDER(T)
T = Pointer variable which holds the address of node of binary
tree.
LPTR = Point to left subtree.
RPTR = Point to right subtree.
This algorithm traverse the tree in preorder in a Recursive manner
1) Process of root node
If ( T <> NULL) then
Write (Data(T))
Else
Write('Empty tree');
Return.
2) Process left subtree
If (LPTR(T) <>NULL) then
Call RPREORDR(LPTR(T))
3) Process right subtree
If (RPTR(T) <>NULL) then
Call RPREORDR(RPTR(T))
4) Finished
Return
Non-recursive or iterative methods for Preorder traversal:
General algorithm for preorder traversal binary tree
1) If the tree is empty then
Write ('Tree empty');
Return
Else
Place the pointer to the root of the tree on the stack
2) Repeat step 3 while stack is not empty
3) Pop the top pointer off the stack
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
74
Write the data associated to the node
If left subtree is not empty then
Stack the pointer to the right subtree
Set pointer to the right subtree
Procedure :
1) Initialize
If T = NULL then
Write ('Empty tree');
Return
Else
Top  0
Call push(s, top, T)
2) Process each stacked branch address
Repeat step 3 while top > 0
3) Get stored address and branch left
P pop(s, top)
Repeat while P<> NULL
Write (data(P))
If(RPTR(P))<> NULL) then
Call push(s, top, RPTR(P))
Store address of non empty right subtree
P  LPTR(P)  branch left
4) Finished
Return.
Inorder
1)
2)
3)
Traversing
Traverse the left subtree R in order
Process root R
Traverse the right subtree
A
B
D
C
E
F
G
In the algorithm the root R is processed in between the traversal of the
subtrees. Hence it is also called as left-node-right traversal.
The In-order traversal of T is DBEAFCG.
Algorithm: Inorder(Root) [Recursive algorithm]
Root  Contain the address of root node of tree
Left_PTR  Point to left subtree of tree.
Right_PTR  Point to right subtree of tree.
Info(root)  Data field.
1) Check for empty tree
If (root =NULL) then
Write('Empty tree');
Return.
2) Traverse the left subtree
If (Left_PTR(root)<>NULL) then
Call Rinorder(left_PTR(root))
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
75
3) Visit the root node
Write(info(root))
4) Traverse the right subtree
If(Right_PTR(root)<>NULL) then
Call Rinorder(Right_PTR(root))
5) Finished
Return
Non recursive Inorder traversal algorithm
Algorithm NRINORDER(root)
Root  Contain root address of binary tree.
Stack  Vector
Top  Top element pointer by top
PTR  Current element in tree
1) If (root = NULL) then
Write("empty tree')
Return
Else
Top  0
Call push(stack, top, root)
2) Repeat step 3 while stack is not empty
Repeat step 3 while (top>0)
3) If root <>NULL then
Call push(stack, top, root)
PTR  left_PTR(root)
Else
Call pop(stack, top, root)
/* No more left subtree */
If not empty then
Write(info(PTR))
PTR right_PTR(root)
4) Finished
Return
Postorder Traversal
1) Traverse the left subtree of R in Postorder
2) Traverse the right subtree of R in Postorder
3) Process of root R.
In this algorithm the root R is processed after subtree are traversed, it is
called as left-right-node traversal.
The traversal way is DFBFGCA i.e. Postorder traversal.
The algebraic expression
[a+(b-c)] * [(d-e)/(f + g-h)]
[a+(bc-)] * [(de-)/(fg+)-h)]
(abc-+ ) * [(de-)/ (fg + h) -]
(abc-+) * [de- fg + h-)] /
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
abc - + de - fg + h /*
Equation (a-b) + (c*(d/e))
ab - cde /* +
76
 Postfix
 Postfix
Recursive algorithm for Postorder traversal of a binary tree
Procedure RPOSTORDER(T)
T = tree starting address
1) Check for empty tree
If T = NULL then
Write ('Empty tree');
Return.
2) Process let subtree
If (LPTR(T) <>NULL) then
Call RPOSTORDER(LPTR(T))
3) Process the right subtree
If (RPTR(T)<>NULL) then
Call RPOSTORDER(RPTR(T))
4) Process the root node
Write (data(T))
5) Finished
Return.
Non-recursive algorithm for Postorder traversal
Algorithm NRPOSTORDER(root)
Root = Contain the root address of binary tree
Stack = Vector
Top = Top element of stack pointed by top
PTR = Current element in tree.
1) If the tree is empty then
Write('Empty tree'); return
Else
Initialize the stack & pointer value to root node of tree.
If (root =NULL) then
Write('Empty tree');
Return.
Else
PTR root
Top  0
2) Start an infinite loop that traverse in post order to repeat through
step 5
Repeat step 5while(true)
3) Repeat while pointer value is not NULL set the pointer value to
left subtree
Repeat while(PTR<> NULL)
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
77
Call push(stack, top, PTR)
PTR left_child(PTR)
4) Traverse left and right subtree of node repeat while top pointer on
stack is negative. Pop pointer off stack and write data associated
with positive value of this pointer
Repeat while(stack(top)<0)
PTR pop(stack, top)
Write(info(PTR))
If(top=0) then
Return
5) Set pointer value to right subtree of the value of the top of stack.
Stack is negative value of pointer to right subtree
PTR  right_child(stack(top))
Stack(top)  (-stack(top))
Insertion of node in binary tree
The binary tree can be created by using insertion operation or inserting
node repeatedly. The tree is created based on the information i.e. ordered
numerically or alphabetically.
Suppose for insertion, the binary tree exist. The insertion of new nodes
will always occur at leaf node of tree for insertion. There are two cases
1) Inserting in empty tree or root node of tree.
2) Inserting a node in empty tree.
Suppose an item of information is given. First we have locate the item in binary
tree and insert item as a new node in appropriate place in tree.
If binary tree is existing, then compare an inserting node(ITEM) with root
node(T)
a) If ITEM < T or alphabetically precedes to the root node and left subtree is
empty then add new node(ITEM) as a left leaf of tree
Else
The comparison is repeated with the left subtree root of node of the
tree
b) If newnode(ITEM) > than or
Alphabetically follows next alphabet to root node and right subtree in
empty then add a new node as a right leaf node of the tree.
Else
Comparison is repeated with right subtree root of the root node of tree.
Example to demonstrate the insertion into binary tree
An important operation is to create and maintain a binary search tree. While
inserting any node we have to take care that the resulting tree satisfies the
properties of binary search tree. A new node will always be inserted at its
proper position in the binary search tree as a leaf.
Before writing a routine for inserting a node, let us see how a binary tree ay be
created for the following input.
10, 15, 12, 7, 8, 18,6, 20.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
78
1. Initialize the tree ie to create an empty tree we must initialize root to
null. The first node will be inserted into the tree as a root as shown in
Fig. a
2. Since 15 is greater than 10, it must be inserted as the right child of the
root as shown in Fig.b
3. Now, 12 is larger than the root, it must go to the right subtree of the
root.Further it is smaller than 15 so it must be inserted as the left child
of the root as shown fig.c
4. Next number 7 is smaller than the root, so it must be inserted as the left
child of the root as shown in Fig.d
Fig. a Fig. b
Fig. c
Fig. d
Fig. e
Fig. f
5. Similarly 8, 18, 6 and 20 are inserted at the proper place as shown in
Fig. e, f, g and h.
6. This example shows that given the root of a binary search tree and a
value to be added to the tree, we must search for the proper place where
the new value can be inserted. We must also create a node for the new
value and finally, we will have to adjust pointers to insert the new node.
Fig.g
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
79
1. To
find
the
insertion place for
the new value we
initialize temporary
pointer p, which
point to the root
node.
2. This pointer p can
be changed to move
left
and
right
through the tree as
we did for searching
a particular value in
the tree. When p
becomes null, we
know that we have
found the insertion
Fig H
place as in Fig.
3. But once p becomes null, it is not possible to link the new node at this
position because there is no access to the node, p was pointing to just
before it became null.
4. As in Fig. p becomes null when we have found that 17 will be inserted at
the left of 18.
5. So we need a way to climb back into the tree so that we can access node
containing 18, in order to make its left pointer point to node 17.
We need a pointer which points to node
containing 18 when p becomes null. To achieve
this we have another pointer which must follow
p as it moves through the tree. When p
becomes null, this pointer will point to the leaf
node to which we will link the new node.
Once the insertion place is known we
must adjust the pointers of the new node. At
this point, we only have a pointer to the leaf
node to which the new node is to be linked.
We must find out whether the is to be
done on the left side or on the right side of the leaf node. To do that we must
compare the value to be inserted with the value in the leaf node. If the value in
the leaf node is greater we insert the new node as its left child, otherwise we
insert it as its right child.
What will happen, when the tree in which we are inserting a node is an
empty tree? We must treat it as a special case because when p equals null,
the second pointer trail will also be null and any reference to info (trail) will be
illegal. We can check the empty tree by checking if trail = null, we can then
change root to point to the new node, if tree was empty.
Example
Following six number are to be inserted in order into an empty binary search
tree.
40,60,50,33,55,11
Insert 40
Insert 60
Insert 53
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
Insert 33
80
Insert 55
Insert 11
To above tree add new node i.e. ITEM=18 to this tree
1) Compare ITEM=18 with root(40)
ITEM <root and left tree node is not leaf node
Hence compare ITEM=18 with subtree root node(15)
2) ITEM(18) is compared with root(15)
18 > 15 i.e. root > ITEM and right node is not leaf(25). Hence compare
ITEM(18) with right subtree(25)
3) ITEM=18, root(25)
18< 25 and left tree node is not leaf node. Hence again compare ITEM=18
with left subtree root node(16)
4) ITEM =18 with root node 16
ITEM(18)>root(16) and right subtree is a leaf node. Hence insert 18 as a
right child of the right subtree.
Tree
Insert 18
Algorithm- recursive
Inserting a node into binary tree
Root  Root node of binary tree
New  Inserted node
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
81
Algorithm BIN_TREE(root, new)
1) If (root = NULL) then
Add new node in binary node as a root node and exit.
2) Compare the inserted node
If(new<root) then
If left child of root is not empty then
Process left subtree and repeat step 2
Else
Add new in existing tree as left leaf and exit.
Else
If right_child of root is not empty then
Process right subtree and repeat step 2
Else
Add new in existing tree referred to as right leafs node and exit
3) Finished
Return.
Algorithm to insert new node in the tree
/* get a new node and make it a leaf */
getnode (q)
left (q) = null
right (q) = null
info (q) = x
/* Initialize the traversal pointers */
p = root
trail = null
/* search for the insertion place *
While p <> null do
begin
trail = p
if info (p) > x then
p = left (p)
else
p = right (p)
end
/* To adjust the pointers */
if trail = null then
root = q 1
/* attach it as a root in the empty tree */
else
if info (trail) > x then
left (trail) = q
else
right (trail) = q
C code for insertion in the tree
This insert algorithm can be coded into a C routine. There are two
parameters, which must be passed to this routine, one is the tree (root) and.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
82
the other is the value to be inserted (x). We will implement this algorithm by
allocating the nodes dynamically and by linking them using pointer variables.
We can code the insert algorithm as follows
tree insert (s, x)
int x;
tree *s;
{
tree * trail, * * q;
q = (tree ) malloc (size of (tree));
q  info = x;
q left = null;
q  right = null;
p= s;
trail = null;
while (p ! = null){
trail = p;
if (p info> x)
p = p  left;
else
p = p right
} // end oif while
if (trail == Null)
{
s=q;
return (s);
}
if (info (trail) > x)
left (trail) = q ;
else
right (trail) = q;
return (s);
Recursive Routine for Insertion in Tree
We have seen that to insert a node we must compare x with info (root). If
it is less, then x must be inserted into the left subtree otherwise, it must be
inserted into the right subtree.
This description suggests a recursive method where we compare the new
key with the one in the root and then we use exactly the same insertion
method either on the left subtree or on the right subtree. The base case is
inserting a node into an empty tree.
We can thus write another routine rinsert to insert a node recursively as
follows
tree rinsert (s, x)
intt x ;
tree *s;
{
if (!s)
{
s = (tree*) malloc (size of (tree));
sinfo=x
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
83
sleft = null
sright = null;
return (s);
}
if (x < s info)
S  left = rinsert ( S left)
else
if (x > s  info)
s  right = rinsert ( sright);
return (s);
}
Deleting a node from tree
An Another important function for maintaining binary search tree is to delete a
specific node from the tree. The method to delete a node depends on the
specific position of the node in the tree. The algorithm to delete a node can be
subdivided into different cases.
Case 1: Deleting the Leaf Node
If the node to be deleted is a
leaf, we only need to set the
appropriate link of its parent to nil,
and dispose of the node which is
deleted. For example to delete node
containing x in Fig. We have to set
the left child of its parent to nil.
Case 2 : Deleting the node with
one Child
If the node to be deleted has
only one child we cannot simply
make the link of the parent to nil as
we have done in the case of a leaf
because if we do so, we will loose all
of the descendants of the node which we are deleting from the tree. So we need
to adjust the link from the parent of the deleted node to point to the child of
the node we intend to delete. We can, then, dispose of the deleted node. For
example, to delete node containing x in Fig., where the left subtree of x is
empty, we simply make the link of parent of x point to y (the right child of x).
Case 3 :Deleting the node with two child
The most complicated
problem comes when we have
to delete a node with two
children. There is no way we
can make the parent of the
deleted node to point to both of
the children of the deleted
node. So, we attach one of the
subtrees of the nodes to be
deleted to the parent and the
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
84
then hang the other subtree onto the appropriate node of the first subtree. Let
us attach right subtree to the parent node and then hang the left subtree onto
a proper node of the right subtree. Since every key in the left subtree is less
than every key in the right subtree.
Example for Deletion of the node in the tree
The initial tree
Delete 13
Delete 6
Delete 15
Delete 10
Delete 11
In a binary search tree T, suppose ITEM of information is given. The
deletion algorithm to perform deletion operation has to find the location of the
node N and also its parent nodes location. The way node(N) is deleted depends
primarily on number of children of node(N). There are three different cases:
Summary for Deletion of the node From the Tree
Case 1: Deleting a leaf
If the node has no children, then tree node can be simply deleted by
replacing the location of node(N) in parent node by the NULL pointer and then
disposing the deleted node
Case 2: Deleting a node with a child.
If the deleted node has either a left or right child i.e. empty child then
non-empty child can be added to its grand parent node by replacing the
location of deleted node by the location of child(either left or right) of that node.
Here the pointer from a parent node skips over the deleted node and
point to the child of deleted node.
Case 3 Deleting a node with two child.
If the deleted node has both left and right child then we have to obtain
the inorder successor of the deleted node. Then node is deleted by first deleting
inorder successor and then replaced the deleted node by its inorder successor.
This is most complicated case in this case replace the node with the
node(child) i.e . closest in value to that node of one which is deleted. This node
can come from right or left of subtree.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
e.g. Consider binary search tree.
Initial Tree
Delete 20
Delete 52
85
Delete 41
Delete 45
Searching in Binary Tree
This is most important operation that is performed on the binary tree.The
searching starts with root node of the tree and process either left or right
subtree but this depends item to be searched.
The process of searching an item continues till the item is found, or the
empty left or right subtree is found.
If the process of searching terminates with empty right or left subtree
then the item is not present in tree.
A binary search tree is defined as a tree, which has root node is greater
than left subtree and is less than every node of right subtree.
Fig.a
Fig. b
Find Davis in binary search tree.
ITEM= Davis, Root = Harries
1) ITEM< Root processed to left child of Harries(root) i.e. Cohen.
2) ITEM> Cohen processed to right child of Cohen i.e. Green
3) ITEM<Green processed to left child of Green i.e. Davis
4) ITEM = Davis i.e. left child is Davis. Hence location of Davis is found in
tree.
Copying a Binary tree
This operation performed on binary tree is used to make duplicate copy
of a tree. The duplicate information is found in each node of the tree. This
operation produces two equivalent trees i.e. both trees have same structure or
shape with corresponding nodes containing the same information.
Algorithm: BIN_COPY(Root)
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
86
Root - Points to root node of the tree.
New - Points to the new binary tree.
Node - Points to the node of binary tree.
1) Check the NULL or empty tree.
If (Root =NULL) then
Write('Empty tree');
Return.
2) Create a new node for copy the tree pointed by pointer new
New  node
3) Copying the information field.
Info(new)  info(root)
4) Set the left and right pointer
Left(new)  copy(left(root))
Right(new)  copy(right(root))
5) Return to the address of new node of the copied tree. Return (new).
Form prefix of following equation:
(2x + y) (5*a - b)3
Equation is
(2*x+y) * (5*a-b)3
*- Multiplication
^ raised to power
(*2x+y) * (5*a-b)3
(+*2xy) * (*5a-b)3
(+*2xy) * (-*5ab)3
*(+*2xy) (-*5ab)^3
*(+*2xy) (^-*5ab3)
*+*2xy^ -*5ab3
Application
Application of trees are
1) Manipulation of arithmetic expression.
2) Symbol table construction.
3) Syntax analysis.
Symbol
.
.
+
*
Meaning
Constant
Variable
Add
Subtract
Multiply
Values
1
2
3
4
5
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
/
^
0
LN
Division
Exponent
Unary
Natural log
87
6
7
8
9
1) Represent following equation using tree and link list format.
1) (2x+y)3 + (2x3+y)
(2*x+y)^3 + (2*x^3+y)
^(+*2xy)3 + (+^*2x3y)
+^+*2xY3 + ^*2x3y
2) (2x+y)3 + (2x2+y)
(2*x+y)^3 + (2*x^2+y)
(*2x+y)^3 +(*2x^2+y)
(+*2xy)^3 + (^*2x2+y)
^(+*2xy3) + (+^*2x2y)
+^+*2xy3 + ^*2x2y
3) d/dx(u.v) = udv/dx + vdu/dx
= uv' + vu'
= u*v' + v*u'
= uv' + vu'
= +*uv' * vu'
4) (2x+y)(a-7b)3
(2*x+y)*(a-7*b)^3
(+*2xy)*(a-*7b)^3
(+*2xy)*(-a*7b)^3
(+*2xy)*^(-a*7b)3
*+*2xy^-a*7b3
5) (2a+b)(x-2y)3
(2*a+b)*(x-2*y)^3
(+*2ab)* (x-*2y)^3
(+*2ab)*(-x*2y)^3
(+*2ab)*^(-x*2y)3
*+*2ab^-x*2y3
*+*2ab^-x*2y3.
6) (5x-6y)*(6x2 + 8z)4
7) v(d/dx u) -u(d/dx v)/ v2
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
88
8) (a+x) *3.1+ln(-y)
+*(+ax)3.1n(-y)
9) D(u/v) = D(u)/v - (u* D(v))/v2
Terminologies used in tree
1) Tree: Tree is a non-linear data structure that represent the hierarchical
relationship among individual data items e.g. familiarize, algebraic
expression etc.
2) Root: It is a specially designated node whose in degree is zero is called root
or. The first subset contain a single element called the root of tree.
3) Node: A node stands for the item of information plus the branches to other
item.
4) Degree of tree: It is the maximum level of the nodes in the tree.
5) Depth of tree: It is defined to be the maximum level of any node in the tree.
6) Ancestor: The root node of tree is the ancestor of nodes in the tree.
7) Leaf node: A node that has no child nodes is called leaf node.
8) Child node: The nodes which are directly rooted to the parent node is called
as child node.
9) Siblings: Children's of the same parent are said to be siblings.
Traversing Algorithm Using Stack
Suppose a binary tree T is maintained in memory by some linked
representation
TREE(INFO, LEFT, RIGHT, ROOT)
Preorder Traversal
The preorder traversal algorithm uses a variable PTR (pointer) which will
contain the location of the node N currently being scanned. This is pictured in
Fig. where L(N) denotes the left child of node N and R(N) denotes the right
child The algorithm also uses an STACK, which will hold the addresses of
nodes for future processing.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
89
Algorithm
Initially push NULL onto STACK and then set PTR = ROOT. Then repeat the
following steps until PTR = NULL or equivalently while PTR  NULL
a. Proceed down the left-most path rooted at PTR, processing each node N
on the path and pushing each right child R(N), if any, onto STACK. The
traversing ends after a node N with no left child L(N) is processed. (Thus
PTR is updated using the assignment PTR : LEfT(PTR), and the traversing
stops when LEFT = NULL.)
b. [Back Tracking) Pop and assign to PTR the top element on STACK. If PTR
 NULL, then return to Step (a); otherwise Exit.
Example
Consider the binary tree T in Fig. Tree T is processed , showing the contents of
STACK at each step.
1. Initially push NULL onto STACK:
STACK: 0.
Then set PTR:= A, the root of T.
2. Proceed down the left-most path rooted at PTR A as follows:
i. Process A and push its right child C onto STACK:
1. STACK: 0, C.
ii. Process B. (There is no right child.)
iii. Process D and push its right child H onto STACK:
1. STACK: 0,C, H.
iv. Process G. (There is no right child.)
No other node is processed, since G has no left child.
3. ( Backtracking.) Pop the top element H from STACK, and set PTR : = H.
This leaves:
STACK: 0 C.
Since PTR  NULL return to Step (a) of the algorithm
4. Proceed down the left-most path rooted at PTR = H as follows:
v. Process H and push its right child K onto STACK:
STACK:0,C,K.
No other node is processed since H has no left child
5. (Backtracking.] Pop K from STACK, and set PTR := K. This leaves:
STACK: 0, C.
Since PTR  NULL, return to Step (a) of the algorithm.
6. Proceed down the left-most path rooted at PTR = K as follows:
Vi. Process K. (There is no right child.)
No other node is processed, since K has no left child.
7. (Back Tracking) Pop C from STACK, and set PTR:=C. This leaves:
STACK: 0.
Since PTR  NULL, return to Step (a) of the algorithm.
8. Proceed down the leftmost path rooted at PTR = C as follows:
vii.Process C and push its right child F onto STACK:
STACK: 0, F.
viii.Process E. (There is no right child)
9. (Back Tracking) Pop F from STACK, and set PTR: = F. This leaves:
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
90
STACK: 0.
Since PTR  NULL, return to Step (a) of the algorithm.
10.
Proceed down th kit most path rooted at PT K = F as follows
(ix) Process F. (There is no right child.)
No other node is processed, since F has no left child.
11.
(Backtracking.] Pop the top element NULL from STACK, and set PTR
:= NULL. Since PTR = NULL,the algorithm is completed.
As seen from steps 2,4,6,8 and 10
the node processed in the order
A,B,D,G,H,K,C,E,F. This is the required preorder traversing of T ( tree)
Inorder Traversal
The inorder traversal algorithm uses a variable PTR (pointer) which will
contain the location of the node N currently being scanned and an array
STACK which will hold the address of nodes for the future processing. In this
way this algorithm a node is processed only when it is popped from the stack.
Algorithm
Initially push NULL onto STACK and then set PTR := ROOT. Then repeat the
following steps until NULL is popped from STACK.
a. Proceed down the left-most path rooted at PTR, pushing each node N
onto STACK and stopping when a node N with no left child is pushed
onto STACK.
b. Pop and process the nodes on STACK. If NULL is popped, then Exit If a
node N with a right child R(N) is processed, set PTR := R(N) (by assigning
PTR = RIGHT(PTR)) and return to Step (a)
We emphasize that a node N is processed only when it is popped from STACK.
1. 1 Initially push NULL onto STACK:
STACK: 0.
Then PTR := A, the root of T.
2. Proceed down the left-most path rooted at PTR A, pushing the nodes A B,
D, 0 and K onto STACK:
STACK: 0, A, B, D, 0, K.
(No other node is pushed onto STACK, since K has no left child.)
3. (BrackTracking ) The nodes K, G and D are popped and processed,
leaving:
STACK: 0, A. B.
(We stop the processing at D, since D has a right child.) Then set PTR
:= H, the right child of D.
4. Proceed down the left-most path rooted at PTR := H, pushing the nodes H
and L onto STACK:
STACK: 0, A, B, H, L.
(No other node is pushed onto STACK, since L has no left child.)
5. (Back Tracking) The nodes L and H are popped and processed, leaving:
STACK: 0, A, B.
(We stop the processing at I since I- has a right child.) Then set PTR:=
M, the right child of H.
6. Proceed down the left-most path rooted at PTR = M, pushing node M onto
STACK:
STACK;0,A,B,M.
(No other node is pushed onto STACK, since M has no left child.)
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
91
7. (BackTracking) The nodes M, B and A are popped and processed,
leaving:
STACK; 0.
(No other element of STACK is popped, since A does have a right
child.) Set PTR:=C, the right child of A.)
8. Proceed down the left-most path rooted at PTR C, pushing the nodes C
and F onto STACK:
STACK: 0, C, E.
9. [BackTracking) Node E is popped and processed. Since E has no right
child node C is popped and processed. Since C has no right child, the
next element, NULL, is popped from STACK.
The algorithm is now finished since NULL is popped from STACK As seen from
Steps 3 5 7 and 9, the nodes are processed in the order K, G, D, L, H, M, B, A,
E, C. This is the required inorder traversal of the binary tree T
Post Order Traversal
The postorder traversal algorithm is more complicated than the preceding two
algorithms, because here we may have to save a node N in two different
situations. We distinguish between the two cases by pushing either N or its
negative, -N, onto STACK. (In actual practice, the location of N is pushed onto
STACK, so -N has the obvious meaning.) Again, a variable PTR (pointer) is used
which contains the location of the node N that is currently being scanned.
Algorithm:
Initially push NULL onto STACK and then set PTR:= ROOT. Then repeat the
following steps until NULL is popped from STACK.
a. Proceed down the left-most path rooted at PTR. At each node N of the
path, push N onto STACK and, if N has a right child R(N), push -R(N)
onto STACK.
b. (Back Tracking) Pop and process positive nodes on STACK. if NULL is
popped, then Exit. If a negative node is popped, i.e , if PTR = —N (or some
node N, set PTR = N (by assigning PTR : =-PTR) and return to Step (a).
We emphasize that a node N is processed only when it is popped from STACK
and it is positive
Example
Consider again the binary tree T in fig. We simulate the above algorithm
with T, showing the contents of STACK.
1. Initially, push NULL onto STACK and set PTR:=A, the root ofT:
STACK: 0.
2. Proceed down the left-most path rooted at PTR A, pushing the nodes A, B,
D, G and K onto STACK. Furthermore, since A has a right child C, push C onto STACK after A but before B, and since D has a right child H push H onto STACK after D but before C This yields
STACK: 0, A, -C, B, U, -H, G, K.
3. (Back Tracking) Pop and process K and pop and process G Since -H is
negative, only pop -H This leaves
STACK:-O, A, -C, B, D.
Now PTR -H. Reset PTR = H and return to Step (a).
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
92
4. Proceed down the left-most path rooted at PTR = H. First push H onto
STACK. Since H has a right child M, push -M onto STACK after H. Last,
push L onto STACK. This gives:
STACK: 0, A, -C, B, U, II, -M, L.
5. (Back Tracking)Pop and process L, but only pop -M . This leaves
STACK: 0, A, -C, B, D, H.
Now PTR -M. Reset PTR = M and return to Step (a).
6. Proceed down the left-most path rooted at PTR = M. Now, only M is
pushed onto STACK. This yields:
STACK: 0, A, -C,B, D, H, M.
7. (BackTracking) Pop and process M, H, D and B, but only pop -C. This
leaves:
STACK: 0, A.
Now PTR = -C. Reset PTR = C, and return to Step (a).
8. Proceed down the left-most path rooted at PTR = C. First C is pushed onto
STACK and then E yielding:
STACK: 0, A, C, E.
9. (BackTracking) Pop and process E, C and A. When NULL is popped,
STACK is empty and the algorithm is completed
As seen from Steps 3,5,7 and 9, the nodes are processed in the order K, G, L,
M, H, U, B, E, C, A. This is the required post order traversal of the binary tree
T.
Linked Representation of a Binary Tree:
In linked representation tree nodes may be implemented as array
elements as an allocation of a dynamic variable. Each node in a binary may
have 2-child field contain the location of left and right child subtree and one
data field containing specific information about the node itself when a node
has no children, the corresponding pointer field are NULL.
Fig. represent a linked representation of binary of fig. The left and right fields
are pointer to (memory address) of the left child and right child of node. Notice
that art operand in the tree is always a leaf node and ROOT is pointer that
contain the address of root node of binary tree.
Advantages
1. It is most efficient and
more appropriate.
2. It is more popular for
inserting
and
deleting
elements.
3. It can easily alter the size
by using wasted memory
space in reformulation of a
binary tree.
Disadvantages
1. Memory space is wasted in NULL pointers in figure (c). there are 10 NULL
pointers
2. It is difficult to determine parent node from its given node.
3. It is more difficult to offer dynamic storage techniques in language such as
COBOL BASIC and FORTRAN.
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
93
Height Balance Tree
Binary search method explained earlier can be explained in terms of binary
trees. The algorithm for Binary search is similar to searching in Binary trees.
Average length of search for Binary tree representation is 0 (log 2N).However
worst case search time is 0 (n). If the binary tree is vertical as shown in
following figures.
In worst case, search time is same as sequential search. Using balanced
binary trees, the worst case search time can be reduced to 0 ((log 2N). Height
balanced trees is an example of balanced binary, tree. In Height balanced
trees, we attempt to keep each leaf node the same distance from the root.
To prevent a tree unbalance, balance indicator is associated with each
node in the tree. The indicator will contain one of the tree values left (L), right
(R) or balance (B) based on following criteria.
Left : If the longest path in node’s left subtree is one longer than the longest
path of the right subtree, node is called left heavy (L).
Right : If the longest path in node’s right subtree is one longer than the
longest path of the left subtree, node is called right heavy (R).
Balance : If longest path
in both subtree of a node
are equal, the node is
called balanced (B).
In height balanced
tree, each node must be
in one of the three states
mentioned above.
Insertion
or
deletion
operation carried out on the balanced tree may sometime make them
unbalanced. Rebalancing is carried out throught in such tree.
Weight Balanced Trees
If probability of access of a particular node in linked list is already
known, the nodes can be arranged according to decreasing order of probability
of occurrence. Thus the node with highest probability of access is kept at the
front of the linked list and node with least probability of access is kept at the
rear of the linked list. This would improve the search time.
If probability of access of a particular key is unknown, the probability of
access of key is maintained dynamically to improve the search performance.
Weight
balanced
tree is another type of
balanced tree in which
an attempt is made to
reduce average length of
search.
Each
node
of
weight-balanced
tree
contains name of node
Data structures – SYCM – 2005-06 / 9422092905 / 2540017
94
along with the number.
The number represents the number of times the node has been visited so far. It
is assumed that keys accessed in the past are likely to be accessed in the same
manner in future.
The fig. shows weight balanced tree ordered lexically. The number
represents, number
of times the node has been visited so far.
Average length of search of T is given by
ALOS (T1) =  Pidi
Where Pi = Probability of access
Di = Depth of node (i + 1) in binary tree.
For above tree, total no. of times tree is accessed
= 2+4+4+5+3+6+1 =25.
Thus Probability of Node manish is accessed = 2/25.
Similarly other probabilities can be calculated.
ALOS (Ti) = 2/25 x 1 + 4/25 x 2 + 4/25 x 3 + 5/25 x 3 + 3/25 x 2 + 6/25 x 3 +
1/25 x 3 = =2.56
The tree T is reorganised as T based on the probabilities of accessing each
node as in fig
The average length of
search for T can be
calculated as
ALOS(T2) =
6/25x1+5/25x2+4/25x3+4
/25x4+2/25x3+3/25x2+1/
25x3 =2.36
For very large tables, the
saving would be significant.
To
construct
weight
balanced, tree, following rules are followed for placement of a node
into tree.
1. The first node of tree is the node with highest activity count.
2. Left subtree is composed of nodes with values lexically less than the first
node.
3. Right subtree is composed of nodes with values lexically greater than the
first node.
/* edited on 6th 01 2005
*/