Sei sulla pagina 1di 79

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

Level 0 Level 1

G F Internal Vertex

Level 2

Level 3

J H I

| | <document classification>

Level 4

K L Leaf/Pendant

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. Edge: is a connection between two vertices. Degree: The number of subtrees of a node is called its degree. In Fig the degree of node A is 3.

| | <document classification>

Leaf or Terminal Nodes: Nodes that have degree

one is called leaf or Terminal nodes. In Fig B, C, E, H, K, L are 'Leaf' nodes, other nodes of Tree are called 'Non Leaf' nodes. Children: The roots of the subtrees of a node I
are called the children of node I. I is the 'parent' of its children. Siblings: Children of the same parent are called 'Siblings', E, F, G are Siblings.
| | <document classification>

Length: the length of a path is the number


of edges on the path.

Ancestor & Descendant:- if there is a


path from m to n then m is ancestor of n & n is a descendant of m.

Depth:- the depth of any node n is the


length of the path from root to n.
| | <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.

| | <document classification>

Binary Trees

A Binary Tree is a finite set of elements that is either empty or is partitioned 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 subtrees of the original tree. A left or right subtree can be empty. A node of a Binary Tree can have at most two Branches.

| | <document classification>

| | <document classification>

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, 2, 5 has two non empty left and right child so it is strictly Binary tree 1 2 3

| | <document classification>

COMPLETE BINARY TREE

A complete binary tree is a binary tree where all the nodes have both children except last node. 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. 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>

Parent of I will be floor(9/2)=4

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

3
C

6
F 11 K G

12
L

13
M

14
N

15
O

| | <document classification>

COMPLETE BINARY TREE

Representation of Binary Tree


Linked Representation As in the linked list we take the structure for tree. In which we take three members. First member for data, second and third members are structure pointers which point to same structure as for tree node
Struct node{ Char data;

Node *lchild;
node *rchild;}
| | <document classification>

Traversing in Binary Tree


1.Preorder (RLR) Root Left Right 2.Inorder (LRR) Left Root Right

3.Postorder (LRR) Left Right Root

| | <document classification>

Consider the fig given below.


-

B
| | <document classification>

Inorder

It follows the general strategy of LEFTROOT-RIGHT. In this traversal if Tree is not empty we first traverse the left subtree. Then visit the root node of Tree, and Then traverse the right subtree.

| | <document classification>

This is an example of an expression tree for (A+B*C) (D *E). Tree 1, at the start is rooted at . Since left (T) is not empty, current T becomes rooted at +. Since left (T) is not empty, current T becomes rooted at A. Since left (T) is empty. We visit root i.e. A.
| | <document classification>

We access T root i.e. +.

We now perform inorder traversal of right (T).


Current T becomes rooted at *.

Since left (T) is not empty. Current T becomes rooted at B since left (T) is empty. We visit its root i.e. B, check for right (T) which is empty, therefore, we move back to parent tree. We visit its root i.e. *.
| | <document classification>

Now inorder traversal of right (T) is performed, which would give us C. We visit Ts root i.e D and perform inorder traversal of right (T) which would give us * and E. Therefore the complete listing is A+B*C D*E We may note that expression is infix notation. The following is a C procedure for inorder traversal of a binary tree.
| | <document classification>

