Tree
Tree
Trupesh
Patel’s Notes
Directed Tree:
❖ A directed tree is an acyclic digraph which has one node called its root with in degree 0, while
all other nodes have in degree 1.
❖ Every directed tree must have at least one node.
❖ An isolated node is also a directed tree.
Root Node
Types of representation:
❖ Venn Diagram
❖ Nesting of Parenthesis
❖ Level Format
Binary tree
Inorder:
Inorder: 1 2 3 4 5 6 Inorder: 1 2 3 4 5 6
Preorder:
Preorder: 4 2 1 3 5 6 Preorder: 4 2 1 3 5 6
Postorder:
Postorder: 1 3 2 6 5 4 Postorder: 1 3 2 6 5 A
Example:
You are given two traversal Inorder and Preorder of Binary seach tree
as: Inorder: g d h b e a f j c Preorder : a b d g h e c f j
Solution:
Inorder: g d h b e a f j c
Preorder: a b d g h e c f j
Step 1
g dhb f j c
e
❖ When we find the far-left leaf, we must begin backtracking to process the right subtrees
we passed on our way down. This is especially inefficient when the parent node has no
right subtree.
Searches:
Find the smallest node: we can see that smallest node is available at the leaf node of the BST, so to
search smallest node, we have to follow left sub branches until we get to a leaf.
Find the Smallest node in BST
Algorithm: findSmallestBST (root)
This algorithm finds the smallest node in a BST. Pre root is a pointer to a nonempty BST or subtree
Return address of smallest node.
1. if (left subtree empty)
1 return (root)
2. end if
3. return findSmallestBST (left subtree)
end findSmallestBST
Insertion:
The insert node function adds data to a BST. To insert data all we need to do is follow the branches to
an empty subtree and then insert the new node.
BST insertion
Deletion:
To delete a node from a binary search tree, we must first locate it. There are four possible cases
when we delete a node:
1. The node to be deleted has no children. In this case, all we need to do is delete the node.
2. The node to be deleted has only a right subtree. We delete the node and attach the right
subtree to the deleted node’s parent.
3. The node to be deleted has only a left subtree. We delete the node and attach the left subtree
to the deleted node’s parent.
Prof. Trupesh Patel’s Notes 14
DATA STRUCTURES & ALGORITHMS TREE
4. The node to be deleted has two subtrees. It is possible to delete a node from the middle of a
tree, but the result tends to create very unbalanced
AVL TREES
❖ 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.
❖ Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree
will be unbalanced and need to be balanced.
❖ Balance Factor (k) = height (left(k)) - height (right(k))
❖ If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right
sub-tree.
❖ If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal
height.
❖ If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right
sub-tree.
B TREE:
❖ B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of order m can
have at most m-1 keys and m children. One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key values by keeping the height of the tree
relatively small.
❖ A B tree of order m contains all the properties of an M way tree. In addition, it contains the following
properties.
1. Every node in a B-Tree contains at most m children.
2. Every node in a B-Tree except the root node and the leaf node contain at least m/2 children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.
It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.
❖ A B tree of order 4 is shown in the following image.
B+ TREE:
❖ 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 cannot 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.
B TREE vs B+ TREE: