Professional Documents
Culture Documents
Sc
TREES
A tree is a data structure in which element is attached to one (or) more
elements directly beneath it. The connections between elements are called branches.
Trees are called inverted trees because they are normally drawn with the root at the
top. Trees are used to represent hierarchical relationships between data elements.
Figure below shows the tree structure.
Level
A 1
B C D 2
E F G H I J 3
K 4
A tree consists of one (or) more nodes (or) vertices, which are connected by branches
(or) edges.
Binary Trees: Binary Tree is a data structure in which, each node consists maximum
of two child nodes. A binary tree is defined as a finite set of elements that is either
empty (or) is portioned into three disjoint subsets. The first subset contains a single
element called the root of the tree. The other two subsets are themselves binary trees,
called the left and right sub-trees of the original tree.
Right subtree 1
Left subtree A
2
C
B
F G 3
D E
I J4 4
H
The nodes a tree are connected by branches (or) edges. Each node can have
the data and its associated information. A binary tree has a single root, called root
node, which is shown at the top of the tree. Each node except the root has exactly
one node above it, called parent. The nodes just below a node are called its children
i.e., child nodes are one level lower than the parent node.
A node which does not have any parent called root node.
A node which does not have any child is called leaf (or) terminal node.
A branch (or) edge in a tree is a link between a parent and its child.
The child nodes of same parent are said to be siblings. All nodes at a
particular level are said to be same generation.
A path in a tree is a list of distinct nodes in which successive nodes are
connected by branches in the trees. The length of a particular path is the number of
branches in that path.
The degree of a node of a tree is the number of children of that node. The
degree of leaves is zero.
The height (or) depth of a tree is the length of the largest path from the root to
leaf node. (or) the height of a tree is maximum level of any node in tree.
and
B B
A binary tree with three nodes :
If there are three nodes in binary tree, five different binary trees can be
obtained.
A A A A
A
B B B
B
B C
C C
( i) C C
(ii) (iii) (iv) (v)
A binary tree is called a strictly binary tree if each node in a binary tree will
have either ‘0’ (or) ‘2’ children. Fig. Below shows a strictly binary tree.
B C
D E
F G
Complete binary tree : A complete binary tree can be defined as a binary tree
whose non-leaf nodes have non-empty left and right subtree and all leaf nodes are at
the same level. Fig. Below shows the complete binary tree.
B C
D E F G
i) the data values of all the nodes in the left sub-tree are less than
that of the root.
ii) The data values of all the nodes in the right sub-tree are greater
than that of the root.
iii) The left and right sub-trees are themselves binary search trees.
Duplicate items are not allowed in a binary search tree. Fig. Below shows the binary
search tree.
20
15 35
14 17 23 40
16 26
The maximum size that is required for an array to store a tree is 2 d -1, where
‘d’ is the depth of the tree.
The maximum number of nodes on level i of binary tree is 2i-1
The maximum number of nodes in a binary tree of depth k is 2k -1
Ex : Consider the following binary tree.
B C
D E F G
H I J
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Advantages :
Disadvantages :
1. The arrays are linear static data structures, thus, growing and
shrinking of a tree can not efficiently managed.
2. A number of locations are not filled and thus wastage of memory.
Binary Tree :
A
B C
D E F G
H I J
Linked Representation :
A
B C
D E F G
H I J
Disadvantages :
1. Wasted space due to NULL pointers at all leaf nodes.
2. Finding a parent of a particular node is difficult.
Tree Traversals
Traversing a tree means processing it so that each node is visited exactly once.
A binary tree can be traversed in number of ways. The most common tree traversal
methods are
1. Inorder traversal
2. Preorder traversal
3. Post order traversal
Inorder traversal:
preorder traversal:
B C
E F G
D
Preorder: A B D E C F G
Inorder: D B E A F C G
Postorder: D E B F G C A
B D
C E
G
Preorder: A B C D E F G
Inorder: C B A E F D G
Postorder: C B F E G D A
Ex:
A *
B
C
Preorder: +A*BC
Inorder: A+B*C
Postorder: ABC*+
Ex: Draw the tree whose preorder and inorder lists are
Preorder : A B D C E F
Inorder : B D A E C F
B C
E
F
D
Post Order : D B E F C A
Inorder : D B A C E
Preorder: A B D C E
Give the post order traversal of tree.
B C
D E
Post order : D B E C A
1. preorder traversal
preorder(T)
{
if(T==NULL) then
{
print “Tree is empty”
return
}
if(T!=NULL)
{
print Info(T)
preorder(leftptr)
preorder(rightptr)
}
}
where ‘T’ is the pointer variable contains address of root node.
leftptrleft pointer of the node
rightptrright pointer of the node.
2. Inorder Traversal
inorder(T)
{
if T=NULL then
{
print “Tree is empty”
return
}
if(T!=NULL)
{
inorder(leftptr)
print info(T)
inorder(rightptr)
}
}
3. Postorder Traversal
posrorder(T)
{
if T=NULL then
{
print “Tree is empty”
return
}
if(T!=NULL)
{
postorder(leftptr)
postorder(rightptr)
print information(T)
}
}
Tree traversal algorithms(Non-Recursive):
Preorder:
PREORDER(INFO,LEFT,RIGHT,ROOT)
1. Initially push NULL to stack and initialize PTR (PTR is a pointer
variable contain address of node , current being scanned)
Set TOP=-1, STACK[TOP]=NULL , PTR=ROOT
2. Repeat steps 3 to 5 while PTRNULL
3. process INFO[PTR]
4. if RIGHT[PTR]NULL then
set TOP=TOP+1 and STACK[TOP]=RIGHT[PTR]
[end of if construct]
5. if LEFT[PTR]NULL then
set PTR=LEFT[PTR]
else
set PTR=STACK[TOP]
TOP=TOP-1
[end of if ]
[ end of loop]
6. exit
Inorder:
INORDER(INFO,LEFT,RIGHT,ROOT)
a. Initially push NULL to stack and initialize PTR
(PTR is a pointer variable contain address of node , current being
scanned)
Set TOP=-1 STACK[TOP]=NULL PTR=ROOT
b. repeat while PTRNULL
i. set TOP=TOP+1 and STACK[TOP]=PTR
ii. set PTR=LEFT[PTR]
[end of while loop]
c. set PTR=STACK[TOP] and TOP=TOP-1
d. repeat steps 5 to 7 while PTRNULL
e. process INFO[PTR]
f. if RIGHT[PTR]NULL then
i. set PTR=RIGHT[PTR]
ii. GO TO step2
[end of if structure]
g. set PTR=STACK[TOP]
TOP=TOP-1
[end of while loop]
h. exit
Postorder:
POSTORDER(INFO,LEFT,RIGHT,ROOT)
a. Initially push NULL to stack and initialize PTR
(PTR is a pointer variable contain address of node , current being
scanned)
Set TOP=-1, STACK[TOP]=NULL, PTR=ROOT
b. repeat steps 3 to 5 while PTRNULL
c. set TOP=TOP+1 and STACK[TOP]=PTR
d. push right child on stack with negative mark
if RIGHT[PTR]NULL then
set TOP=TOP+1
STACK[TOP]=-RIGHT[PTR]
[end of if structure]
e. set PTR=LEFT[PTR]
[end of while loop]
f. set PTR=STACK[TOP] (pop node from stack)
TOP=TOP-1
g. if PTR is +ve
Repeat while PTR>0
a) Apply PROCESS to INFO[PTR]
b) set PTR=STACK[TOP] (POP node from stack)
TOP=TOP-1
[ end of while ]
h. if PTR is –ve
if PTR< 0 then
i. set PTR=-PTR
ii. GOTO step2
i. exit
Construction of BST
50 40 45 60 65 58 70
2. Take the next element, start from root node and consider it as current node. If
the element is less than current node traverse towards left subtree, and mark
new left node as current node.
3. If the element is greater than current node traverse towards right subtree, and
mark new right node as current node.
4. Repeat the steps 2 and 3 at each node, until a leaf node is found.
5. At leaf node insert the new element
i) to left if element value is less than leaf node value
ii) to right if element value is greater than leaf node value
iii) if element is equal to leaf node value neglect the element.
Repeat the above process until tree is constructed with required no. of elements.
50
4 6
0 0
4
6
5 5 5
8
7
0
50
4 7
5 0
4 4
0 8 6
0
5
0
4
5 7
0
4 4 7
0 8 6 2
0
4. If leaf node value is less than current node, value travel towards left
subtree of current node, and mark the new left node as current node.
5. If leaf node value is greater than current node value travel towards right
subtree of current node and mark the new right node as current node.
6. Repeat step 3 , 4 and 5 until parent node of leaf node is reached.
7. Delete leaf node and mark corresponding address pointer of parent node
as NULL.
7
0
5
0 8
0
4 6 9
0 0 7 0
5
7
0
5
0 8
0
4 6
0 0 7
Department of Computer Science 5 15 A.S.N. Degree College, Tenali
Data Structures Using Java UNIT-III II Year IV Semester B.Sc
If the node to be deleted has only one child, we need to adjust the link from
the parent of the deleted node to the child of the node we intend to delete.
7
0
5
0 8
0
4 6
0 0 7
5
7
0
6
0 7
5
5 6
0 5
If a node with two children has to be deleted, attach one of the subtrees
of the nodes to be deleted to the parent and then hang the other subtree onto the
appropriate node of the first subtree.
Ex: Attach right subtree to the parent node and then hang the left subtree onto a
proper node of the right subtree.
Ex: Consider the following tree
7
0
6
0 8
0
5 6 9
0 5 7 0
5
55 68
7
0
6
8
0
0
5 6 9
7
0 8 0
5
5
5
Department of Computer Science 17 A.S.N. Degree College, Tenali
Data Structures Using Java UNIT-III II Year IV Semester B.Sc
7
0
6
0 9
0
5 6
0 8 7
5
55
Heap Tree:
Heap data structure is a specialized binary tree based data structure. Heap is a binary
tree with special characteristics. In a heap data structure, nodes are arranged based
on thier value. A heap data structure, some time called as Binary Heap.
There are two types of heap data structures and they are
1.Max Heap
2.Min Heap
Every heap data structure has the Some properties...
Property #1 (Ordering): Nodes must be arranged in a order according to values
based on Max heap or Min heap.
Property #2 (Structural): All levels in a heap must full, except last level and nodes
must be filled from left to right strictly.
Max heap is a specialized full binary tree in which every parent node contains
greater or equal value than its child nodes. And last leaf node can be alone.
1. Insertion
2. Deletion
EX:
Consider the MAX heap tree is preformed as
Step 1: Insert the newNode with value 85 as last leaf from left to right. That means
newNode is added as a right child of node with value 75. After adding max heap is
as follows...
Step 2: Compare newNode value (85) with its Parent node value (75). That means 85
> 75
Step 4: Now, again compare newNode value (85) with its parent nede value (89).
Here, newNode value (85) is smaller than its parent node value (89). So, we stop
insertion process. Finally, max heap after insetion of a new node with value 85 is as
follows...
EX:
Consider the MAX heap tree is preformed as Delete root node (90) from the max
heap.
Step 1: Swap the root node (90) with last node 75 in max heap After swapping
Step 2: Delete last node. Here node with value 90. After deleting node with value 90
from heap
Step 3: Compare root node (75) with its left child (89).
Here as, root value (75) is smaller than its left child value (89). So, compare left child
(89) with its right sibling (70).
Step 4: Here, left child value (89) is larger than its right sibling (70), So, swap root
(75) with left child (89).
Here, node with value 75 is larger than its left child. So, we compare node with value
75 is compared with its right child 85.
Step 6: Here, node with value 75 is smaller than its right child (85). So, we swap both
of them. After swapping max heap
Step 7: Now, compare node with value 75 with its left child (15).
Here, node with value 75 is larger than its left child (15) and it does not have right
child. So we stop the process.
Finally, max heap after deleting root node (90) is as
Priority Queue: A priority queue is an abstract concept like "a list" or "a map"; just as
a list can be implemented with a linked list or an array, a priority queue can be
implemented with a heap or a variety of other methods.
Order statistics: The Heap data structure can be used to efficiently find the kth
smallest (or largest) element in an array.