This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.

Find out moreA 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 .

In Fig the degree of node A is 3. Consider the fig it has 13 nodes. Edge: is a connection between two vertices. | | <document classification> . Degree: The number of subtrees of a node is called its degree.Basic Terminology about Trees Node: A node stands for the item of information plus the branches to other items.

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

if there is a path from m to n then m is ancestor of n & n is a descendant of m.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. | | <document classification> . Depth:.

Forest: A set of trees is called a forest. | | <document classification> . Height : The height of a node n is defined as the longest path from node n to leaf.

called the left and right subtrees of the original tree. | | <document classification> . A node of a Binary Tree can have at most two Branches.Binary Trees A Binary Tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. 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> .

2. 5 has two non empty left and right child so it is strictly Binary tree 1 2 3 4 5 | | <document classification> 6 7 . Here internal node 1.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.

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

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 .

} | | <document classification> . In which we take three members. second and third members are structure pointers which point to same structure as for tree node Struct node{ Char data.Representation of Binary Tree Linked Representation As in the linked list we take the structure for tree. First member for data. Node *lchild. node *rchild.

Traversing in Binary Tree 1.Inorder (LRR) Left Root Right 3.Preorder (RLR) Root Left Right 2.Postorder (LRR) Left Right Root | | <document classification> .

- + * A * D E B | | <document classification> C .Consider the fig given below.

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

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

We visit its root i. we move back to parent tree. We now perform inorder traversal of right (T). We visit its root i. B. Since left (T) is not empty.e. Current T becomes rooted at *. therefore. Current T becomes rooted at ‘B’ since left (T) is empty.e. ‘*’.We access T root i. check for right (T) which is empty. | | <document classification> .e. ‘+’.

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

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

-+A*BC*DE | | <document classification> . Traverse the left Subtree in Preorder . Visit the root. A preorder traversal of the tree given in inorder traversal would yield. Traverse the right Subtree in Preorder.Preorder It is a Root – Left – Right strategy.

} else return. } .Implementation using C Procedure | | <document classification> void postorder (struct btree *s) { if(s != NULL) { postorder (s -> left). printf(“%d”. postorder(s -> right). s -> data).

a postorder traversal of the tree give in inorder traversal would be | | <document classification> ABC*+DE*- . Traverse the Left Subtree in Postorder. i. Visit the root.e. Traverse the right Subtree in Postorder. For exp:.Postorder It is a Left-Right-Root strategy.

Preorder(ptr->lchild).Preorder(ptr) Struct node *ptr. Preorder(ptr->rchild).}} | | <document classification> .ptr->data). Preorder Traversal { If(ptr!=NULL) { Printf(“%c”.

inorder(ptr) Struct node *ptr.ptr->data). Printf(“%c”. inorder(ptr->rchild).}} | | <document classification> . Inorder Traversal { If(ptr!=NULL) { inorder(ptr->lchild).

Postorder Traversal { If(ptr != NULL) { postorder(ptr->lchild).postorder(ptr) Struct node *ptr.}} | | <document classification> . postorder(ptr->rchild). Printf(“%c”.ptr->data).

1. 3. The left & right subtrees are also binary search trees. if it is not empty. | | <document classification> . that all left node contain value less than the node(root) value 2. then it satisfies the following properties.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.

Operations of Binary Search Tree Creation Insertion & Searching Deletion | | <document classification> .

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> . Compare key with data of root node 1.Insertion & Searching Suppose we want to search and insert data. This can be done as: 1.

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<38 compare with Left child of 38 But 38 has no left child so this is the exact place to insert 34 | | <document classification> . 34<50 compare with Left child of 50 Which is 30 Compare 34 with 30.

Node to be deleted is leaf node . There are no node in the tree 2.Deletion Operation There are Four possibilities 1. Node to be deleted has only one child 4. it has no child 3. Node to be deleted has two children | | <document classification> .

| | <document classification> .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.AVL-TREE An almost height balanced tree is called an AVL tree after the Russian mathematician G.

for each node.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. | | <document classification> . According to this definition we might consider a tree to be well balanced if. the longest paths from the left of the node are about the same length as the longest path on the right.

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

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.Building Height Balanced Tree Each node of an AVL tree has the property that the height of the left subtree is either one more. 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> . or one less that the height of the right subtree.

| | <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.

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. B 0 | | <document classification> 0 A C 0 .

Rules of AVL Tree – Single Rotation Rule :.2 1 0 2 A B C if the height is 2 then put that node to the right most of its left child. 0 B C | | <document classification> 0 A 0 .

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. -2 B -1 C 0 | | <document classification> A .Rule of AVL Tree – Double Rotation Rule :.

Now the height (BF) is -2. 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.

then rotate it toward the left first & then rotate is toward the right. 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 .Double Rotation – Method 2 We can also explain the concept of double rotation in the following way.

Rule of AVL Tree – Double Rotation A -2 Rule :.4 B 1 C 0 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> 1 A 0 .

C 0 0 A B 0 | | <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.

4. 3. 7. 8. 10 3 0 3 -1 3 5 0 -2 5 -1 11 | | <document classification> 0 (a) (b) (c) . 1.EXAMPLE of AVL Tree Make the Height Balanced Tree from the following list of elements. 5. 12. 11. 6. 2.

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) .

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. the tree becomes unbalanced.

5 1 -1 0 3 4 0 0 11 7 12 1 0 1 2 0 0 6 8 0 (L) | | <document classification> .

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 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.

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> .

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 2nd -1 -2 K E -1 L 0 F 0 G H | | <document classification> 0 1st .

A 1 1 -1 B M 0 0 C D 0 -2 K F -1 L 0 H -1 0 E G | | <document classification> .

each node holds a single value and has at most two branches (edges).Multi – Way Tree Multi-way Tree :. For exp: if we keep two values in each node. | | <document classification> .In a binary search tree. This can be generalized by allowing more values at each node. the descendents are split into three groups (maximum). while those in the right branch have value greater than the node value. Those in the left branch have value less than the node value. that means at most three branches.

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

50 60 45 20 30 52 55 53 70 100 84 | | <document classification> .

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

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. | | <document classification> .B-Tree 3. Each node has a maximum of M children and a minimum of M/2 children or any number from 2 to the maximum .

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

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

If the node overflows because there is an upper bound on the size of a node. | | <document classification> .B-Tree Insertion First the search for the place where the new record must be put is done. splitting is required. 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 can accommodate the new record insertion is simple.

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>

6

Therefore it is split at median value 8 and this it moves up to the parent. where 13 finds appropriate place. is full. 5 8 1 2 | | <document classification> 6 11 13 . But the right leaf node.B-Tree Example Now 13 is to be added. Also it split up to make two nodes.

The final result is 5 8 13 1 2 6 7 9 11 18 20 The tree built up in this manner is balanced. | | <document classification> . rather than at its leaves as was the case in binary tree. Also the tree appears to grow at its root. having all of its leaf nodes at one level.B-Tree Example The remaining items may also be added following the above procedure.

So we have 5 1 2 6 8 11 | | <document classification> . Next 11 is also put in the same.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.

| | <document classification> . deletion is simple. CASE 2 : If the record is not in terminal node. CASE 1: If the record is in a terminal node. the record to be deleted is first search for. it is replaced by a copy of its successor.B-Tree Deletion As in the insertion method. that is a record with a next higher value. The record along with an appropriate pointer is deleted.

Redistribution is carried out if either of a adjacent siblings contains more than the minimum number of records. | | <document classification> .B-Tree Deletion if on deleting the record. For redistribution the contents of the node which has less than minimum records. If the new node size is lower than the minimum an underflow occurs. the deletion is over. the new node size is not below the minimum.

B-Tree Deletion The contents of its adjacent siblings which has more then minimum records. and the separating record from parent are collected. The left and right halves are written back to the two siblings. 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> .

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

B-Tree 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.B-Tree Deletion Delete ‘r’. 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.

j c f m t ab de | | <document classification> gI k l n s ux .B-Tree Deletion Delete ‘p’. The sibling can spare a key. The node contains less than minimum numbers of keys required. So ‘t’ moves up and ‘s’ moves down.

m and t are combined to form the new root. This leaves the parent with only one key. Therefore the size of tree shrinks by one level. j. It’s sibling cannot contribute.B-Tree 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 . 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> .

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd