Sei sulla pagina 1di 35

IT T33- Data Structures

UNIT III
Trees : General trees – binary tree – traversal methods – expression trees – game trees. Binary
search trees – AVL trees – Splay trees – B Trees – B+ Trees – Tries – application.

Objective:
 To learn the concept and application of different varieties of tree data structure
Outcome:
 Able to implement the tree data structure in programming
3.1 General Trees
3.1.1. Definition of Tree
A tree is a collection of nodes. The collection can be empty .Otherwise a tree consists of
distinguished node r called the root and zero or more non empty sub-trees T1,T2,T3…………….Tk
each of whose roots are connected by a directed edge from r.

 There is one specially designated node called ROOT.


 The remaining nodes are partitioned into a collection of sub-trees of the root each of which is
also a tree.
3.1.2 Basic Terminology of tree:
Node (Vertex, Edge)
 Main component of a tree structure
 Stores data and links to other nodes
Edge - connection between one node to another.
Root - the top most node in a tree.
Parent - node that has a child.
Siblings - nodes with the same parent.
Leaves - nodes with no children.
Internal nodes - nodes with at least one child.
Degree - number of sub trees of a node.
Level - The level of a node is defined by 1 + the number of connections between the node and
the root

SMVEC- Department of Information Technology 1


IT T33- Data Structures
Height - The height of a node is the length of the longest downward path between the node and
a leaf
Forest- A set of disjoint trees
External nodes -A node that has no children is called as a terminal node

Properties of a Tree
 Any node can be the root of the tree and each node in a tree has the property that there is
exactly one path connecting that node with every other node in the tree.
 The tree in which the root is identified is called a rooted tree; a tree in which the root is
not identified is called a free tree.
 Each node, except the root, has a unique parent.

3.2 Binary Tree:

 Finite collection of elements / nodes that is either empty or partitioned into three disjoint
subsets.
 The first subset is termed as the root element.
 The remaining elements (if any) are partitioned into two binary trees.
 These are called the left and right sub-trees of the binary tree.

Properties of Binary Trees:

Property 1: Each node can have up to two successor nodes.


Property 2:A unique path exists from the root to every other node.

SMVEC- Department of Information Technology 2


IT T33- Data Structures

Full binary tree: A full binary tree (2-tree or strictly binary tree) is a tree in which every node other
than the leaves has two children.

Complete binary tree: binary tree in which every level, except possibly the last, is completely
filled, and all nodes are as far left as possible

Strictly binary tree : If every non-leaf node in a binary tree has non-empty left and right sub-trees,

Height-balanced Binary Tree:

 A height-balanced binary tree is a binary tree such that, the left & right sub trees for any
given node differ in height by no more than one
 Note: Each complete binary tree is a height-balanced binary tree

Extended Binary tree:

 A binary tree T is said to be an extended binary tree if each node in the tree has either no
child or exactly two children.
 In extended binary tree, node having two children are called internal node, and node without
children are called as external node.

In-degree and Out-degree In binary tree:

Degree: The degree of node is equal to number of children that a node has.
In-degree: The number of edges arriving at that node .The root node is the only node that has
in-degree as zero
Out-degree: The number of edges leaving that node

SMVEC- Department of Information Technology 3


IT T33- Data Structures
Balancing factor in binary tree: The balance factor of a binary tree is the difference in height
between its left and right sub trees:

B  HL  HR
Searching in a binary tree:
 Start at the root
 Search the tree level by level, until you find the element you are searching for or you
reach a leaf.
Time Taken for searching binary tree is O(N),which is not efficient

3.2.1 Representation of Binary Trees.


 Linear representation or Array representation
 Linked list representation

Linear representation or Array representation:


 The linear representation method of implementing a binary tree uses a one dimensional
array of size (2h+1)-1.
 Once the size of the array has been determined the following method is used to represent
the tree.
1. Store the root in 1st location of the array.
2. The left child of the nth node is stored at location 2n, and the right child of the nth
node is stored at location 2n+1.
3. If there is no left or right child , the array location is left empty.
4.
Location 0 of the array can be used to store the size of the tree in terms of total number of nodes.
Example

 All non-existing children are show by \0.


 Index of the left child of a node n= 2*n
 Index of the right of a node n=2*n+1
 Index of the parent of a node i=i/2.

SMVEC- Department of Information Technology 4


IT T33- Data Structures
Advantages
 Given a child node, its parent node can be determined immediately. If a child node is at
location N in the array, then its parent is at location N/2.
 It can be implemented easily in languages in which only static memory allocation is directly
available.

Disadvantages
 Insertion or deletion of a node causes considerable data movement up and down the array,
using an excessive amount of processing time.
 Wastage of memory due to partially filled trees.

Linked list representation:

The advantage of using linked representation of binary tree is that insertion and deletions involve
no data movement of nodes except the rearrangement of pointers.

Disadvantage of linked representation of binary trees.

 Given a node structure, it is difficult to determine its parent node.


 Memory spaces are wasted for storing NULL pointers for the nodes, which have one or no sub-
tree.
 This implementation requires dynamic memory allocation which is not possible in some
programming language.

