Unit 4 Tree
Unit 4 Tree
A tree is also one of the data structures that represent hierarchical data. Suppose we
want to show the employees and their positions in the hierarchical form then it can
be represented as shown below:
In the above structure, each node is labeled with some number. Each arrow shown
in the above figure is known as a link between the two nodes.
o Root: The root node is the topmost node in the tree hierarchy. In other
words, the root node is the one that doesn't have any parent. In the above
structure, node numbered 1 is the root node of the tree. If a node is directly
linked to some other node, it would be called a parent-child relationship.
o Child node: If the node is a descendant of any node, then the node is known
as a child node.
o Parent: If the node contains any sub-node, then that node is said to be the
parent of that sub-node.
o Sibling: The nodes that have the same parent are known as siblings.
o Leaf Node:- The node of the tree, which doesn't have any child node, is
called a leaf node. A leaf node is the bottom-most node of the tree. There
can be any number of leaf nodes present in a general tree. Leaf nodes can
also be called external nodes.
o Internal nodes: A node has atleast one child node known as an internal
o Ancestor node:- An ancestor of a node is any predecessor node on a path
from the root to that node. The root node doesn't have any ancestors. In the
tree shown in the above image, nodes 1, 2, and 5 are the ancestors of node
10.
o Descendant: The immediate successor of the given node is known as a
descendant of a node. In the above figure, 10 is the descendant of node 5.
Number of edges: If there are n nodes, then there would n-1 edges. Each
arrow in the structure represents the link or path. Each node, except the root
node, will have atleast one incoming link known as an edge. There would be
one link for the parent-child relationship.
Depth of node x: The depth of node x can be defined as the length of the
path from the root to the node x. One edge contributes one-unit length in the
path. So, the depth of node x can also be defined as the number of edges
between the root node and the node x. The root node has 0 depth.
Height of node x: The height of node x can be defined as the longest path
from the node x to the leaf node.
The above structure can only be defined for the binary trees because the binary
tree can have utmost two children, and generic trees can have more than two
children. The structure of the node for generic trees would be different as
compared to the binary tree.
o Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored
on the disc drive, the file and folder are in the form of the naturally
hierarchical data and stored in the form of trees.
o Organize data: It is used to organize data for efficient insertion, deletion
and searching. For example, a binary tree has a logN time for searching an
element.
o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast
and efficient way for dynamic spell checking.
o Heap: It is also a tree data structure implemented using arrays. It is used to
implement priority queues.
o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
o Routing table: The tree data structure is also used to store the data in
routing tables in the routers.
a) General tree: The general tree is one of the types of tree data structure. In
the general tree, a node can have either 0 or maximum n number of nodes.
There is no restriction imposed on the degree of the node (the number of
nodes that a node can contain). The topmost node in a general tree is known
as a root node. The children of the parent node are known as subtrees.
b) Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In
a binary tree, each node in a tree can have utmost two child nodes. Here,
utmost means whether the node has 0 nodes, 1 node or 2 nodes.
c) Binary Search tree: Binary search tree is a non-linear data structure in which
one node is connected to n number of nodes. It is a node-based data
structure. A node can be represented in a binary search tree with three fields,
i.e., data part, left-child, and right-child. A node can be connected to the
utmost two child nodes in a binary search tree, so the node contains two
pointers (left child and right child pointer).
c) N-ary tree:
Generic trees are a collection of nodes where each node is a data structure that
consists of records and a list of references to its children(duplicate references are
not allowed). Unlike the linked list, each node stores the address of multiple nodes.
The Binary tree means that the node can have maximum two children. Here, binary
name itself suggests that 'two'; therefore, each node can have either 0, 1 or 2
children.
Properties of Binary tree:
struct node
{
int data,
struct node *left, *right;
}
In the above structure, data is the value, left pointer contains the address of the
left node, and right pointer contains the address of the right node.
1. Inorder :
Now, let's see an example of inorder traversal. It will be easier to understand the
procedure of inorder traversal using an example.
The nodes with yellow color are not visited yet. Now, we will traverse the nodes of
the above tree using inorder traversal.
o Here, 40 is the root node. We move to the left subtree of 40, that is 30, and it
also has subtree 25, so we again move to the left subtree of 25 that is 15.
Here, 15 has no subtree, so print 15 and move towards its parent node, 25.
o Now, print 25 and move to the right subtree of 25.
o Now recursively traverse the right subtree of 50 that is 60. 60 have subtree
so first traverse the left subtree of 60 that is 55. 55 has no children, so print
55 and move to its root node.
o Now print 60 and move to the right subtree of 60 that is 70.
{15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70}
2. Preorder:
In preorder traversal, first, root node is visited, then left sub-tree and after that right
sub-tree is visited. The process of preorder traversal can be represented as -
root → left → right
Root node is always traversed first in preorder traversal, while it is the last item of
postorder traversal. Preorder traversal is used to get the prefix expression of a tree.
Now, let's see an example of preorder traversal. It will be easier to understand the
process of preorder traversal using an example.
The nodes with yellow color are not visited yet. Now, we will traverse the nodes of
the above tree using preorder traversal.
o Start with the root node 40. First, print 40 and then recursively traverse the
left subtree.
o Now, move to the left subtree. For left subtree, the root node is 30. Print 30,
and move towards the left subtree of 30.
o In left subtree of 30, there is an element 25, so print 25, and traverse the left
subtree of 25.
o In left subtree of 25, there is an element 15, and 15 has no subtree. So, print
15, and move to the right subtree of 25.
o In right subtree of 25, there is 28, and 28 has no subtree. So, print 28, and
move to the right subtree of 30.
o In right subtree of 30, there is 35 that has no subtree. So print 35, and
traverse the right subtree of 40.
o In the right subtree of 40, there is 50. Print 50, and traverse the left subtree
of 50.
o In the left subtree of 50, there is 45 that do not have any child. So, print 45,
and traverse the right subtree of 50.
o In right subtree of 50, there is 60. Print 60 and traverse the left subtree of
60.
o In the left subtree of 60, there is 55 that does not have any child. So, print
55 and move to the right subtree of 60.
o In the right subtree of 60, there is 70 that do not have any child. So, print
70 and stop the process.
40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70
3. Postorder:
The postorder traversal is one of the traversing techniques used for visiting the
node in the tree. It follows the principle LRN (Left-right-node). Postorder
traversal is used to get the postfix expression of a tree.
The nodes with yellow color are not visited yet. Now, we will traverse the nodes of
above tree using postorder traversal.
o Here, 40 is the root node. We first visit the left subtree of 40, i.e., 30. Node
30 will also traverse in post order. 25 is the left subtree of 30, so it is also
traversed in post order. Then 15 is the left subtree of 25. But 15 has no
subtree, so print 15 and move towards the right subtree of 25.
o 28 is the right subtree of 25, and it has no children. So, print 28.
o Now, print 25, and the postorder traversal for 25 is finished.
o Next, move towards the right subtree of 30. 35 is the right subtree of 30, and
it has no children. So, print 35.
o After that, print 30, and the postorder traversal for 30 is finished. So, the left
subtree of given binary tree is traversed.
o Now, move towards the right subtree of 40 that is 50, and it will also
traverse in post order. 45 is the left subtree of 50, and it has no children.
So, print 45 and move towards the right subtree of 50.
o 60 is the right subtree of 50, which will also be traversed in post order. 55 is
the left subtree of 60 that has no children. So, print 55.
o Now, print 50, and the post order traversal for 50 is completed.
o At last, print 40, which is the root node of the given binary tree, and the
post order traversal for node 40 is completed.
{15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40}
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert it as the
root of the left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as the
root of the right subtree.
Now, let's see the process of creating the Binary search tree using the given data
element. The process of creating the BST is shown below -
As 15 is smaller than 45, so insert it as the root node of the left subtree.
Step 3 - Insert 79.
As 79 is greater than 45, so insert it as the root node of the right subtree.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of
79.
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right
subtree of 10.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree
of 15.
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root
element, if it is smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.
Now, let's understand the searching in binary tree using an example. We are taking
the binary search tree formed above. Suppose we have to find node 20 from the
below tree.
Step1:
Step2:
Step3:
Deletion in Binary Search tree:
In a binary search tree, we must delete a node from the tree by keeping in mind
that the property of BST is not violated. To delete a node from BST, there are three
possible situations occur -
It is the simplest case to delete a node in BST. Here, we have to replace the leaf
node with NULL and simply free the allocated space.
We can see the process to delete a leaf node from BST in the below image. In
below image, suppose we have to delete node 90, as the node to be deleted is a leaf
node, so it will be replaced with NULL, and the allocated space will free.
When the node to be deleted has only one child
In this case, we have to replace the target node with its child, and then delete the
child node. It means that after replacing the target node with its child node, the
child node will now contain the value to be deleted. So, we simply have to replace
the child node with NULL and free up the allocated space.
We can see the process of deleting a node with one child from BST in the below
image. In the below image, suppose we have to delete the node 79, as the node to
be deleted has only one child, so it will be replaced with its child 55.
So, the replaced node 79 will now be a leaf node that can be easily deleted.
This case of deleting a node in BST is a bit complex among other two cases. In
such a case, the steps to be followed are listed as follows -
We can see the process of deleting a node with two children from BST in the
below image. In the below image, suppose we have to delete node 45 that is the
root node, as the node to be deleted has two children, so it will be replaced with its
inorder successor. Now, node 45 will be at the leaf of the tree so that it can be
deleted easily.
A new key in BST is always inserted at the leaf. To insert an element in BST, we
have to start searching from the root node; if the node to be inserted is less than the
root node, then search for an empty location in the left subtree. Else, search for the
empty location in the right subtree and insert the data. Insert in BST is similar to
searching, as we always have to maintain the rule that the left subtree is smaller
than the root, and right subtree is larger than the root.
Now, let's see the process of inserting a node into BST using an example.
11.Explain in detail on AVL tree.
AVL Tree can be defined as height balanced binary search tree in which each
node is associated with a balance factor which is calculated by subtracting the
height of its right sub-tree from that of its left sub-tree.
SN Operatio Description
n
When BST becomes unbalanced, due to a node is inserted into the right subtree of
the right subtree of A, then we perform RR rotation, RR rotation is an
anticlockwise rotation, which is applied on the edge below a node having balance
factor -2
In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of
the left subtree of C, then we perform LL rotation, LL rotation is clockwise
rotation, which is applied on the edge below a node having balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted in the
left subtree of C left subtree. We perform the LL rotation on the edge below A.
Min Heap: The value of the parent node should be less than or equal to either of
its children.
Max Heap: The value of the parent node is greater than or equal to its children.
Searching :
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left
sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.
Searching in a B tree depends upon the height of the tree. The search algorithm
takes O(log n) time to search any element in a B tree.
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be
followed in order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the
node can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
Now, 57 is the only element which is left in the node, the minimum number of
elements that must be present in a B tree of order 5, is 2. it is less than that, the
elements in its left and right sub-tree are also not sufficient therefore, merge it with
the left sibling and intervening element of parent i.e. 49.
Advertisement
1.
nsert the node 8 into the B Tree of order 5 shown in the following image.
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can
either be a leaf node or an internal node. Following algorithm needs to be followed
in order to delete a node from a B tree.
If the the node which is to be deleted is an internal node, then replace the node
with its in-order successor or predecessor. Since, successor or predecessor will
always be on the leaf node hence, the process will be similar as the node is being
deleted from the leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
Now, 57 is the only element which is left in the node, the minimum number of
elements that must be present in a B tree of order 5, is 2. it is less than that, the
elements in its left and right sub-tree are also not sufficient therefore, merge it with
the left sibling and intervening element of parent i.e. 49.
Applications of B tree:
B tree is used to index the data and provides fast access to the actual data stored on
the disks since, the access to value stored in a large database that is stored on a disk
is a very time consuming process.
In B Tree, Keys and records both can be stored in the internal as well as leaf nodes.
Whereas, in B+ tree, records (data) can only be stored on the leaf nodes while
internal nodes can only store the key values.
The leaf nodes of a B+ tree are linked together in the form of a singly linked lists
to make the search queries more efficient.
B+ Tree are used to store the large amount of data which can not be stored in the
main memory. Due to the fact that, size of main memory is always limited, the
internal nodes (keys to access records) of the B+ tree are stored in the main
memory whereas, leaf nodes are stored in the secondary memory.
The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is
shown in the following figure.
Advantages of B+ Tree
Insertion in B+ Tree
Step 1: Insert the new node as a leaf node
Step 2: If the leaf doesn't have required space, split the node and copy the middle
node to the next index node.
Step 3: If the index node doesn't have required space, split the node and copy the
middle element to the next index page.
Example :
Insert the value 195 into the B+ tree of order 5 shown in the following figure.
195 will be inserted in the right sub-tree of 120 after 190. Insert it at the desired
position.
The node contains greater than the maximum number of elements i.e. 4, therefore
split it and place the median node up to the parent.
Now, the index node contains 6 children and 5 keys which violates the B+ tree
properties, therefore we need to split it, shown as follows.
Deletion in B+ Tree
Step 1: Delete the key and data from the leaves.
Step 2: if the leaf node contains less than minimum number of elements, merge
down the node with its sibling and delete the key in between them.
Step 3: if the index node contains less than minimum number of elements, merge
the node with the sibling and move down the key in between them.
Example
Delete the key 200 from the B+ Tree shown in the following figure.
200 is present in the right sub-tree of 190, after 195. delete it.
Merge the two nodes by using 195, 190, 154 and 129.
Now, element 120 is the single element present in the node which is violating the
B+ Tree properties. Therefore, we need to merge it by using 60, 78, 108 and 120.