void inorder (struct btree *s) { if(s != NULL) { inorder (s->left); printf (%d,s->data); inorder (s->right); } else { return; }
| | <document classification>

Preorder
It is a Root Left Right strategy.

Visit the root. Traverse the left Subtree in Preorder . Traverse the right Subtree in Preorder.
A preorder traversal of the tree given in inorder traversal would yield.

-+A*BC*DE
| | <document classification>

Implementation using C Procedure

| | <document classification>

void postorder (struct btree *s) { if(s != NULL) { postorder (s -> left); postorder(s -> right); printf(%d, s -> data); } else return; }

Postorder
It is a Left-Right-Root strategy. i.e. Traverse the Left Subtree in Postorder. Traverse the right Subtree in Postorder. Visit the root.

For exp:- a postorder traversal of the tree give in inorder traversal would be
| | <document classification>

ABC*+DE*-

Preorder(ptr)
Struct node *ptr;

Preorder Traversal

{
If(ptr!=NULL)

{
Printf(%c,ptr->data); Preorder(ptr->lchild); Preorder(ptr->rchild);}}
| | <document classification>

inorder(ptr)
Struct node *ptr;

Inorder Traversal

{
If(ptr!=NULL)

{
inorder(ptr->lchild); Printf(%c,ptr->data); inorder(ptr->rchild);}}
| | <document classification>

postorder(ptr)
Struct node *ptr;

Postorder Traversal

{
If(ptr != NULL)

{
postorder(ptr->lchild); postorder(ptr->rchild); Printf(%c,ptr->data);}}
| | <document classification>

Binary Search Tree


A Binary Search tree is a binary tree that may be empty, if it is not empty, then it satisfies the following properties. 1. that all left node contain value less than the node(root) value 2. and all the right side node contain value greater than the node(root) value. 3. The left & right subtrees are also binary search trees.

| | <document classification>

Operations of Binary Search Tree


Creation

Insertion & Searching


Deletion

| | <document classification>

Insertion & Searching


Suppose we want to search and insert data. This can be done as: 1. Compare key with data of root node 1. If key<data of node then compare the data of left child node 2. If key>data of node then compare the data of right child node
| | <document classification>

We want to insert a node 34


50

30

60

22

38

55

Compare 34 with 50. 34<50 compare with Left child of 50 Which is 30 Compare 34 with 30. 34<30 compare with right child of 30 Which is 38 Compare 34 with 38. 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>

Deletion Operation
There are Four possibilities

1. There are no node in the tree


2. Node to be deleted is leaf node , it has no child

3. Node to be deleted has only one child


4. Node to be deleted has two children

| | <document classification>

AVL-TREE
An almost height balanced tree is called an AVL tree after the Russian mathematician G.M addison-velskii and E.M Lendis, who first defined and studied this form of a tree. AVL tree may or may not be perfectly balanced.

| | <document classification>

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 h-1 and if all nodes at levels lower than h-1 have two children. According to this definition we might consider a tree to be well balanced if, for each node, the longest paths from the left of the node are about the same length as the longest path on the right.
| | <document classification>

Height Balanced Tree


More precisely, a tree is height balanced if, for each node in the tree, the height of left subtree differs from the height of the right subtree by no more than 1. The tree in fig1 given in next slide is height balanced, but it is not completely balanced. On the other hand tree in fig2 is completely balanced tree.
| | <document classification>

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

Building Height Balanced Tree


Each node of an AVL tree has the property that the height of the left subtree is either one more, 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>

AVL-TREE
An AVL tree is binary search tree which has the following properties: The sub-tree of every node differs in height by at most one.

Every sub tree is an AVL tree.

| | <document classification>

Rules of AVL Tree Single Rotation


A
-2

Rule :- 1

-1

If the height is -2 then put that node to the left most of its right child.
B
0
| | <document classification>

Rules of AVL Tree Single Rotation

Rule :- 2
1 0

if the height is 2 then put that node to the right most of its left child.
0

B
C
| | <document classification>

Rule of AVL Tree Double Rotation

Rule :- 3
-1

B
0

In this case the height (BF) is 2 so we will put that node to the right most of its left child. -2 B
-1

C
0

| | <document classification>

Now the height (BF) is -2. So we will have to again rotate it toward the left most of its right child C node.

0 0

C
A
0

| | <document classification>

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, then rotate it toward the left first & then rotate is toward the right.
2

A 2nd
1

A C
0
0

-1

B
0

| | <document classification>

1st

0 A

Rule of AVL Tree Double Rotation


A
-2

Rule :- 4 B
1

In this case the height (BF) is -2, so we will put that node to the left most of its rightchild. B 2 C
| | <document classification>

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

| | <document classification>

EXAMPLE of AVL Tree


Make the Height Balanced Tree from the following list of elements. 3, 5, 11, 8, 4, 1, 12, 7, 2, 6, 10

-1

3 5
0

-2

-1

11
| | <document classification>

(a)

(b)

(c)

-1

11

11
8

(d) 5 3
-1 0

(e 0 )

11 4
0

| | <document classification>

(f)

5
0 0

3 4
0

11 8
0

(g)
0 0

5 3 4
0

11 8
0

12

(h)
| | <document classification>

5
0

-1

3
0

11

8
7
0

12

(i)

5
0 3
-1

11

1
2
0

12

| | <document classification>

(j)

After adding 6, the tree becomes unbalanced. We need to rebalance by rotating the structure at node 8. 5
1 -1

3
4
0 2

11
8

-1

12 (k)

2
| | <document classification>

7
6
0

5
1
-1

3
4
0 0

11
7 12

1
0

(L)

| | <document classification>

5
1 -1

-1

3 4 2
0 0 -1 0

11 7 6
1st

0 12 2nd

-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

(n)
| | <document classification>

5
1 -1

3
4
0 0 1 0

8
7

0 0 0 0

11

10

12

(n)
| | <document classification>

If height is -2 & the left side is large, then rotate it toward the right & then rotate it toward the left. A
1
1 -1

B
M
0 0

C
D
2nd
-1

-2

-1

H
| | <document classification>

1st

A
1
1

-1

B
M
0 0

C
D
0

-2

-1

-1
0

E
G

| | <document classification>

Multi Way Tree


Multi-way Tree :- In a binary search tree, each node holds a single value and has at most two branches (edges).

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. This can be generalized by allowing more values at each node.
For exp: if we keep two values in each node, that means at most three branches, the descendents are split into three groups (maximum).

| | <document classification>

The leftmost descendent will have the value less than the first value in the root, 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. Such a structure is called a Multi-Value Tree or MTree.

| | <document classification>

50

60

45 20 30

52

55 53

70 100 84

| | <document classification>

B-Tree
A B-Tree is a balanced M-way tree. B-Tree is also known as the balance sort-tree. If find it use in external sorting. To reduce disk access, several conditions of the tree must be true:1. The height of the tree must be kept to a minimum. 2. There must be no empty subtrees above the leaves (terminal nodes) of the tree.
| | <document classification>

B-Tree
3. The leaves (terminal nodes) of the must all be on the same level tree

4. All nodes except the leaves node must have at least some minimum number of children. B-Tree of order M has following properties 1. Each node has a maximum of M children and a minimum of M/2 children or any number from 2 to the maximum .
| | <document classification>

B-Tree
2. Each node has one fewer keys than children with a maximum of M-1 keys. 3. Keys are arranged in a defined order within the node. 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. 4. When a new key is to be inserted into a full node, the node is split into two nodes, and the key with the median value is
| | <document classification>

B-Tree
Inserted in the parent node. In case the parent node is the root, a new root is created. 5. All leaves are on the same level i.e there is no empty subtree above the level of the leaves. For exp:- a normal node of tree of order 11 has at least 6 (11/2) & not more than 11 children.
| | <document classification>

B-Tree Insertion
First the search for the place where the new record must be put is done. If the node can accommodate the new record insertion is simple.

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, splitting is required.
| | <document classification>

B-Tree 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>

B-Tree Example
Consider building a B-Tree of degree 4 that is a balanced four-way 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>

B-Tree 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>

B-Tree Example
Now 13 is to be added. But the right leaf node, where 13 finds appropriate place, is full. Therefore it is split at median value 8 and this it moves up to the parent. Also it split up to make two nodes. 5 8

1 2
| | <document classification>

11 13

B-Tree Example
The remaining items may also be added following the above procedure. The final result is 5 8 13

1 2

6 7

9 11

18 20

The tree built up in this manner is balanced, having all of its leaf nodes at one level. Also the tree appears to grow at its root, rather than at its leaves as was the case in binary tree.
| | <document classification>

B-Tree Example
The following item 8 is to be added in a leaf node. A search for its appropriate place puts it in the node containing 6. Next 11 is also put in the same. So we have

5
1 2 6 8 11

| | <document classification>

B-Tree Deletion
As in the insertion method, the record to be deleted is first search for.

CASE 1: If the record is in a terminal node, deletion is simple. The record along with an appropriate pointer is deleted.
CASE 2 : If the record is not in terminal node, it is replaced by a copy of its successor, that is a record with a next higher value.
| | <document classification>

B-Tree Deletion
if on deleting the record, the new node size is not below the minimum, the deletion is over. If the new node size is lower than the minimum an underflow occurs.

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,
| | <document classification>

B-Tree Deletion
The contents of its adjacent siblings which has more then minimum records, and the separating record from parent are collected. The central record from this collection is written back to parent. The left and right halves are written back to the two siblings.

In case the node with less than minimum number of records has no adjacent sibling that is more than minimally full
| | <document classification>

B-Tree Deletion
Concatenation is used. In this case the node is merged with its adjacent sibling and the separate record from its parent. It may be solved by redistribution or concatenation we will illustrate by deleting keys from the tree given below.

| | <document classification>

B-Tree Deletion
Delete h. This is a simple deletion

c f

m r

a b
| | <document classification>

d e

g h I

k l n p stux

B-Tree Deletion
Delete r. r is not at a leaf node. Therefore its successor s is moved up r moved down and deleted. j

c f
ab d e g I k l

m s
n p t ux

| | <document classification>

B-Tree Deletion
Delete p. The node contains less than minimum numbers of keys required. The sibling can spare a key. So t moves up and s moves down.

j
c f m t

ab de
| | <document classification>

gI

k l

n s

ux

B-Tree Deletion
Deletion d again node is less than minimal required. This leaves the parent with only one key. Its sibling cannot contribute. Therefore f, j, m and t are combined to form the new root. Therefore the size of tree shrinks by one level. f j m t a b c
| | <document classification>

g i

k l

n s

u x

A
1 1

B M
0 0 0

F C
0

E H

-1 0

| | <document classification>

Potrebbero piacerti anche