A node of a binary tree consists of the three fields as shown below.

 Data Typedef struct node


 Address of the left child Left Data Right {
 Address of the right child int data;
struct node *left;
struct node *right;
}node;

Left Data Right

Left Data Right Left Data Right

Left Data Right

Example of linked representation of binary tree

SMVEC- Department of Information Technology 5


IT T33- Data Structures

3.3 Traversals in binary tree:


In a traversal of a binary tree, each element of the binary tree is visited exactly once.
During the visit of an element, all action(searching for particulars nodes, compilers commonly build
binary trees in the process of scanning, parsing, generating code and evaluation of arithmetic
expression)with respect to this element is taken. The various traversals in binary tree are:

 In-order Traversal (Left, Parent, Right)


 Preorder Traversal (Parent, Left, Right)
 Post-order Traversal (Left, Right, Parent)
 Level Order Traversal (Top to Bottom, Left to Right)

Each node is defined as a structure of the following form:


struct node
{
int info;
struct node *lchild;
struct node *rchild;
} typedef struct node NODE;

3.3.1 Inorder Traversal(Left-Root-Right)


In this traversal, if T is not empty, the following steps followed;
Steps : void inorder_traversal ( NODE * T)
1. Traverse left subtree in inorder {
2. Process root node if( T ! = NULL)
3. Traverse right subtree in inorder { inorder_traversal(T->lchild);
Write T->info;
inorder_traversal(T->rchild);
}
}

SMVEC- Department of Information Technology 6


IT T33- Data Structures
Consider the binary tree given in the following figure.
-

+ *

A * D E

B C

Expression Tree- (A+B*C)-(D*E)


A binary tree can be used to represent arithmetic expressions if the node value can be either
operators or operand values and are such that
 Each operator node has exactly two branches.
 Each operand node has no branches; such trees are called expression trees.

Tree, T 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.
 We access T root i.e. ‗+‘.
 We now perform in-order 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 it to
root i.e. B; cluck for right (T) which is empty, therefore we move back to parent tree. We visit its
root i.e. ‗*‘.
 Now In order traversal of right (T) is performed; which would give us ‗C‘. We visit T‘s root i.e.,
‗D‘ and perform in-order traversal of right (T); which would give us ‗* and E‘.
The final listing is A+B*C-D*E. We may note that expression is in infix notation.

SMVEC- Department of Information Technology 7


IT T33- Data Structures
3.3.2 Preorder traversal(root-left-right)
Steps : void preorder_traversal ( NODE * T)
1. Process root node {
2. Traverse left subtree in preorder if( T ! = NULL)
3. Traverse right subtree in preorder {
write T->info;
preorder_traversal(T->lchild);
preorder_traversal(T->rchild);
}
}

 Since it visits the root first , it is displayed i.e. ‗-‗ is displayed


 Next left traversal is done. Here the current T becomes rooted at ‗+‘
 Since left(T) is not empty; current T becomes rooted at A
 Since left (T) is empty we visit right (T) i.e. ‗*‘ and T is rooted here.
 Since left (T) is not empty , current T gets rooted at B
 Since left (T) is empty, we visit right (T) i.e. ‗C‘ and T is at present rooted here
 Since left (T) is empty and no right (T) we move to the root ‗*‘
 Visit root ‗+‘
 Now the left sub tree is complete so we move to right sub tree i.e. T is rooted at ‗*‘
 Since left (T) is not empty , current T becomes rooted at D
 Since left (T) is empty, T gets rooted at right (T) i.e. E.
Thus the complete listing is -+A*BC*DE. We may note that expression is in prefix notation.

3.3.3 Postorder Traversal( Left-Right-Root):


Steps : void postorder_traversal ( NODE * T)
1. Traverse left subtree in postorder {
2. Traverse right subtree in postorder if( T ! = NULL)
3. process root node {
postorder_traversal(T->lchild);
postorder_traversal(T->rchild);
write T->info;
}
}

SMVEC- Department of Information Technology 8


IT T33- Data Structures
 Tree, T, 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 right (T) ‗*‘
 Since Right (T) is empty ; we visit root i.e. A
 We access right T‘s root it *
 Since left (T) is not empty ; we visit right (T)
 Since right (T) is empty we visit root i.e., B
 Since right (T) is not empty ; current T becomes rooted at ‗C‘
 Since left (T) & right (T) is empty , we visit root i.e. ‗C‘
 Visit root i.e. ‗*‘
 Visit root i.e., ‗+‘
 Since right (T) is not empty ; current T becomes rooted at ‗*‘
 Since left (T) is not empty ; current T becomes rooted at ‗D‘
 Since left (T) and right (T) are empty we visit root i.e., ‗D‘
 Since right (T) is not empty ; current T becomes rooted at ‗E‘.
 Since left (T) & right (T) are empty ; we visit root i.e., ‗E‘
 Visit root is ‗*‘
 Visit root is ‗-‗
Therefore, the complete listing is ABC*+DE*- . We may note that expression is in postfix
notation.

3.4 Expression Tree


A binary tree can be used to represent arithmetic expressions if the node value can be
either operators or operand values and are such that
 Each operator node has exactly two branches.
 Each operand node has no branches; such trees are called expression trees.

