TREE
A Tree is an acyclic connected graph. A tree contains no loops or cycles. Tree is used in application such as compiler construction, database design, windows, operating system programs etc. This structure is mainly used to represents data containing a hierarchical relationship between elements.
  <document classification>
TREE
Definition: A data structure accessed beginning at the root node. Each node is either a leaf or an internal node. An internal node has one or more child nodes and is called the parent of its child nodes. All children of the same node are siblings., the root is usually depicted at the top of the structure, and the leaves are depicted at the bottom
  <document classification>
  <document classification>
Root
A
Level 0 Level 1
B
C
D
E
G F Internal Vertex
Level 2
Level 3
J H I
  <document classification>
Level 4
K L Leaf/Pendant
.
Edge: is a connection between two vertices.Basic Terminology about Trees
Node: A node stands for the item of information plus the branches to other items. Consider the fig it has 13 nodes. Degree: The number of subtrees of a node is called its degree. In Fig the degree of node A is 3.
  <document classification>
.
  <document classification>
. I is the 'parent' of its children. L are 'Leaf' nodes. K. H. E. G are Siblings. Siblings: Children of the same parent are called 'Siblings'. other nodes of Tree are called 'Non Leaf' nodes. C.
Leaf or Terminal Nodes: Nodes that have degree
one is called leaf or Terminal nodes. F. In Fig B. Children: The roots of the subtrees of a node I
are called the children of node I. E.
  <document classification>
.Length: the length of a path is the number
of edges on the path.
Ancestor & Descendant:.the depth of any node n is the
length of the path from root to n.
Depth:.if there is a
path from m to n then m is ancestor of n & n is a descendant of m.
  <document classification>
.
Height : The height of a node n is
defined as the longest path from node n to leaf.
Forest: A set of trees is called a forest.
called the left and right subtrees of the original tree. A node of a Binary Tree can have at most two Branches. A left or right subtree can be empty. The first subset contains a single element called the Root of the tree. The other two subsets are themselves Binary Trees.
  <document classification>
.Binary Trees
A Binary Tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets.
  <document classification>
.
2.STRICTLY BINARY TREE
If every internal node (non terminal node) has its non empty left and right children then it is called strictly binary tree. Here internal node 1. 5 has two non empty left and right child so it is strictly Binary tree 1 2 3
4
5
  <document classification>
6
7
.
Left child and right child of any node will be at position 2N and 2N+1. Parent of any node N will be floor (N/2)
  <document classification>
. The main advantage with this structure is that we can easily find the left and right child of any node and parent of any child.COMPLETE BINARY TREE
A complete binary tree is a binary tree where all the nodes have both children except last node.
Parent of I will be floor(9/2)=4
1
Left and right child of D will 2*4=8 H and 2*4+1=9 2
B 4 D 8 H I 9 10 J E 5
A
3
C
6
F 11 K G
7
12
L
13
M
14
N
15
O
  <document classification>
COMPLETE BINARY TREE
.
second and third members are structure pointers which point to same structure as for tree node
Struct node{ Char data. In which we take three members.
node *rchild.
Node *lchild.}
  <document classification>
. First member for data.Representation of Binary Tree
Linked Representation As in the linked list we take the structure for tree.
Postorder (LRR) Left Right Root
  <document classification>
.Preorder (RLR) Root Left Right 2.Traversing in Binary Tree
1.Inorder (LRR) Left Root Right
3.

+
*
A
*
D
E
B
  <document classification>
C
.Consider the fig given below.
and Then traverse the right subtree.Inorder
It follows the general strategy of LEFTROOTRIGHT. Then visit the root node of Tree. In this traversal if Tree is not empty we first traverse the left subtree.
  <document classification>
.
A. current T becomes rooted at A.This is an example of an expression tree for (A+B*C) – (D *E). We visit root i. Since left (T) is empty. Since left (T) is not empty. current T becomes rooted at +.
  <document classification>
. at the start is rooted at ‘ – ‘. Tree 1. Since left (T) is not empty.e.
‘+’. B. We visit its root i. We visit its root i. therefore.e.e.We access T root i.
Since left (T) is not empty. check for right (T) which is empty.
We now perform inorder traversal of right (T).
  <document classification>
. Current T becomes rooted at ‘B’ since left (T) is empty. ‘*’.e. we move back to parent tree.
Current T becomes rooted at *.
Therefore the complete listing is A+B*C – D*E We may note that expression is infix notation. We visit T’s root i. The following is a C procedure for inorder traversal of a binary tree.Now inorder traversal of right (T) is performed.
  <document classification>
