Trees
Trees
Introduction
• A tree is a data structure consisting of nodes organized as a hierarchy.
• In Tree nodes are connected by edges.
• A tree is a nonlinear data structure, compared to arrays, linked lists,
stacks and queues which are linear data structures.
• A tree is a collection of nodes connected by directed (or undirected)
edges.
• The collection can be empty, which is sometimes denoted as A.
• Otherwise, a tree consists of a distinguished node r, called the root,
and zero or more (sub)trees T1, T2, . . . , Tk, each of whose roots
are connected by a edge to r.
• The root of each subtree is said to be a child of r, and r is the parent
of each subtree root.
Trees
• Figure shows a typical tree using the recursive definition.
3. AVL Tree
4. B-Tree
Binary Tree
• A binary tree is a tree in which no node can have more than two
children.
• A binary tree is a tree data structure in which each node has at most
two children, which are referred to as the left child and the right child.
• Figure shows that a binary tree consists of a root and two subtrees,
Tl and Tr, both of which could possibly be empty.
Types of Binary Tree
• Full/Strictly Binary tree
Here,
•First binary tree is not a full binary tree because node C has only 1 child.
Perfect binary tree
A Perfect binary tree is a binary tree that satisfies the following 2
properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
Complete Binary tree
• A complete binary tree is just like a full binary tree, but with two major
differences
• Every level must be completely filled
• All the leaf elements must lean towards the left.
• 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.
Full Binary Tree vs Complete Binary Tree
Skewed Binary Tree
A skewed binary tree is a binary tree that satisfies the following 2
properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child.
• OR
A skewed binary tree is a binary tree of n nodes such that its depth
is (n-1).
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
Binary Tree Representation
• A binary tree data structure is represented using two methods.
Those methods are as follows...
1. Array Representation
2. Linked List Representation
Consider the following binary tree...
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...
• The above example of the binary tree represented using Linked list representation is shown as
follows...
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.
struct node
{
int data;
struct node *left;
struct node *right;
};
Binary Tree Traversal
• Traversal is a process to visit all the nodes of a tree and may print their values.
• Because, all nodes are connected via edges (links) we always start from the
root node.
• That is, we cannot random access a node in tree.
• There are three ways which we use to traverse a tree
i. Pre-order Traversal
ii. In-order Traversal
iii. Post-order Traversal
In order Traversal
Post Order Traversal
Binary Search Tree
Operations on Binary Search Tree
• Search
• Insert
• Delete
Search Operation
• Whenever an element is to be search;
– Start search from root node then if data is less than key value,
search element in left subtree otherwise search element in
right subtree.
• This operation generally requires returning a pointer to the node in
tree T that has key x, or NULL if there is no such node.
search() function:
• Start search from root node then if data is less than key value, search
empty location in left subtree and insert the data.
• Otherwise search empty location in right subtree and insert the data.
struct node *newNode(int item) struct node *insert(struct node *node, int key)
{
{ // Return a new node if the tree is empty
struct node *temp = (struct node if (node == NULL) return newNode(key);
*)malloc(sizeof(struct node));
temp->key = item; // Traverse to the right place and insert the
node
temp->left = temp->right = NULL; if (key < node->key)
return temp; node->left = insert(node->left, key);
else
} node->right = insert(node->right, key);
return node;
}
Example:
Create BST
• BST can be created by using repeated insert operation.
• eg. Create BST for following sequence
72905681
Delete Operation
• Once we have found the node to be deleted, we need to consider
several possibilities.
i. A leaf node
• Each character occupies 8 bits. There are a total of 15 characters in the above string. Thus, a total of 8 * 15 = 120 bits are
required to send this string.
• Using the Huffman Coding technique, we can compress the string to a smaller size.
• Huffman coding first creates a tree using the frequencies of the character and then generates code for each character.
• Huffman coding is done with the help of the following steps.
1.Calculate the frequency of each character in the string.
C= 0
B=100
D=101
A=11
From here, we can observe-
∙ Characters occurring less frequently in the text are assigned the larger code.
∙ Characters occurring more frequently in the text are assigned the smaller code.
2. Average Code Length :
Using formula-01, we have-
Average code length
= ∑ ( frequencyi x code lengthi ) / ∑ ( frequencyi )
= { (1 x 3) + (6x1) + (5 x 2) + (3 x 3) } / (1+6+5+3)
=1.87
3.Length of Huffman encoded message:
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code length per character
= 15x 1.87
= 28.05
Construction of the AVL Tree for the given Sequence 21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
Create a B tree of order 5 by
inserting following element
3 ,14, 7, 1, 8, 5, 11, 17, 13, 6,
23, 12, 20 ,26, 4, 16, 18, 24,
25 ,19
Comparison between a B-tree and a
B+ Tree
• The data pointers are present only at the leaf nodes on a B+ tree whereas the data pointers are present in the
• The leaves are not connected with each other on a B-tree whereas they are connected on a B+ tree.