Construction of expression tree


For constructing expression tree we use a stack. We loop through input expression and do
following for every character.
 If character is operand push that into stack.
 If character is operator pop two values from stack make them its child and push current
node again.
 At the end only element of stack will be root of expression tree.

SMVEC- Department of Information Technology 9


IT T33- Data Structures

Example: The input is: a b + c d e + * *


1. Since the first two symbols are operands, one- 2.The next symbol is a '+'. It pops the two pointers
node trees are created and pointers are pushed to to the trees, a new tree is formed, and a pointer to
them onto a stack. For convenience the stack will it is pushed onto the stack.
grow from left to right.

3.Next, c, d, and e are read. A one-node tree is 4.Continuing, a '+' is read, and it merges the last
created for each and a pointer to the two trees.
corresponding tree is pushed onto the stack

5. Now, a '*' is read. The last two tree pointers are 6.Finally, the last symbol is read. The two trees
popped and a new tree is formed with a '*' as the are merged and a pointer to the final tree remain
root on the stack

SMVEC- Department of Information Technology 10


IT T33- Data Structures
3.5 Game Trees
 General trees prove to be a useful strategy for planning moves in a game.
 Eliminating a potential move eliminates the entire subtree as a set of possible scenarios
 In game theory, a game tree is a directed graph whose nodes are positions in a game and
whose edges are moves. The complete game tree for a game is the game tree starting at
the initial position and containing all possible moves from each position.
The diagram shows the first two levels, or ply, in the game tree for tic-tac-toe.

 We consider all the rotations and reflections of positions as being equivalent, so the first
player has three choices of move: in the centre, at the edge, or in the corner. The second
player has two choices for the reply if the first player played in the centre, otherwise five
choices and so on.
 The number of leaf nodes in the complete game tree is called the game tree complexity. It is
the number of possible different ways the game can be played. The game tree complexity for
tic-tac-toe is 26,830.
 Game trees are important in artificial intelligence because one way to pick the best move in a
game is to search the game tree using the min-max algorithm or its variants.

3.6 Binary Search Tree(BST)


A binary search tree is a binary tree which is either empty or satisfies the following properties:
 Every node has a value and no two nodes have the same value.
 If there exists a left child or left subtree then its value is less than the value of the root.
 The values in the right child or right subtree is larger than the value of the root.

Operations of binary search tree(BST)

 Insertion
 Deletion
 Searching
 Find min, find max

SMVEC- Department of Information Technology 11


IT T33- Data Structures
Structure of node in binary search tree:

Struct BSTnode
{
Int data;
Struct BSTnode *left, *right;
}BSTnode;

3.6.1 Insertion:

 Check whether the root node of the binary search tree is NULL.
 If the condition is true, it mean, that the binary search tree is empty hence consider the new
node as the root node.
 Compare the new node data with root node data, for the following three condition
 if the content of the new node is equal to the root node insertion operation is
terminated.

 If the content of the new node is lesser than the root node data. Check whether
the left child of the root node is null. If it is true insert the new node.

 If the content of the newnode data is greater than the root node data, check
whether the right child of the root node is NULL. If the condition is true, insert the
new data.

advantages of binary search tree?

 Since the elements in the tree are ordered ,searching is faster and easier
 Insertion and deletion are also fast.

SMVEC- Department of Information Technology 12


IT T33- Data Structures

BSTnode *insert(BST *T, int x)


{
If(T==NULL)
{
T=(BSTnode*)malloc(sizeof(BSTnode));
T->data=x;
T->left=NULL;
T->right=NULL;
}
if(x > T -> data)
{
T -> right = insert (T -> right, x);
Return(T);
}
Else
{
T -> left = insert(T -> left, x);
Return (T);
}
}

3.6.2 Deletion:

Memory is to be released for the node to be deleted. A node can be deleted from the tree from
three different places namely,

 Deleting the leaf node


 Deleting the node with only one child
 Deleting the node with two children.

Deleting the Leaf node:

 Search the parent of the leaf node, and make the link to the leaf node or NULL.
 Release the memory for the deleted node

Deleting the node with only one child:

 Search the parent of the node to be deleted(with only one child)


 Assign the link of the parent node to the child of the node to be deleted.
 Release the memory for the deleted node.

SMVEC- Department of Information Technology 13


IT T33- Data Structures
Deleting the node with the two children:

 Search the parent of the node to be deleted (with two children)


 Copy the content of the inorder successor to the node to be deleted.
 Delete the inorder successor node – if the inorder successor node has no child.
 Release the memory for the inorder successor node.

Deleting the node with two children

3.6.3 Searching:

 Check whether the root node of the binary search tree is NULL. If yes, binary search tree is
empty.
 If the binary search tree is not empty, compare the new node data with root node data for the
following three condition.
o If the content of the new node data is equal to the root node data, search
element is found.
o If the content of the new node data is lesser than the root node data,
check the left child.
o If the content of the new node data is greater than the root node data,
check the right child.

SMVEC- Department of Information Technology 14


IT T33- Data Structures

find(BSTnod *rroot, int x)


