0% found this document useful (0 votes)
13 views

Trees

Data structure notes Topic. Trees

Uploaded by

55- Sheldon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Trees

Data structure notes Topic. Trees

Uploaded by

55- Sheldon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

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.

• From the recursive definition, we find that a tree is a collection of n


nodes, one of which is the root, and n - 1 edges.
Trees
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.
Terminologies used in Trees
Terminologies used in Trees
• Subtree − Subtree represents descendants of a node.
• Degree – The degree of a node is the total number of branches of that node.
• Height of 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).
– The height of ni is the longest path from ni to a leaf.
– Thus all leaves are at height 0.
• Height of tree - The height of a tree is the height of its root node.
• Depth - The depth of a node is the number of edges from the node to the tree's root node.
– For any node ni, the depth of ni is the length of the unique path from the root to ni.
– Thus, the root is at depth 0.
• Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next
child node is at level 1, its grandchild is at level 2 and so on.
• Forest - A forest is a set of n ≥ 0 disjoint trees.
Types of Trees
1. Binary Trees

2. Binary search trees

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

• Perfect binary tree

• Skewed binary tree

• Complete Binary tree


Full/Strictly Binary tree
• A binary tree in which every node has either 0 or 2 children is
called as a Full binary tree.
• Full binary tree is also called as 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:

struct node* search(struct node * root,int n)


{
struct node *p = root;
while(p!=NULL)
{
if(n > p->data)
{
return(search(p->right_ptr, n));
}
else if (n < p->data)
{
return(search(p->left_ptr, n));
}
else return (p);
}
return NULL;
}
Insert Operation
• Whenever an element is to be inserted.

• First locate its proper location.

• 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

ii. A node with one child

iii. A node with two children


Delete..
A leaf node
If the node is a leaf, it can be deleted immediately by setting the
corresponding parent pointer to NULL.
Delete..
A node with one child
If the node has one child, the node can be deleted after its parent
adjusts a pointer to bypass the node.
Delete..
A node with two children
• The general strategy is to replace the key of this node with the
smallest key of the right subtree.
• The smallest child in right subtree will either be leaf node or a node
with single right child.
Application of Binary Tree- Expression Tree
• The leaves of an expression tree are operands, such as constants or
variable names and the other nodes contain operators.
• This tree happens to be binary, because all the operations are binary.
• It is also possible for a node to have only one child, as is the case with
the unary minus operator.
• We can evaluate an expression tree, T, by applying the operator at the
root to the values obtained by recursively evaluating the left and right
subtrees.
Huffman Coding
• Huffman Coding is a technique of compressing data to reduce its

size without losing any of the details.

• It was first developed by David Huffman.

• Huffman Coding is generally useful to compress the data in which

there are frequently occurring characters.


How Huffman Coding works?
• Suppose the string below is to be sent over a network.

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

2. Sort the characters in increasing order of the frequency.


3.Make each unique character as a leaf node.
4.Create an empty node z. Assign the minimum frequency to the
left child of z and assign the second minimum frequency to the
right child of z. Set the value of the z as the sum of the above two
minimum frequencies.
5. Remove these two minimum frequencies from Q and add the sum
into the list of frequencies.
6. Insert node z into the tree.
7. Repeat steps 3 to 5 for all the characters.
8. For each non-leaf node, assign 0 to the left edge and 1 to the right edge.
1. Huffman code for character :
• To write Huffman Code for any character, traverse the Huffman Tree from root node to the leaf node of that character.
• Following this rule, the Huffman Code for each character is-

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

internal, leaf or root nodes on a B-tree.

• The leaves are not connected with each other on a B-tree whereas they are connected on a B+ tree.

You might also like