Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

Unit 1b

A tree is a nonlinear hierarchical data structure that consists of nodes


connected by edges.

Why Tree Data Structure?


Other data structures such as arrays, linked list, stack, and queue are linear data
structures that store data sequentially. In order to perform any operation in a
linear data structure, the time complexity increases with the increase in the
data size. But, it is not acceptable in today's computational world.

Different tree data structures allow quicker and easier access to the data as it is
a non-linear data structure.

Tree Terminologies

Node
A node is an entity that contains a key or value and pointers to its child nodes.
The last nodes of each path are called leaf nodes or external nodes that do not
contain a link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge
It is the link between any two nodes.

Root
It is the topmost node of a tree.

Height of a Node
The height of a node is the number of edges from the node to the deepest leaf
(ie. the longest path from the node to a leaf node).

Depth of a Node
The depth of a node is the number of edges from the root to the node.

Height of a Tree
The height of a Tree is the height of the root node or the depth of the deepest
node.
Height and depth of each node in a tree

Degree of a Node
The degree of a node is the total number of branches of that node.

Forest
A collection of disjoint trees is called a forest.

Creating forest
from a tree
You can create a forest by cutting the root of a tree.
Tree Traversal
In order to perform any operation on a tree, you need to reach to the specific
node. The tree traversal algorithm helps in visiting a required node in the tree.

Tree Traversal - inorder, preorder and postorder


Traversing a tree means visiting every node in the tree. You might, for instance,
want to add all the values in the tree or find the largest one. For all these
operations, you will need to visit each node of the tree.

Linear data structures like arrays, stacks, queues, and linked list have only one


way to read the data. But a hierarchical data structure like a tree can be
traversed in different ways.

Tree traversal
Let's think about how we can read the elements of the tree in the image shown
above.

Starting from top, Left to right


1 -> 12 -> 5 -> 6 -> 9

Starting from bottom, Left to right

5 -> 6 -> 12 -> 9 -> 1

Although this process is somewhat easy, it doesn't respect the hierarchy of the
tree, only the depth of the nodes.

According to this structure, every tree is a combination of

 A node carrying data

 Two subtrees

Remember that our goal is to visit each node, so we need to visit all the nodes in
the subtree, visit the root node and visit all the nodes in the right subtree as
well.

Depending on the order in which we do this, there can be three types of


traversal.
Inorder traversal
1. First, visit all the nodes in the left subtree

2. Then the root node

3. Visit all the nodes in the right subtree

Preorder traversal
1. Visit root node

2. Visit all the nodes in the left subtree

3. Visit all the nodes in the right subtree

Postorder traversal
1. Visit all the nodes in the left subtree

2. Visit all the nodes in the right subtree

3. Visit the root node

Let's visualize in-order traversal. We start from the root node.


Left and Right Subtree
We traverse the left subtree first. We also need to remember to visit the root
node and the right subtree when this tree is done.

Following are the generally used ways for traversing trees.


 

Depth First Traversals: 


(a) Inorder (Left, Root, Right) : 4 2 5 1 3 
(b) Preorder (Root, Left, Right) : 1 2 4 5 3 
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Breadth First or Level Order Traversal : 1 2 3 4 5 
Depth First Search Traversal: 1 2 4 5 3
Consider the given binary tree,
         

 
Inorder Traversal: 7 9 4 2 5 1 3 6 8
Preorder Traversal: 1 2 4 7 9 5 3 6 8
Postorder Traversal: 9 7 4 5 2 8 6 3 1
 

Inorder Traversal: For binary search trees (BST), Inorder Traversal


specifies the nodes in non-descending order. In order to obtain nodes from BST in
non-increasing order, a variation of inorder traversal may be used where inorder
traversal is reversed.

Preorder Traversal: Preorder traversal will create a copy of the tree.


Preorder Traversal is also used to get the prefix expression of an expression.