{
If ((root == NULL))
Return(NULL);
If(root -> data == x)
Return(root);
If (x > root -> data)
Return (find (root -> right), x));
Else
Return(find(root->left), x));
}

3.6.4 Find Min:

This function returns to address of the node with smallest value in the tree.

BSTnode *find min (BSTnode *T)


{
While (T -> left! = NULL)
T = T -> left;
Return(T);
}

3.6.5 Find Max:


This function returns the address of the node with largest value in the tree.

BSTnode find max(BSTnode *T)


{
While (T -> right! = NULL)
T = T -> right;
Return(T);
}

3.7 AVL tree

AVL tree: G.M. Adelson-Velskii and E.M. Landis, who discovered them in 1962.

 A binary search tree, in which the heights of the left and right sub-trees of the root
differ by at most 1.The left and right sub-trees of the root are again AVL trees
 An AVL tree may or (in certain cases) may not be balanced.
Calculating balance factor for AVL tree:

 AVL trees are height-balanced binary search trees. A height-balanced binary tree is a
binary tree such that, the left & right sub trees for any given node differ in height by no
more than one.

SMVEC- Department of Information Technology 15


IT T33- Data Structures
 The height of a node in a tree is the length of a longest path from the node to a leaf. The
height of a tree is the height of its root.
 An AVL tree has balance factor calculated at every node:

Balance factor= Height(left sub-tree)-Height(right sub-tree)

 In binary search tree every node has a balancing factor of -1 ,0,1 is said to be height
balanced.
 A node with any other balance factor is said to be unbalanced tree and thus required to
be rebalancing
Structure of AVL Node:

struct avlnode
{
int data;
struct avl_node *left;
struct avl_node *right;
}*root;

3.7.1 Inserting into an AVL Tree :

 The basic insertion algorithm is similar to that of a binary search tree; during insertion,
the new node is inserted as the leaf node, as nodes are randomly added or removed the
balance factor can get disturbed.
 Balance factor will get disturbed when, the new node is added to a sub-tree of the root, that
is higher than the other sub-tree and the new node increases the height of the sub-tree.

Critical Node: Initially the node was heavy (either left or right) and the new node has been
inserted in the heavy sub-tree thereby creating an unbalanced sub-tree. Such a node is said to be
a critical node.

3.7.2 Imbalanced Conditions:


 Addition of a node affects the AVL property, that is the height value is greater than -1
 These imbalance conditions can be reduced to one of four reference cases,
Case1: Addition in left sub-tree of left child (Left-Left Imbalance).
Case2: Addition in right sub-tree of left child (Left-Right Imbalance).
Case3: Addition in right sub-tree of right child (Right-Right Imbalance).
Case 4: Addition in left sub-tree of right child (Right-Left Imbalance).

SMVEC- Department of Information Technology 16


IT T33- Data Structures
Case – I: Left-Left Imbalance Case – II: Left-Right Imbalance

Case – III: Right-Right Imbalance Case – IV: Right-Left Imbalance

3.7.3 Restoring Balance:


Balance, in either of the cases can be restored by means of a process termed rotation
 In cases I & III balance is restored by means of single rotation(LL,RR)
 In cases II & IV balance is restored by means of double rotation (LR,RL)
LL rotations: Inserted node is in the left sub-tree of the left sub-tree of root node A.

RR rotations: Inserted node is in the right sub-tree of the right sub-tree of root node A.

LR rotations: Inserted node is in the right sub-tree of the left sub-tree of root node A.

RL rotations: Inserted node is in the left sub-tree of the right sub-tree of root node A.

3.7.4 LL Rotation:

An LL imbalance occurs at a node A such that A has a balance factor -2 and a left child B with a
balance factor -1 or 0, this type of imbalance can be fixed by performing a single right rotation
at A.

SMVEC- Department of Information Technology 17


IT T33- Data Structures
avl_node*avlTree::LLrotation(avlnode*parent)
{
avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}

3.7.5 RR Rotation:
An RR imbalance occurs at a node A such that A has a balance factor +2 and a right child B
with a balance factor +1 or 0, this type of imbalance can be fixed by performing a single left
rotation at A.

avlnode*avlTree::RR_rotation(avlnode*parent)
{
avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}

3.7.6 LR Rotation:

 An LR imbalance occurs at a node A such that A has a balance factor -2 and a left child B
with a balance factor +1,
Assume B‘s right child is C. This type of imbalance can be fixed by performing a double
rotation at A (first a single left rotation at B and then a single right rotation at A)

SMVEC- Department of Information Technology 18


IT T33- Data Structures
avlnode*avlTree::LR_rotation(avlnode *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}

3.7.7 RL Rotation:

 An RL imbalance occurs at a node A such that A has a balance factor +2 and a right child
B with a balance factor -1,
 Assume B‘s left child is C. This type of imbalance can be fixed by performing a double
rotation at A (first a single right rotation at B and then a single left rotation at A)
avlnode*avlTree::RL_rotation(avlnode*parent)
{
avl_node *temp;
temp = parent->right;
parent->right = ll_rotation (temp);
return rr_rotation (parent);
}

3.7.8 Deletion in AVL Tree:

 Search for the key to be deleted.


 If the key is not contained, we are done.
Otherwise we distinguish three cases:
(a) The node to be deleted has no internal nodes as its children.
(b) The node to be deleted has exactly one internal child node.
(c) The node to be deleted has two internal children.
 After deleting a node the AVL property may be violated (similar to insertion).This must
be fixed appropriately.

SMVEC- Department of Information Technology 19


IT T33- Data Structures
3.8 Splay Trees

Splay Tree is a self - adjusted Binary Search Tree in which every operation on an element
rearrange the tree so that the element is placed at the root position of the tree.

 In a splay tree, every operation is performed at root of the tree. All the operations on a
splay tree are involved with a common operation called "Splaying".
 Splaying an element is the process of bringing it to the root position by performing
suitable rotation operations.

In a splay tree, splaying an element rearrange all the elements in the tree so that splayed element
is placed at root of the tree. With the help of splaying an element we can bring most frequently
used element closer to the root of the tree so that any operation on those element performed
quickly. That means the splaying operation automatically brings more frequently used elements
closer to the root of the tree.

Advantages
 A splay tree gives good performance for search, insert and delete operations
 Splay trees are considerably simpler to implement than other self-balancing binary search
trees,
 Splay tree minimizes memory requirements
 Splay trees gives good performance (amortized O(log n))

Disadvantages
 While sequentially accessing all the nodes of the tree tree becomes completely unbalanced
 For uniform access, the performance of a splay tree will be considerably worse.

Rotations in Splay Tree

 Zig Rotation
 Zag Rotation
 Zig - Zig Rotation
 Zag - Zag Rotation
 Zig - Zag Rotation
 Zag - Zig Rotation

3.8.1 Zig Rotation

The Zig Rotation in a splay tree is similar to the single right rotation in AVL Tree rotations. In
zig rotation every node moves one position to the right from its current position.

SMVEC- Department of Information Technology 20


IT T33- Data Structures
3.8.2 Zag Rotation

The Zag Rotation in a splay tree is similar to the single left rotation in AVL Tree
rotations. In zag rotation every node moves one position to the left from its current position.
Consider the following example...

3.8.3 Zig-Zig Rotation


The Zig-Zig Rotation in a splay tree is a double zig rotation. In zig-zig rotation every node moves
two position to the right from its current position.

3.8.4 Zag-Zag Rotation


The Zag-Zag Rotation in a splay tree is a double zag rotation. In zag-zag rotation every node
moves two position to the left from its current position.

3.8.5 Zig-Zag Rotation


The Zig-Zag Rotation in a splay tree is a sequence of zig rotation followed by zag rotation. In zig-
zag rotation every node moves one position to the right followed by one position to the left from its
current position.

SMVEC- Department of Information Technology 21


IT T33- Data Structures
3.8.6 Zag-Zig Rotation
The Zag-Zig Rotation in a splay tree is a sequence of zag rotation followed by zig rotation. In zag-
zig rotation every node moves one position to the left followed by one position to the right from its
current position.

Note: Every Splay tree must be a binary search tree but it is need not to be balanced tree.

3.8.7 Insertion Operation in Splay Tree

The insertion operation in Splay tree is performed using following steps.


Step 1: Check whether tree is Empty.
Step 2: If tree is Empty then insert the newNode as Root node and exit from the operation.
Step 3: If tree is not Empty then insert the newNode as a leaf node using Binary Search tree
insertion logic.
Step 4: After insertion, Splay the newNode

3.8.8 Deletion Operation in Splay Tree

In a Splay Tree, the deletion operation is similar to deletion operation in Binary Search
Tree. But before deleting the element first we need to splay that node then delete it from the root
position then join the remaining tree.

3.9 B-Trees in detail

B-Tree:

 A B-tree is a specialized m- way tree that is widely used for disk access.
 A B tree of order m can have maximum m-1 keys and m pointers to its sub-trees.
 A B-tree may contain a large number of key values and pointers to sub-trees.
 A B-tree is designed to store sorted data and allows search, insert, and delete operations.

3.9.1 Properties of B-Tree


 Every node has at most m children.
 Every node (except root and leaves) has at least ceil(m⁄2) children.
 The root has at least two children if it is not a leaf node.
 All leaves appear in the same level, and carry information.
 A non-leaf node with k children contains k–1 key
 Each leaf node (other than the root node if it is a leaf) must contain at least ceil(m / 2) - 1
keys
 Keys and sub-trees are arranged in the fashion of search tree

SMVEC- Department of Information Technology 22


IT T33- Data Structures
3.9.2 Search in a B-Tree:

 Searching is similar to searching a binary search tree.


 Starting at the root, the tree is recursively traversed from top to bottom. At each level, the
search chooses the child pointer (sub-tree) whose separation values are on either side of
the search value.
 Binary search is typically (but not necessarily) used within nodes to find the separation
values and child tree of interest.

3.9.3 Insertion in B-Tree:

All insertions start at a leaf node. To insert a new element, search the tree to find the leaf node
where the new element should be added. Insert the new element into that node with the following
steps:
1. Insert the new element in the node, keeping the node's elements ordered.
2. Otherwise the node is full, evenly split it into two nodes so:
o A single median is chosen from among the leaf's elements and the new element.
o Values less than the median are put in the new left node and values greater than the
median are put in the new right node, with the median acting as a separation value.
o The separation value is inserted in the node's parent, which may cause it to be split,
and so on. If the node has no parent (i.e., the node was the root), create a new root
above this node (increasing the height of the tree).
3.9.4 Deletion in B-Tree:

 Like insertion, deletion must be on a leaf node. If the key to be deleted is not in a leaf, swap
it with either its successor or predecessor (each will be in a leaf).
 The successor of a key k is the smallest key greater than k.
 The predecessor of a key k is the largest key smaller than k.
 In a B-tree the successor and predecessor, if any, of any key is in a leaf node

Deletion Algorithm:
1. If the key k is in node x and x is a leaf, delete the key k from x.
2. If the key k is in node x and x is an internal node, do the following:
a. If the child y that precedes k in node x has at least t keys, then find the predecessor k' of
k in the subtree rooted at y. Recursively delete k', and replace k by k' in x. (Finding k' and
deleting it can be performed in a single downward pass.)

SMVEC- Department of Information Technology 23


IT T33- Data Structures
b. Symmetrically, if the child z that follows k in node x has at least t keys, then find the
successor k' of k in the subtree rooted at z. Recursively delete k', and replace k by k' in x.
(Finding le and deleting it can be performed in a single downward pass.)
c. Otherwise, if both y and z have only t - 1 keys, merge k and all of z into y, so that x loses
both k and the pointer to z, and y now contains 2t - 1 keys. Then, free z and recursively
delete k from y.

Example : Construct a B-Tree with order m=3 for the key values 2, 3, 7, 9, 5, 6, 4, 8, 1 and
delete the values 4 and 6. Show the tree in performing all operations.

Since M=3
Key = (M-1) = 3-1 = 2
Pointers = M = 3

Insert 2 ,3 Insert 7 since no space split


into two child

Insert 9 Insert 5 since no space place


the center value 7 in root node

Insert 6 Insert 4 since no space place Again no space in the root so


the center value 5 in root move the center value 5 as
root

SMVEC- Department of Information Technology 24


IT T33- Data Structures
Insert 8

Node has too few keys so get the left sibling


Delete 4

Node has too few keys so get the right sibling

Delete 6
Finally we get a balanced B- tree

3.10 B+Trees

A B+ tree is a balanced tree in which every path from the root of the tree to a leaf is of the
same length, and each non-leaf node of the tree has between [n/2] and [n] children, where n is
fixed for a particular tree. It contains index pages and data pages. The capacity of a leaf has to be
50% or more.

For example: if n = 4, then the key for each node is between 2 to 4.


Index page will be 4 + 1 = 5.
Example of a B+ tree

SMVEC- Department of Information Technology 25


IT T33- Data Structures
3.10.1 Properties of B+trees
• Balanced
• Every node except root must be at least ½ full.
• Order: the minimum number of keys/pointers in a non-leaf node
• Fan-out of a node: the number of pointers out of the node

Operations in B+ Tree

• Search:
o logd(n) – Where d is the order, and n is the number of entries
• Insertion:
o Find the leaf to insert into
o If full, split the node, and adjust index accordingly
o Similar cost as searching
• Deletion
o Find the leaf node
o Delete
o May not remain half-full; must adjust the index accordingly

Search: Since no structure change in a B+ tree during a searching process, so just compare the
key value with the data in the tree, then give the result back.

For example: find the value 45, and 15 in below tree.


Result:
1. For the value of 45, not found.
2. For the value of 15, return the position
where the pointer located.

3.10.2 Insertion

 Since insert a value into a B+ tree may cause the tree unbalance, so rearrange the tree if
needed.

SMVEC- Department of Information Technology 26


IT T33- Data Structures

3.10.3 Deletion algorithm

• Descend to the leaf where the key exists.


• Remove the required key and associated reference from the node.
• If the node still has enough keys and references to satisfy the invariants, stop.
• If the node has too few keys to satisfy the invariants, but its next oldest or next youngest
sibling at the same level has more than necessary, distribute the keys between this node and
the neighbor. Repair the keys in the level above to represent that these nodes now have a
different ―split point‖ between them; this involves simply changing a key in the levels above,
without deletion or insertion.
• If the node has too few keys to satisfy the invariant, and the next oldest or next youngest
sibling is at the minimum for the invariant, then merge the node with its sibling; if the node is a
non-leaf, we will need to incorporate the ―split key‖ from the parent into our merging. In either
case, we will need to repeat the removal algorithm on the parent node to remove the ―split
key‖ that previously separated these merged nodes — unless the parent is the root and we
are removing the final key from the root, in which case the merged node becomes the new
root (and the tree has become one level shorter than before).

SMVEC- Department of Information Technology 27


IT T33- Data Structures

3.11 Tries: Tries are an extremely special and useful data-structure that are based on the prefix
of a string. They are used to represent the ―Retrieval” of data and thus the name Trie.

Prefix : The prefix of a string is nothing but any n letters n≤|S| that can be considered beginning
strictly from the starting of a string. For example , the word ―abacaba‖ has the following prefixes:

a
ab
aba
abac
abaca
abacab

A Trie is a special data structure used to store strings that can be visualized like a graph. It
consists of nodes and edges. Each node consists of at max 26 children and edges connect each
parent node to its children. These 26 pointers are nothing but pointers for each of the 26 letters of
the English alphabet A separate edge is maintained for every edge.

Strings are stored in a top to bottom manner on the basis of their prefix in a trie. All prefixes
of length 1 are stored at until level 1, all prefixes of length 2 are sorted at until level 2 and so on.

SMVEC- Department of Information Technology 28


IT T33- Data Structures
Two Marks

1. Define a tree
A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a
distinguished node r, called the root, and zero or more nonempty (sub) trees T1, T2,…,Tk, each of
whose roots are connected by a directed edge from r.

2. Define root
This is the unique node in the tree to which further sub-trees are attached.

Here, A is the root.


3. Define degree of the node
The total number of sub-trees attached to that node is called the degree of the node.

For node A, the degree is 2 and for B and C, the degree is 0.

4. Define leaves
These are the terminal nodes of the tree. The nodes with degree 0 are always the leaves.

Here, B and C are leaf nodes.

5. Define internal nodes


The nodes other than the root and the leaves are called internal nodes.

Here, C is the internal node.

6. Define parent node


The node which is having further sub-branches is called the parent node of those
sub-branches.

SMVEC- Department of Information Technology 29


IT T33- Data Structures

Here, node C is the parent node of D and E

7. Define depth and height of a node


For any node ni, the depth of ni is the length of the unique path from the root to ni. The
height of ni is the length of the longest path from ni to a leaf.

8. Define depth and height of a tree


The depth of the tree is the depth of the deepest leaf. The height of the tree is equal to the
height of the root. Always depth of the tree is equal to height of the tree.

9. What do you mean by level of the tree?


The root node is always considered at level zero, then its adjacent children are supposed
to be at level 1 and so on.
Here, node A is at level 0, nodes B and C are at level 1 and nodes D and E are at level 2.

10. Define forest


A tree may be defined as a forest in which only a single node (root) has no predecessors.
Any forest consists of a collection of trees.

11. Define a binary tree (May 2014)


A binary tree is a finite set of nodes which is either empty or consists of a root and two
disjoint binary trees called the left sub-tree and right sub-tree.

12. Define a path in a tree


A path in a tree is a sequence of distinct nodes in which successive nodes are connected
by edges in the tree.

13. Define a full binary tree


A full binary tree is a tree in which all the leaves are on the same level and every non-leaf
node has exactly two children.

14. Define a complete binary tree


A complete binary tree is a tree in which every non-leaf node has exactly two children not
necessarily to be on the same level.

SMVEC- Department of Information Technology 30


IT T33- Data Structures
15. State the properties of a binary tree
• The maximum number of nodes on level n of a binary tree is 2n-1, where n≥1.
• The maximum number of nodes in a binary tree of height n is 2n-1, where n≥1.
• For any non-empty tree, nl=nd+1 where nl is the number of leaf nodes and nd is the
number of nodes of degree 2.

16. What is meant by binary tree traversal?


Traversing a binary tree means moving through all the nodes in the binary tree, visiting
each node in the tree only once.

17. What are the different binary tree traversal techniques?


• Preorder traversal
• Inorder traversal
• Postorder traversal
• Levelorder traversal

18. What are the tasks performed during inorder traversal? (November 2013)
• Traverse the left sub-tree
• Process the root node
• Traverse the right sub-tree

19. What are the tasks performed during postorder traversal? (November 2013)
• Traverse the left sub-tree
• Traverse the right sub-tree
• Process the root node

20. State the merits of linear representation of binary trees.


• Storage method is easy and can be easily implemented in arrays
• When the location of a parent/child node is known, other one can be determined easily
• It requires static memory allocation so it is easily implemented in all Programming
language

21. State the demerit of linear representation of binary trees.


Insertions and deletions in a node take an excessive amount of processing time due to data
movement up and down the array.

22. State the merit of linked representation of binary trees.


Insertions and deletions in a node involve no data movement except the rearrangement of
pointers, hence less processing time.

23. State the demerits of linked representation of binary trees.


• Given a node structure, it is difficult to determine its parent node
• Memory spaces are wasted for storing null pointers for the nodes, which have one or no
sub-trees
• It requires dynamic memory allocation, which is not possible in some programming
language

SMVEC- Department of Information Technology 31


IT T33- Data Structures
24. Define a binary search tree (December 2014)(November 2015)
A binary search tree is a special binary tree, which is either empty or it should satisfy the
following characteristics:
Every node has a value and no two nodes should have the same value i.e) the
values in the binary search tree are distinct
• The values in any left sub-tree is less than the value of its parent node
• The values in any right sub-tree is greater than the value of its parent node
• The left and right sub-trees of each node are again binary search trees

25. What is the use of threaded binary tree?


In threaded binary tree, the NULL pointers are replaced by some addresses. The left pointer
of the node points to its predecessor and the right pointer of the node points to its successor.