. which would give us ‘C’.e ‘D’ and perform inorder traversal of right (T) which would give us ‘*’ and ‘E’.
s>data). }
  <document classification>
}
. inorder (s>right). printf (“%d”.void inorder (struct btree *s) { if(s != NULL) { inorder (s>left). } else { return.
A preorder traversal of the tree given in inorder traversal would yield.
+A*BC*DE
  <document classification>
. Traverse the left Subtree in Preorder .Preorder
It is a Root – Left – Right strategy.
Visit the root. Traverse the right Subtree in Preorder.
printf(“%d”.Implementation using C Procedure
  <document classification>
void postorder (struct btree *s) { if(s != NULL) { postorder (s > left). }
. s > data). postorder(s > right). } else return.
a postorder traversal of the tree give in inorder traversal would be
  <document classification>
ABC*+DE*
.
For exp:.Postorder
It is a LeftRightRoot strategy. Traverse the right Subtree in Postorder.e. Traverse the Left Subtree in Postorder. i. Visit the root.
Preorder(ptr>lchild).}}
  <document classification>
.ptr>data).Preorder(ptr)
Struct node *ptr.
Preorder Traversal
{
If(ptr!=NULL)
{
Printf(“%c”. Preorder(ptr>rchild).
inorder(ptr>rchild).inorder(ptr)
Struct node *ptr.}}
  <document classification>
.
Inorder Traversal
{
If(ptr!=NULL)
{
inorder(ptr>lchild).ptr>data). Printf(“%c”.
ptr>data). postorder(ptr>rchild).postorder(ptr)
Struct node *ptr.
Postorder Traversal
{
If(ptr != NULL)
{
postorder(ptr>lchild).}}
  <document classification>
. Printf(“%c”.
  <document classification>
. 1. that all left node contain value less than the node(root) value 2.Binary Search Tree
A Binary Search tree is a binary tree that may be empty. and all the right side node contain value greater than the node(root) value. then it satisfies the following properties. if it is not empty. The left & right subtrees are also binary search trees. 3.
Operations of Binary Search Tree
Creation
Insertion & Searching
Deletion
  <document classification>
.
Compare key with data of root node 1.Insertion & Searching
Suppose we want to search and insert data. This can be done as: 1. If key>data of node then compare the data of right child node
  <document classification>
. If key<data of node then compare the data of left child node 2.
34<38 compare with Left child of 38 But 38 has no left child so this is the exact place to insert 34
  <document classification>
.We want to insert a node 34
50
30
60
22
38
55
Compare 34 with 50. 34<30 compare with right child of 30 Which is 38 Compare 34 with 38. 34<50 compare with Left child of 50 Which is 30 Compare 34 with 30.
it has no child
3. Node to be deleted has two children
  <document classification>
. Node to be deleted is leaf node .Deletion Operation
There are Four possibilities
1. Node to be deleted has only one child
4. There are no node in the tree
2.
M Lendis. AVL tree may or may not be perfectly balanced.AVLTREE
An almost height balanced tree is called an AVL tree after the Russian mathematician G. who first defined and studied this form of a tree.
  <document classification>
.M addisonvelskii and E.
  <document classification>
. the longest paths from the left of the node are about the same length as the longest path on the right. According to this definition we might consider a tree to be well balanced if.Height Balanced Tree
A binary tree of height h is completely balanced or balanced if all leaves (terminal) occur at nodes of level h or h1 and if all nodes at levels lower than h1 have two children. for each node.
On the other hand tree in fig2 is completely balanced tree.
  <document classification>
. a tree is height balanced if.Height Balanced Tree
More precisely. but it is not completely balanced. for each node in the tree. The tree in fig1 given in next slide is height balanced. the height of left subtree differs from the height of the right subtree by no more than 1.
8
5 2 1 4 6 9 7 10 11 12 1 2 3 4 5
6
9 8 10
7 Fig 2
3
  <document classification>
Fig 1
.
equal. or one less that the height of the right subtree. We may define a balance factor (BF) as BF= (Height of left subtree – Height of right subtree) If two subtree are of same height BF= 0 if right subtree is higher BF= 1 if left subtree is higher BF= +1
  <document classification>
.Building Height Balanced Tree
Each node of an AVL tree has the property that the height of the left subtree is either one more.
AVLTREE
An AVL tree is binary search tree which has the following properties: The subtree of every node differs in height by at most one.
  <document classification>
.
Every sub tree is an AVL tree.
B
0
  <document classification>
0
A
C
0
.Rules of AVL Tree – Single Rotation
A
2
Rule :.1
B
1
C
0
If the height is 2 then put that node to the left most of its right child.
0
B
C
  <document classification>
0
A
0
.2
1 0
2
A
B
C
if the height is 2 then put that node to the right most of its left child.Rules of AVL Tree – Single Rotation
Rule :.
2 B
1
C
0
  <document classification>
A
.3
1
2
A
B
0
C
In this case the height (BF) is 2 so we will put that node to the right most of its left child.Rule of AVL Tree – Double Rotation
Rule :.
0 0
C
A
0
B
  <document classification>
. So we will have to again rotate it toward the left most of its right child C node.Now the height (BF) is 2.
Double Rotation – Method 2 We can also explain the concept of double rotation in the following way. If height is 2 & the right side of its left child is larger.
2
A 2nd
1
2
A C
0
0
1
B
0
C
  <document classification>
1st
C
0
B
B
0 A
. then rotate it toward the left first & then rotate is toward the right.
B 2 C
  <document classification>
1
A
0
. so we will put that node to the left most of its rightchild.Rule of AVL Tree – Double Rotation
A
2
Rule :.4 B
1
C
0
In this case the height (BF) is 2.
Rule of AVL Tree – Double Rotation
Now the height (BF) is 2. so we will put that node to the right most of its left child.
C
0
0
A
B
0
  <document classification>
.
10
3
0
3
1
3 5
0
2
5
1
11
  <document classification>
0
(a)
(b)
(c)
. 6. 1. 11. 7. 12. 8. 2. 4. 5. 3.EXAMPLE of AVL Tree
Make the Height Balanced Tree from the following list of elements.
5
0
5
1
3
0
11
0
3
0
11
8
1
(d) 5 3
1 0
(e 0 )
11 4
0
1
8
0
  <document classification>
(f)
.
5
0 0
0
3 4
0
11 8
0
1
1
(g)
0 0
5 3 4
0
0
11 8
0
0
1
12
0
(h)
  <document classification>
.
5
0
1
3
0
11
1
1
4
0
8
7
0
1
12
0
(i)
5
0 3
1
0
11
1
1
2
0
4
0
1
8
12
0
7
0
  <document classification>
(j)
.
the tree becomes unbalanced.After adding 6. 5
1 1
3
4
0 2
11
8
2
1
1
12 (k)
0
2
  <document classification>
0
7
6
0
1
. We need to rebalance by rotating the structure at node 8.
5
1
1
0
3
4
0 0
11
7 12
1
0
1
2
0
0
6
8
0
(L)
  <document classification>
.
5
1 1
1
3 4 2
0 0 1 0
11 7 6
1st
2
1
0 12 2nd
8
1
10 0 If height is 2 & the right side of its left child is larger. then rotate it toward the left first & then rotate is toward the right.
  <document classification>
(m)
.
5
1 1
1
3 4 2
0 0 1 1
11 8 7 12 10
2 0 0
1
6
0
(n)
  <document classification>
.
5
1 1
0
3
4
0 0 1 0
8
7
0 0 0 0
1
11
2
6
10
12
(n)
  <document classification>
.
A
1
1 1
B
M
0 0
C
D
2nd
1
2
K
E
1
L
0
F
0
G
H
  <document classification>
0
1st
. then rotate it toward the right & then rotate it toward the left.If height is 2 & the left side is large.
A
1
1
1
B
M
0 0
C
D
0
2
K
F
1
L
0
H
1
0
E
G
  <document classification>
.
Those in the left branch have value less than the node value. while those in the right branch have value greater than the node value.
For exp: if we keep two values in each node. each node holds a single value and has at most two branches (edges).In a binary search tree.
  <document classification>
. that means at most three branches. the descendents are split into three groups (maximum). This can be generalized by allowing more values at each node.Multi – Way Tree
Multiway Tree :.
Such a structure is called a MultiValue Tree or MTree. the middle descendent will have values between the two values in the root node and the rightmost descendent will have value greater than the second value in the root node.The leftmost descendent will have the value less than the first value in the root.
  <document classification>
.
50
60
45 20 30
52
55 53
70 100 84
  <document classification>
.
BTree is also known as the balance sorttree. several conditions of the tree must be true:1. The height of the tree must be kept to a minimum. There must be no empty subtrees above the leaves (terminal nodes) of the tree. 2.
  <document classification>
. To reduce disk access. If find it use in external sorting.BTree
A BTree is a balanced Mway tree.
  <document classification>
. All nodes except the leaves node must have at least some minimum number of children. Each node has a maximum of M children and a minimum of M/2 children or any number from 2 to the maximum .BTree
3. BTree of order M has following properties 1. The leaves (terminal nodes) of the must all be on the same level tree
4.
the node is split into two nodes.BTree
2. and the key with the median value is
  <document classification>
. 4. 3. All keys in the subtree to the left of a keys are predecessors of the key and that on the right are successor of the key. Keys are arranged in a defined order within the node. When a new key is to be inserted into a full node. Each node has one fewer keys than children with a maximum of M1 keys.
  <document classification>
. 5.BTree
Inserted in the parent node. All leaves are on the same level i. In case the parent node is the root. For exp:.e there is no empty subtree above the level of the leaves.a normal node of tree of order 11 has at least 6 (11/2) & not more than 11 children. a new root is created.
The record is added to the node with an appropriate pointer so that the number of pointer one more than the number of records. If the node overflows because there is an upper bound on the size of a node. If the node can accommodate the new record insertion is simple. splitting is required.
  <document classification>
.BTree Insertion
First the search for the place where the new record must be put is done.
BTree Insertion
The node is split into three parts. The middle record is passed upward and inserted into the parent, leaving two children behind where there was one before. Splitting may propagate up the tree because the parent into which a record to be split in its child node, may overflow. Therefore it may also split. If the root is required to be split, a new root is created with just two children, and the tree grows taller by one level.
  <document classification>
BTree Example
Consider building a BTree of degree 4 that is a balanced fourway tree where each node can hold three data values and have four branches. Suppose it needs to contain the following values. 1, 5, 6, 2, 8, 11, 13, 18, 20, 7, 9 The first value 1 is placed in a new node which can accommodate the next two values also i.e.
  <document classification>
BTree Example
1 5 6
When the fouth value 2 is to be added, the node is split at a median value 5 into two leaf node with a parent at 5. 5
1 2
  <document classification>
6
Therefore it is split at median value 8 and this it moves up to the parent. where 13 finds appropriate place. Also it split up to make two nodes. is full. But the right leaf node. 5 8
1 2
  <document classification>
6
11 13
.BTree Example
Now 13 is to be added.
rather than at its leaves as was the case in binary tree.
  <document classification>
. Also the tree appears to grow at its root. The final result is 5 8 13
1 2
6 7
9 11
18 20
The tree built up in this manner is balanced.BTree Example
The remaining items may also be added following the above procedure. having all of its leaf nodes at one level.
BTree Example
The following item 8 is to be added in a leaf node. Next 11 is also put in the same. So we have
5
1 2 6 8 11
  <document classification>
. A search for its appropriate place puts it in the node containing 6.
the record to be deleted is first search for. deletion is simple. The record along with an appropriate pointer is deleted.BTree Deletion
As in the insertion method.
CASE 1: If the record is in a terminal node.
  <document classification>
. that is a record with a next higher value.
CASE 2 : If the record is not in terminal node. it is replaced by a copy of its successor.
If the new node size is lower than the minimum an underflow occurs.
  <document classification>
. the deletion is over. the new node size is not below the minimum.BTree Deletion
if on deleting the record.
Redistribution is carried out if either of a adjacent siblings contains more than the minimum number of records.
For redistribution the contents of the node which has less than minimum records.
The left and right halves are written back to the two siblings.BTree Deletion
The contents of its adjacent siblings which has more then minimum records. The central record from this collection is written back to parent.
In case the node with less than minimum number of records has no adjacent sibling that is more than minimally full
  <document classification>
. and the separating record from parent are collected.
BTree Deletion
Concatenation is used. It may be solved by redistribution or concatenation we will illustrate by deleting keys from the tree given below. In this case the node is merged with its adjacent sibling and the separate record from its parent.
  <document classification>
.
BTree Deletion
Delete ‘h’. This is a simple deletion
J
c f
m r
a b
  <document classification>
d e
g h I
k l n p stux
.
‘r’ is not at a leaf node. j
c f
ab d e g I k l
m s
n p t ux
  <document classification>
. Therefore its successor ‘s’ is moved up ‘r’ moved down and deleted.BTree Deletion
Delete ‘r’.
j
c f m t
ab de
  <document classification>
gI
k l
n s
ux
.BTree Deletion
Delete ‘p’. So ‘t’ moves up and ‘s’ moves down. The node contains less than minimum numbers of keys required. The sibling can spare a key.
j. It’s sibling cannot contribute.BTree Deletion
Deletion ‘d’ again node is less than minimal required. f j m t a b c
  <document classification>
g i
k l
n s
u x
. m and t are combined to form the new root. Therefore the size of tree shrinks by one level. This leaves the parent with only one key. Therefore f.
A
1 1
0
B M
0 0 0
F C
0
0
K
E H
1 0
L
0
D
G
  <document classification>
.