Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
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.
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,
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
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.
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
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
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.
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.
+ *
A * D E
B C
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
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.
Insertion
Deletion
Searching
Find min, find max
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.
Since the elements in the tree are ordered ,searching is faster and easier
Insertion and deletion are also fast.
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,
Search the parent of the leaf node, and make the link to the leaf node or NULL.
Release the memory for the deleted node
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.
This function returns to address of the node with smallest value in the 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.
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;
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.
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.
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)
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);
}
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.
Zig Rotation
Zag Rotation
Zig - Zig Rotation
Zag - Zag Rotation
Zig - Zag Rotation
Zag - 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.
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...
Note: Every Splay tree must be a binary search tree but it is need not to be balanced 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.
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.
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.)
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
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.
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.
3.10.2 Insertion
Since insert a value into a B+ tree may cause the tree unbalance, so rearrange the tree if
needed.
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.
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.
4. Define leaves
These are the terminal nodes of the tree. The nodes with degree 0 are always the leaves.
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
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
Assignment Questions
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