26. Traverse the given tree using Inorder, Preorder and Postorder traversals.

Inorder : D H B E A F C I G J
Preorder: A B D H E C F G I J
Postorder: H D E B F I J G C A

27. In the given binary tree, using array you can store the node 4 at which location?(May
2014)

At location 6
1 2 3 - - 4 - - 5

where LCn means Left Child of node n and RCn means Right Child of node n

28. Define AVL Tree. (November 2013)


An empty tree is height balanced. If T is a non-empty binary tree with TL and
TR as its left and right subtrees, then T is height balanced if
i) TL and TR are height balanced and
ii) │hL - hR│≤ 1
Where hL and hR are the heights of TL and TR respectively.

SMVEC- Department of Information Technology 32


IT T33- Data Structures
29. What do you mean by balanced trees? (November 2015)
Balanced trees have the structure of binary trees and obey binary search tree properties.
Apart from these properties, they have some special constraints, which differ from one data
structure to another. However, these constraints are aimed only at reducing the height of the tree,
because this factor determines the time complexity.
Eg: AVL trees, Splay trees.

30. What are the categories of AVL rotations? (April 2015)


Left-Left: The newly inserted node is in the left subtree of the left child of A.
Right-Right: The newly inserted node is in the right subtree of the right child of
Left-Right: The newly inserted node is in the right subtree of the left child of A.
Right-Left: The newly inserted node is in the left subtree of the right child of A.

31. What do you mean by balance factor of a node in AVL tree?


The height of left subtree minus height of right subtree is called balance factor of a node in
AVL tree.The balance factor may be either 0 or +1 or -1.The height of an empty tree is -1.

32. Define splay tree.


A splay tree is a binary search tree in which restructuring is done using a scheme called
splay. The splay is a heuristic method which moves a given vertex v to the root of the splay tree
using a sequence of rotations.

33. What is the idea behind splaying?


Splaying reduces the total accessing time if the most frequently accessed node is moved
towards the root. It does not require to maintain any information regarding the height or balance
factor and hence saves space and simplifies the code to some extent.

34. List the types of rotations available in Splay tree.


Let us assume that the splay is performed at vertex v, whose parent and grandparent are p
and g respectively. Then, the three rotations are named as:
Zig: If p is the root and v is the left child of p, then left-left rotation at p would suffice. This case
always terminates the splay as v reaches the root after this rotation.
Zig-Zig: If p is not the root, p is the left child and v is also a left child, then a left- left rotation at g
followed by a left-left rotation at p, brings v as an ancestor of g as well as p.
Zig-Zag: If p is not the root, p is the left child and v is a right child, perform a left-right rotation at g
and bring v as an ancestor of p as well as g.

35. Define Heap.


A heap is defined to be a complete binary tree with the property that the value of each node
is atleast as small as the value of its child nodes, if they exist. The root node of the heap has the
smallest value in the tree.

36. What is the minimum number of nodes in an AVL tree of height h?


The minimum number of nodes S(h), in an AVL tree of height h is given
by S(h)=S(h-1)+S(h-2)+1. For h=0, S(h)=1.

37. Define B-tree of order M.


A B-tree of order M is a tree that is not binary with the following structural properties:

SMVEC- Department of Information Technology 33


IT T33- Data Structures
• The root is either a leaf or has between 2 and M children.
• All non-leaf nodes (except the root) have between ┌M/2┐ and M children.
• All leaves are at the same depth.

38. What do you mean by 2-3 tree?


A B-tree of order 3 is called 2-3 tree. A B-tree of order 3 is a tree that is not binary with the
following structural properties: The root is either a leaf or has between 2 and 3 children.
• All non-leaf nodes (except the root) have between 2 and 3 children.
• All leaves are at the same depth.

39. What do you mean by 2-3-4 tree?


A B-tree of order 4 is called 2-3-4 tree. A B-tree of order 4 is a tree that is not binary with
the following structural properties:
• The root is either a leaf or has between 2 and 4 children.
• All non-leaf nodes (except the root) have between 2 and 4 children.
• All leaves are at the same depth.

40. What are the applications of B-tree? (April 2015)


• Database implementation
• Indexing on non primary key fields

41. Define expression trees?


The leaves of an expression tree are operands such as constants or variable names
and the other nodes contain operators.

SMVEC- Department of Information Technology 34


IT T33- Data Structures

Assignment Questions

1. Perform all the tree traversal on the following tree

2. Show the result of inserting 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty AVL tree. Prove that
the resulting tree is perfectly balanced.
3. Write a routine to perform insertion into a B-tree. b. Write a routine to perform deletion from
a B-tree. Construct a B-Tree with order m = 3.for the key values 2, 3, 7, 9, 5, 6, 4, 8, 1 and
delete the values 4 and 6. Show the tree in performing all operations.
4. Construct a binary search tree by inserting 30, 10, 4, 19, 62, 35, 28, 73 into an initially
empty tree.Show the results of splaying the nodes 4 and 62 one after the other of the
constructed tree.
5. Give the prefix, infix, and postfix expressions corresponding to the tree in the Figure

SMVEC- Department of Information Technology 35

Potrebbero piacerti anche