Postorder Traversal: Postorder traversal is used to get the postfix


expression of an expression given
Tree Applications
 Binary Search Trees(BSTs) are used to quickly check whether an element is
present in a set or not.

 Heap is a kind of tree that is used for heap sort.

 A modified version of a tree called Tries is used in modern routers to store


routing information.

 Most popular databases use B-Trees and T-Trees, which are variants of the tree
structure we learned above to store their data

 Compilers use a syntax tree to validate the syntax of every program you write.

Types of Tree
1. Binary Tree
2. AVL Tree
3. Binary Search Tree
4. Balanced Tree
1. Binary Tree
A binary tree is a tree data structure in which each parent node can have at
most two children. For example,

Binary Tree

Types of Binary Tree

Full Binary Tree


A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
Full Binary Tree

Perfect Binary Tree


A perfect binary tree is a type of binary tree in which every internal node
has exactly two child nodes and all the leaf nodes are at the same level.

Perfect Binary Tree

Complete Binary Tree


A complete binary tree is just like a full binary tree, but with two major
differences

1. Every level must be completely filled

2. All the leaf elements must lean towards the left.


3. The last leaf element might not have a right sibling i.e. a complete
binary tree doesn't have to be a full binary tree.

Complete Binary Tree

Full Binary Tree vs Complete Binary Tree

Comparison between full binary tree and complete binary

tree Comparison between full binary tree and complete binary


tree Comparison between full binary tree and complete binary

tree Comparison between full binary tree and complete binary


tree

How a Complete Binary Tree is Created?


1. Select the first element of the list to be the root node.

(no. of elements on level-I: 1)


Select the first element as root

2. Put the second element as a left child of the root node and the third
element as the right child. (no. of elements on level-II: 2)

12 as a left child and 9 as a right child

3. Put the next two elements as children of the left node of the second
level. Again, put the next two elements as children of the right node
of the second level (no. of elements on level-III: 4) elements).

4. Keep repeating until you reach the last element.

5 as a left child and 6 as a right child

Degenerate or Pathological Tree


A degenerate or pathological tree is the tree having a single child either left
or right.

Degenerate Binary Tree

Skewed Binary Tree


A skewed binary tree is a pathological/degenerate tree in which the tree is
either dominated by the left nodes or the right nodes. Thus, there are two
types of skewed binary tree: left-skewed binary tree and right-skewed
binary tree.

Skewed Binary Tree

Balanced Binary Tree


It is a type of binary tree in which the difference between the height of the
left and the right subtree for each node is either 0 or 1.
Balanced Binary Tree

Binary Tree Representation


A node of a binary tree is represented by a structure containing a data part
and two pointers to other structures of the same type.

Binary Tree Representation


Balanced Binary Tree
A balanced binary tree, also referred to as a height-balanced binary tree, is
defined as a binary tree in which the height of the left and right subtree of
any node differ by not more than 1.

To learn more about the height of a tree/node, visit Tree Data Structure.


Following are the conditions for a height-balanced binary tree:
1. difference between the left and the right subtree for any node is not
more than one

2. the left subtree is balanced

3. the right subtree is balanced


Balanced Binary Tree with depth at each level

Unbalanced Binary Tree with depth


at each level

2. AVL Trees

What if the input to binary search tree comes in a sorted (ascending or descending)
manner? It will then look like this −
It is observed that BST's worst-case performance is closest to linear search
algorithms, that is Ο(n). In real-time data, we cannot predict data pattern and their
frequencies. So, a need arises to balance out the existing BST.
Named after their inventor Adelson, Velski & Landis, AVL trees are height
balancing binary search tree. AVL tree checks the height of the left and the right
sub-trees and assures that the difference is not more than 1. This difference is
called the Balance Factor.
Here we see that the first tree is balanced and the next two trees are not balanced −

In the second tree, the left subtree of C has height 2 and the right subtree has
height 0, so the difference is 2. In the third tree, the right subtree of A has height 2
and the left is missing, so it is 0, and the difference is 2 again. AVL tree permits
difference (balance factor) to be only 1.
BalanceFactor = height(left-subtree) − height(right-subtree)
If the difference in the height of left and right sub-trees is more than 1, the tree is
balanced using some rotation techniques.

AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations −

 Left rotation
 Right rotation
 Left-Right rotation
 Right-Left rotation
The first two rotations are single rotations and the next two rotations are double
rotations. To have an unbalanced tree, we at least need a tree of height 2. With this
simple tree, let's understand them one by one.

Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the
right subtree, then we perform a single left rotation −

In our example, node A has become unbalanced as a node is inserted in the right


subtree of A's right subtree. We perform the left rotation by making A the left-
subtree of B.

Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left
subtree. The tree then needs a right rotation.

As depicted, the unbalanced node becomes the right child of its left child by
performing a right rotation.

Left-Right Rotation
Double rotations are slightly complex version of already explained versions of
rotations. To understand them better, we should take note of each action performed
while rotation. Let's first check how to perform Left-Right rotation. A left-right
rotation is a combination of left rotation followed by right rotation.

State Action

A node has been inserted into the right subtree of the left subtree. This
makes C an unbalanced node. These scenarios cause AVL tree to
perform left-right rotation.

We first perform the left rotation on the left subtree of C. This makes A,
the left subtree of B.

Node C is still unbalanced, however now, it is because of the left-subtree


of the left-subtree.

We shall now right-rotate the tree, making B the new root node of this
subtree. C now becomes the right subtree of its own left subtree.
The tree is now balanced.

Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of right
rotation followed by left rotation.

State Action

A node has been inserted into the left subtree of the right subtree. This
makes A, an unbalanced node with balance factor 2.

First, we perform the right rotation along C node, making C the right


subtree of its own left subtree B. Now, B becomes the right subtree of A.

Node A is still unbalanced because of the right subtree of its right subtree
and requires a left rotation.
A left rotation is performed by making B the new root node of the
subtree. A becomes the left subtree of its right subtree B.

The tree is now balanced.

3.Binary Search Tree(BST)


Binary search tree is a data structure that quickly allows us to maintain a
sorted list of numbers.

 It is called a binary tree because each tree node has a maximum of


two children.

 It is called a search tree because it can be used to search for the


presence of a number in  O(log(n))  time.

The properties that separate a binary search tree from a regular binary


tree is
1. All nodes of left subtree are less than the root node

2. All nodes of right subtree are more than the root node

3. Both subtrees of each node are also BSTs i.e. they have the above
two properties
A tree having a right subtree with one value smaller than the root is shown to demonstrate
that it is not a valid binary search tree

Rules of Binary Search Tree


1. Binary Search tree can be defined as a class of binary trees, in which the
nodes are arranged in a specific order. This is also called ordered binary tree.

2. In a binary search tree, the value of all the nodes in the left sub-tree is less
than the value of the root.

3. Similarly, value of all the nodes in the right sub-tree is greater than or equal
to the value of the root.

4. This rule will be recursively applied to all the left and right sub-trees of the
root.
A Binary search tree is shown in the above figure. As the constraint applied on the
BST, we can see that the root node 30 doesn't contain any value greater than or
equal to 30 in its left sub-tree and it also doesn't contain any value less than 30 in
its right sub-tree.

Advantages of using binary search tree


1. Searching become very efficient in a binary search tree since, we get a hint at
each step, about which sub-tree contains the desired element.

2. The binary search tree is considered as efficient data structure in compare to


arrays and linked lists. In searching process, it removes half sub-tree at every
step. Searching for an element in a binary search tree takes o(log 2n) time. In
worst case, the time it takes to search an element is 0(n).
3. It also speed up the insertion and deletion operations as compare to that in
array and linked list.

Create the binary search tree using the following data


elements.
43, 10, 79, 90, 12, 54, 11, 9, 50

1. Insert 43 into the tree as the root of the tree.

2. Read the next element, if it is lesser than the root node element, insert it as
the root of the left sub-tree.

3. Otherwise, insert it as the root of the right of the right sub-tree.

The process of creating BST by using the given elements, is shown in the image
below.
Operations on Binary Search Tree
There are many operations which can be performed on a binary search tree.

SN Operation Description
1 Searching Finding the location of some specific element in a binary
in BST search tree.

2 Insertion in Adding a new element to the binary search tree at the


BST appropriate location so that the property of BST do not
violate.

3 Deletion in Deleting some specific node from a binary search tree.


BST However, there can be various cases in deletion depending
upon the number of children, the node have.

B-tree

B-tree is a special type of self-balancing search tree in which each node


can contain more than one key and can have more than two children. It is a
generalized form of the binary search tree. It is also known as a height-
balanced m-way tree.
B
-tree

Why B-tree?
The need for B-tree arose with the rise in the need for lesser time in
accessing the physical storage media like a hard disk. The secondary
storage devices are slower with a larger capacity. There was a need for
such types of data structures that minimize the disk accesses.

Other data structures such as a binary search tree, avl tree, red-black tree,
etc can store only one key in one node. If you have to store a large number
of keys, then the height of such trees becomes very large and the access
time increases.

However, B-tree can store many keys in a single node and can have
multiple child nodes. This decreases the height significantly allowing faster
disk accesses.
Operations
Searching Example

1. Let us search key  k = 17  in the tree below of degree 3.

B-tree
2. k  is not found in the root so, compare it with the root key.

k is not found on the


root node
3. Since  k > 11 , go to the right child of the root node.

Go to the right subtree


4. Compare k with 16. Since  k > 16 , compare k with the next key 18.

Compare with the keys


from left to right
5. Since  k < 18 , k lies between 16 and 18. Search in the right child of 16

or the left child of 18. k


lies in between 16 and 18
6. k is found. k is found

B Tree Applications
 databases and file systems

 to store blocks of data (secondary storage media)

 multilevel indexing

Insertion into a B-tree

Inserting an element on a B-tree consists of two events: searching the


appropriate node to insert the element and splitting the node if
required.Insertion operation always takes place in the bottom-up approach.
Let us understand these events below.
Insertion Operation
1. If the tree is empty, allocate a root node and insert the key.

2. Update the allowed number of keys in the node.

3. Search the appropriate node for insertion.

4. If the node is full, follow the steps below.

5. Insert the elements in increasing order.

6. Now, there are elements greater than its limit. So, split at the median.

7. Push the median key upwards and make the left keys as a left child
and the right keys as a right child.

8. If the node is not full, follow the steps below.

9. Insert the node in increasing order.

Insertion Example
Let us understand the insertion operation with the illustrations below.

The elements to be inserted are 8, 9, 10, 11, 15, 16, 17, 18, 20, 23.
Inserting elements into a B-tree
Algorithm for Inserting an Element
Inserting an element on a B-tree consists of two events: 
1.searching the appropriate node to insert the element
2.and splitting the node if required.Insertion operation always takes place in
the bottom-up approach.

Insertion Operation
1. If the tree is empty, allocate a root node and insert the key.

2. Update the allowed number of keys in the node.

3. Search the appropriate node for insertion.

4. If the node is full, follow the steps below.

5. Insert the elements in increasing order.

6. Now, there are elements greater than its limit. So, split at the median.

7. Push the median key upwards and make the left keys as a left child
and the right keys as a right child.

8. If the node is not full, follow the steps below.

9. Insert the node in increasing order.


Insertion Example
Let us understand the insertion operation with the illustrations below.

The elements to be inserted are 8, 9, 10, 11, 15, 16, 17, 18, 20, 23.
Inserting elements into a B-tree

You might also like