You are on page 1of 27

DATA STRUCTURES AND ALGORITHMS

Trees in Data Structures.

Binary trees and Tree traversals.


A binary tree is a tree-type non-linear data structure with a maximum of two children for each parent.
Every node in a binary tree has a left and right reference along with the data element. The node at
the top of the hierarchy of a tree is called the root node. The nodes that hold other sub-nodes are the
parent nodes.
A parent node has two child nodes: the left child and right child. Hashing, routing data for network
traffic, data compression, preparing binary heaps, and binary search trees are some of the applications
that use a binary tree.

Terminologies associated with Binary Trees and Types of Binary Trees


• Node: It represents a termination point in a tree.
• Root: A tree’s topmost node.
• Parent: Each node (apart from the root) in a tree that has at least one sub-node of its own is
called a parent node.
• Child: A node that straightway came from a parent node when moving away from the root is
the child node.
• Leaf Node: These are external nodes. They are the nodes that have no child. Same as vertex.
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Internal Node: As the name suggests, these are inner nodes with at least one child.
• Depth of a Tree: The number of edges from the tree’s node to the root is. (A level of a tree:
is the number of parent nodes corresponding to a given a node of the tree. It is basically the
number of ancestors from that node until the root node. So, for the root node (topmost node),
it's level is 0, since it has no parents).

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 1


• Height of a Tree: It is the number of edges from the node to the deepest leaf. The tree height
is also considered the root height.

• The degree of a tree: is the maximum degree of a node in the tree. The number of edges
along the shortest path between two nodes. The level of a node is the number of edges along
the unique path between it and the root node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
• A descendant node of a node is any node in the path from that node to the leaf node
(including the leaf node). The immediate descendant of a node is the “child” node.

• An ancestor node of a node is any node in the path from that node to the root node
(including the root node). The immediate ancestor of a node is the “parent” node. If node A
is an ancestor of node B then node B is a descendant of node A.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 2


Binary Tree Components

1. Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
Consider the above example of a binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional array with
a maximum size of 2n + 1.

2. Linked List Representation of Binary Tree


We use a double linked list to represent a binary tree. In a double linked list, every node consists of
three fields. First field for storing left child address, second for storing actual data and third for
storing right child address.
In this linked list representation, a node has the following structure...

There are three binary tree components. Every binary tree node has these three components
associated with it. It becomes an essential concept for programmers to understand these
three binary tree components:
1. Data element
2. Pointer to left subtree
3. Pointer to right subtree

These three binary tree components represent a node. The data resides in the middle. The left
pointer points to the child node, forming the left sub-tree. The right pointer points to the child node
at its right, creating the right subtree.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 3


Types of Binary Tree

There are five types of Binary tree:

o Full/ proper/ strict Binary tree / 2-Tree


o Perfect Binary tree
o Complete Binary tree
o Degenerate Binary tree / Pathological tree
o Balanced Binary tree

1. Full/ proper/ strict Binary tree / 2-Tree.

The full binary tree is also known as a strict binary tree. The tree can only be considered as the full
binary tree if each node must contain either 0 or 2 children. A binary tree in which every node has
either two or zero number of children is called Strictly Binary Tree, Full Binary Tree, Proper Binary

Tree or 2-Tree.

2. 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. All leaves have the same depth or same level.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 4


3. Complete binary tree.

It is a binary tree in which every level, except possibly the last, is completely filled, and all nodes
are as far left as possible. In a complete binary tree, the nodes should be added from the left. A
Binary tree is said to be complete Binary Tree if all levels are completely filled except possibly the
last level and the last level has all keys as left as possible.

A Perfect Binary Tree whose rightmost leaves (perhaps all) on the last level have been removed is called Complete
Binary Tree.

Perfect vs Complete Binary Tree: Some authors also refer Perfect Binary Tree as Complete Binary Tree.
And they call Complete Binary Tree as Almost Complete Binary Tree or Nearly Complete Binary Tree.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 5


4. Degenerate or Pathological Tree.

The degenerate binary tree is a tree in which all the internal nodes have only one child. It is a tree is
where each parent node has only one child node. It behaves like a linked list. A degenerate or
Pathological Tree is a Tree where every parent node has only one child either left or right.

Such trees are performance-wise same as linked list. In fact, comparatively it provides slow
performance than LinkedList as while traversing, you need to first check whether tree has left child or
right child and then move to next node.

Degenerate binary trees can also be classified into two types

a. Left-skewed – A degenerate binary tree in which all the nodes lean towards the left side of the
tree. The following diagram shows a left-skewed degenerate binary tree.

b. Right-skewed – A degenerate binary tree in which all the nodes lean towards the right side of the
tree. The following diagram shows a right-skewed degenerate binary tree.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 6


5. Balanced Binary Tree.

Binary tree is called Balanced Binary Tree, if difference of left and right subtree height is maximum
one for all the nodes.

If any one node violates this rule i.e. Difference of left and right subtree height is more than one, then that tree is not
balanced.

Below diagram shows the example of Balanced and Non-balanced Binary Tree. We have also shown
pair of left and right subtree height of each node i.e. (Left Subtree Height, Right Subtree Height)

The balanced binary tree is a tree in which both the left and right trees differ by atmost 1. For
example, AVL and Red-Black trees are balanced binary tree.

Properties of Binary Tree

o At each level of i, the maximum number of nodes is 2i.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 7


o The height of the tree is defined as the longest path from the root node to the leaf node. The
tree which is shown above has a height equal to 3. Therefore, the maximum number of nodes
at height 3 is equal to (1+2+4+8) = 15. In general, the maximum number of nodes possible
at height h is (20 + 21 + 22+….2h) = 2h+1 -1.
o The minimum number of nodes possible at height h is equal to h+1.
o If the number of nodes is minimum, then the height of the tree would be maximum.
Conversely, if the number of nodes is maximum, then the height of the tree would be
minimum.

If there are 'n' number of nodes in the binary tree.

The minimum height can be computed as:

As we know that,

n = 2h+1 -1

n+1 = 2h+1

Taking log on both the sides,

log2(n+1) = log2(2h+1)

log2(n+1) = h+1

h = log2(n+1) - 1

The maximum height can be computed as:

As we know that,

n = h+1

h= n-1

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 8


Tree traversal (Inorder, Preorder an Postorder)
Tree traversal means traversing or visiting each node of a tree. There is a single way to traverse the
linear data structure such as linked list, queue, and stack. Whereas, there are multiple ways to traverse
a tree that are listed as follows -

o Preorder traversal
o Inorder traversal
o Postorder traversal
o Levelorder traversal

Preorder traversal
This technique follows the 'root left right' policy. It means that, first root node is visited after that the
left subtree is traversed recursively, and finally, right subtree is recursively traversed. As the root node
is traversed before (or pre) the left and right subtree, it is called preorder traversal.

So, in a preorder traversal, each node is visited before both of its subtrees.

The applications of preorder traversal include -

o It is used to create a copy of the tree.


o It can also be used to get the prefix expression of an expression tree.

Algorithm

Until all nodes of the tree are not visited

1. Step 1 - Visit the root node


2. Step 2 - Traverse the left subtree recursively.
3. Step 3 - Traverse the right subtree recursively.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 9


Example

Now, start applying the preorder traversal on the above tree. First, we traverse the root node A; after
that, move to its left subtree B, which will also be traversed in preorder.

So, for left subtree B, first, the root node B is traversed itself; after that, its left subtree D is traversed.
Since node D does not have any children, move to right subtree E. As node E also does not have any
children, the traversal of the left subtree of root node A is completed.

Now, move towards the right subtree of root node A that is C. So, for right subtree C, first the root
node C has traversed itself; after that, its left subtree F is traversed. Since node F does not have any
children, move to the right subtree G. As node G also does not have any children, traversal of the
right subtree of root node A is completed.

Therefore, all the nodes of the tree are traversed. So, the output of the preorder traversal of the above
tree is -

A→B→D→E→C→F→G

Postorder traversal

This technique follows the 'left-right root' policy. It means that the first left subtree of the root node
is traversed, after that recursively traverses the right subtree, and finally, the root node is traversed. As
the root node is traversed after (or post) the left and right subtree, it is called postorder traversal.

So, in a postorder traversal, each node is visited after both of its subtrees.

The applications of postorder traversal include -

o It is used to delete the tree.


o It can also be used to get the postfix expression of an expression tree

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 10


Algorithm

Until all nodes of the tree are not visited

1. Step 1 - Traverse the left subtree recursively.


2. Step 2 - Traverse the right subtree recursively.
3. Step 3 - Visit the root node.

Example

Now, start applying the postorder traversal on the above tree. First, we traverse the left subtree B that
will be traversed in postorder. After that, we will traverse the right subtree C in postorder. And finally,
the root node of the above tree, i.e., A, is traversed.

So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any children,
traverse the right subtree E. As node E also does not have any children, move to the root
node B. After traversing node B, the traversal of the left subtree of root node A is completed.

Now, move towards the right subtree of root node A that is C. So, for right subtree C, first its left
subtree F is traversed. Since node F does not have any children, traverse the right subtree G. As node
G also does not have any children, therefore, finally, the root node of the right subtree, i.e., C, is
traversed. The traversal of the right subtree of root node A is completed.

At last, traverse the root node of a given tree, i.e., A. After traversing the root node, the postorder
traversal of the given tree is completed.

Therefore, all the nodes of the tree are traversed. So, the output of the postorder traversal of the above
tree is -

D→E→B→F→G→C→A

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 11


Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is visited after that
root node is traversed, and finally, the right subtree is traversed. As the root node is traversed between
the left and right subtree, it is named inorder traversal.

So, in the inorder traversal, each node is visited in between of its subtrees.

The applications of Inorder traversal includes -

o It is used to get the BST nodes in increasing order.


o It can also be used to get the prefix expression of an expression tree.

Algorithm

Until all nodes of the tree are not visited

1. Step 1 - Traverse the left subtree recursively.


2. Step 2 - Visit the root node.
3. Step 3 - Traverse the right subtree recursively.

Example

Now, let's see the example of the Inorder traversal technique.

Now, start applying the inorder traversal on the above tree. First, we traverse the left subtree B that
will be traversed in inorder. After that, we will traverse the root node A. And finally, the right
subtree C is traversed in inorder.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 12


So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any children,
so after traversing it, node B will be traversed, and at last, right subtree of node B, that is E, is
traversed. Node E also does not have any children; therefore, the traversal of the left subtree of root
node A is completed.

After that, traverse the root node of a given tree, i.e., A.

At last, move towards the right subtree of root node A that is C. So, for right subtree C; first, its left
subtree F is traversed. Since node F does not have any children, node C will be traversed, and at last,
a right subtree of node C, that is, G, is traversed. Node G also does not have any children; therefore,
the traversal of the right subtree of root node A is completed.

As all the nodes of the tree are traversed, the inorder traversal of the given tree is completed. The
output of the inorder traversal of the above tree is -

D→B→E→A→F→C→G

Levelorder traversal

Level Order Traversal is the algorithm to process all nodes of a tree by traversing through depth,
first the root, then the child of the root. Level order traversal can be done by using a queue and
traversing nodes by depth.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 13


Binary Search Tree
Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.
In a binary search tree (BST), each node contains-
• Only smaller values in its left sub tree
• Only larger values in its right sub tree

Example-

Binary Search Tree Construction-


Let us understand the construction of a binary search tree using the following example-
Example-
Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
When elements are given in a sequence,
• Always consider the first element as the root node.
• Consider the given elements and insert them in the BST one by one.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 14


The binary search tree will be constructed as explained below-
Insert 50-

Insert 70-

• As 70 > 50, so insert 70 to the right of 50.

Insert 60-

• As 60 > 50, so insert 60 to the right of 50.


• As 60 < 70, so insert 60 to the left of 70.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 15


Insert 20-

• As 20 < 50, so insert 20 to the left of 50.

Insert 90-

• As 90 > 50, so insert 90 to the right of 50.


• As 90 > 70, so insert 90 to the right of 70.

Insert 10-

• As 10 < 50, so insert 10 to the left of 50.


• As 10 < 20, so insert 10 to the left of 20.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 16


Insert 40-

• As 40 < 50, so insert 40 to the left of 50.


• As 40 > 20, so insert 40 to the right of 20.

Insert 100-

• As 100 > 50, so insert 100 to the right of 50.


• As 100 > 70, so insert 100 to the right of 70.
• As 100 > 90, so insert 100 to the right of 90.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 17


Exercise: Construct Binary Search Tree from the Data given.
1. 20,23,13,9,14,19,21,27,24
2. 25,10,12,15,39,64,53

BST Basic Operations


The basic operations that can be performed on a binary search tree data structure, are the following

• Insert − Inserts an element in a tree/create a tree.
• Search − Searches an element in a tree.
• Preorder Traversal − Traverses a tree in a pre-order manner.
• Inorder Traversal − Traverses a tree in an in-order manner.
• Postorder Traversal − Traverses a tree in a post-order manner.
• Levelorder Traversal − Traverses a tree in a level-order manner.

Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate
its proper location. Start searching from the root node, then if the data is less than the key value,
search for the empty location in the left subtree and insert the data. Otherwise, search for the empty
location in the right subtree and insert the data.
Algorithm

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 18


If root is NULL
then create root node
return

If root exists then


compare the data with node.data

while until insertion position is located

If data is greater than node.data


goto right subtree
else
goto left subtree

endwhile

insert data

end If

Search Operation
Whenever an element is to be searched, start searching from the root node, then if the data is less than
the key value, search for the element in the left subtree. Otherwise, search for the element in the right
subtree. Follow the same algorithm for each node.
Algorithm
If root.data is equal to search.data
return root
else
while data not found

If data is greater than node.data


goto right subtree
else
goto left subtree

If data found
return node
endwhile

return data not found

end if

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 19


A C program to traverse a binary Tree.

// Tree traversal in C

#include <stdio.h>
#include <stdlib.h>

struct node
{
int item;
struct node* left;
struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root)
{
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

// Preorder traversal
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Postorder traversal
void postorderTraversal(struct node* root)
{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}

// Create a new Node


struct node* createNode(value)

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 20


{
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

// Insert on the left of the node


struct node* insertLeft(struct node* root, int value)
{
root->left = createNode(value);
return root->left;
}

// Insert on the right of the node


struct node* insertRight(struct node* root, int value)
{
root->right = createNode(value);
return root->right;
}

int main()
{
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);
}

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 21


OUTPUT:

Application of Binary Trees:


• Huffman coding tree is an application of binary trees that are used in data compression
algorithms.
• In compilers, Expression Trees are used which are applications of binary trees.
• Priority Queue is another application of binary tree that is used to search maximum or
minimum in O(log N) time complexity.
• Represent hierarchical data.
• used in editing software like Microsoft Excel and spreadsheets.
• useful for indexing segmented at the database is useful in storing cache in the system,
• syntax trees are used for most famous compilers for programming like GCC, and
AOCL to perform arithmetic operations.
• for implementing priority queues.
• used to find elements in less time (binary search tree)
• used to enable fast memory allocation in computers.
• to perform encoding and decoding operations.
• Binary Search Tree is used to search elements efficiently and used as a collision handling
technique in Hash Map implementations.
• Binary Tree is used to implement indexing of Segmented Database.
• Binary Space Partition Trees are used in Computer Graphics, Back face Culling,
Collision detection, Ray Tracing and algorithms in rendering game graphics.

Real-time applications of Binary Trees:


• DOM (Document Object Model) in HTML.
• File explorer.
• Used as the basic data structure in Microsoft Excel and spreadsheets.
• Editor tool: Microsoft Excel and spreadsheets.
• Evaluate an expression
• Routing Algorithms

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 22


Advantages of Binary Tree:
• The searching operation in a binary tree is very fast.
• The representation of a binary tree is simple and easy to understand.
• Traversing from a parent node to its child node and vice-versa is efficiently done.
• simple to implement
• easy to understand.
• a hierarchical structure.
• reflect structural relationships that are present in the data set
• easy to insert data than in another data store.
• easy to store data in memory management.
• user can many nodes
• executions are fast.
• store an arbitrary number of data values.
Disadvantages of Binary Tree:
• In binary tree traversals, there are many pointers that are null and hence useless.
• The access operation in a Binary Search Tree (BST) is slower than in an array.
• A basic option is dependent on the height of the tree.
• Deletion node not easy.
• A basic option is based on the height of tree.

AVL trees.
An AVL tree is another balanced binary search tree. Named after their inventors, Adelson-Velskii
and Landis, they were the first dynamically balanced trees to be proposed.
An AVL tree is a binary search tree which has the following properties:

1. The sub-trees of every node differ in height by at most one.


2. Every sub-tree is an 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.

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.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 23


An AVL tree is given in the following figure. We can see that, balance factor associated with each
node is in between -1 and +1. therefore, it is an example of AVL tree.

B-trees.
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

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 24


A B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions
in logarithmic amortized time. Unlike self-balancing binary search trees, it is optimized for systems
that read and write large blocks of data. It is most commonly used in database and file systems.
Important properties of a B-tree:
• B-tree nodes have many more than two children.
• A B-tree node may contain more than just a single element.

Why do you need a B-tree data structure.

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, 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.

Searching an element in a B-tree

Searching for an element in a B-tree is the generalized form of searching an element in a Binary
Search Tree. The following steps are followed.
1. Starting from the root node, compare k with the first key of the node.
If k = the first key of the node, return the node and the index.
2. If k.leaf = true, return NULL (i.e. not found).
3. If k < the first key of the root node, search the left child of this key recursively.
4. If there is more than one key in the current node and k > the first key, compare k with the
next key in the node.
If k < next key, search the left child of this key (ie. k lies in between the first and the second
keys).
Else, search the right child of the key.
5. Repeat steps 1 to 4 until the leaf is reached.

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 25


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

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 26


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

GEORGE WAINAINA 0718313173/0731919231 georgewainaina58@gmail.com Page 27

You might also like