trees - itviewson.files.wordpress.com · trees introduction: tree a tree is a non-linear data...
TRANSCRIPT
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 1
Trees
Introduction:
Tree
A tree is a non-linear data structure in which items are arranged in a sorted
sequence. It is used to represent hierarchical relationship existing amongst several
data items. Formally it can be defined as it is a finite set of one or more data items
(nodes) such that:
1. There is a special data item called the root of the tree.
2. And its remaining data items are partitioned into number of mutually
exclusive (i.e. disjoint) subsets, each of which is itself a tree. And they are
called subtrees.
3. Let n is a node and T1, T2, ………………., Tk are trees with roots n1, n2, …,
nk respectively, we can construct a new tree by making n parent of nodes n1,
n2, ………………nk.
Here n1, n2,……………nk called the children of n.
e.g.
A Root
B C D
Figure: Tree representation
Here, A is root node and B, C, D are children of A.
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 2
Tree Terminology
A Root Level 0
B C D Level 1
E F G H I Level 2
J K L M Level 3
Figure. A Tree.
Root: It is specially designed data item in tree. It is the first in the hierarchical
arrangement of data items.
Node: Each data item in a tree is called a node. It is the basic structure in a tree. It
specifies the data information and links (branches) to other data items. There are
13 nodes in the above tree.
Degree of a node: It is the number of subtrees of a node in a given tree. In the
above tree:
The degree of node A is 3
The degree of node C is 1
The degree of node B is 2
The degree of node H is 0
Degree of tree: It is the maximum degree of nodes in a given tree. In the above
tree the node a has degree 3 and another node I is also having its degree 3. In all
this value is the maximum. So, the degree of the above tree is 3.
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 3
Leaf node: A node with degree zero is called a leaf node. In above tree, there are 7
leaf nodes. They are E, J, G, H, K, L and M.
Siblings: The children nodes of a given parent node are called siblings. They are
also called brothers. In the above tree,
E and F are siblings of parent node B.
Level: The entire tree structure is leveled in such a way that the root node is
always at level 0. Then, its immediate children are at level1 and their immediate
children are at level 2 and so on upto the leaf node.
In general, if a node is at level n, then its children will be at level n+1.
Edge: It is a connecting line of two nodes.
Path: It is a sequence of consecutive edges from the source node to the destination
node.
Depth: It is the maximum level of any node in a given tree. In the above tree, the
root node A has the maximum level. That is the number of levels one can descend
the tree from its root to the terminal nodes. The term height is also used to denote
the depth.
Ancestor: If there is path from nod A to node B then A is ancestor of B.
Descendent: If A is ancestor of B, then B is descendent of A.
Null Tree: A tree with no any nodes is termed as null tree. Mathematically, a null
tree is represented as ε and in data structure null tree is represented by a NULL
pointer.
Forest: It is a set of disjoint trees. In any tree if we remove its root node then it
becomes a forest.
Binary Trees:
Def
n: A binary tree is a finite collection of nodes which is either empty or it
consists of a node called root together with two disjoint binary trees called left-sub
tree and right-sub tree of root.
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 4
The left-sub tree, right sub tree may also be empty.
The general representation of binary tree is given as follows:
Left-sub tree Right-sub tree
Figure representation of binary tree
Example
Figure a binary tree with 7 nodes rooted at node A.
root
A
B C
F
G
E D
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 5
Binary tree with 3 nodes
or or or or
Note: In binary tree, each node can have at most two Childs, hence called binary.
Types of Binary Tree
1. Strictly binary tree:
If every non-leaf node of binary tree has empty left and right sub tree, then it is
strictly binary tree.
A
B
C
A
C
B
A
C
B
A
C
B
A
C B
A
B C
E D G F
D
C B
A
A
E F
E D
C B
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 6
Strictly binary tree Strictly binary tree Non-Strictly binary tree
2. Complete Binary Tree:
A binary tree with n nodes and of depth d is a strictly binary tree all of whose
terminal nodes are at level d. The following figure depicts a complete binary tree
of depth 3.
Figure A complete binary tree with depth 3.
A complete binary tree of depth d is the binary tree of depth d that contains exactly
2l nodes at each level l between 0 and d. The total number of nodes in a complete
binary tree of depth d, tn, equals the sum of the numbers of nodes at each level
between 0 and d. Thus
d
tn=20+2
1+2
2+……………………………+2
d= ∑ 2
j
j=0
This sum equals 2d+1
-1(By induction).
A
B C
D E F G
O N K J I H M L
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 7
3. Almost Complete tree:
A binary tree of depth d is an almost complete binary tree if:
1. Any node nd at level less than d-1 has two sons.
2. For any node nd in the tree with a right descendent at level d, nd must have a
left son and every left descendent of nd is either a leaf at level d or has two
sons.
Figure A almost complete binary tree
Applications: 1. Trees are used to implement the file system of several operating systems.
2. Used to implement the arithmetic expressions.
3. Used to support searching operations in O(logn) average time using binary
search trees.
4. The binary trees are used when there are two way decisions must be made at
each point in a process.
Binary tree Representation;
A
B C
D E
F G
H I
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 8
1. Array Representation of Binary Trees: Consider a complete binary tree,
and number vertices from left to right beginning from root to each level.
1
2 3
4 5 6 7
Total number of nodes=7
This binary tree can be put into contiguous array by sorting each node in the
position shown by its label.
Notice that, the left and right children of node at index k are in position 2k and
2k+1. Assume that k starts at 1,
Then we can represent the above tree as
1 2 3 4 5 6 7
A B C D E F G
Data structure
struct node
{
int info;
int left;
int right;
int father;
};
struct node node[MAXNODES]
A
B
D
C
E F G
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 9
Info left right father
1
2
3
4
5
6
7
Linked List Representation of Binary Trees (Dynamic):
The basic component to be represented in binary tree is a node. The node consists
of three fields such as:
1. Data
2. Left child
3. Right child
The structure of node is given as
Lchild Data Rchild
Figure A node structure of a binary tree
Data structures
struct node
{
char data;
struct node *lchild;
struct node *rchild;
} ;
struct node node;
Consider the following binary tree
A 2 3 -1
B 4 5 1
C 6 7 1
D -1 -1 2
E -1 -1 2
F -1 -1 3
G -1 -1 3
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 10
Then its linked list representation is given as below:
A
Figure A binary tree in linked list representation.
Operations on Binary Trees:
The basic operations to be performed on a binary tree are listed as below:
S.N. Operation Description
1. create It creates an empty binary tree.
2. makeBT It creates a new binary tree having a single node with data field
set to some value.
3. emptyBT It returns true if the binary tree is empty. Otherwise it returns
false.
4. Lchild It returns a pointer to the left child of the node. If the node has
B
D G E
C
F
A
B
D
C
E F G
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 11
no left child it returns the null pointer.
5. Rchild It returns a pointer to the right child of the node. If the node has
no right child it returns the null pointer.
6. father It returns a pointer to the father of the node. Otherwise returns
the null pointer.
7. Brother
(sibling)
It returns a pointer to the brother of the node. Otherwise returns
the null pointer.
8. Data It returns the contents of the node.
Tree Traversal: Traverse is an operation in tree i.e. to pass through the tree e numerating each
of its nodes once i.e. visiting each node at once.
In binary tree, there are three common ways of binary tree traversal. They are:
1. Preorder traversal
2. Inorder traversal
3. Postorder traversal
Inorder traversal: The inorder traversal of a non-empty binary tree is
defined as follows:
i. Traverse the left subtree in inorder (L).
ii. Visit the root node (V).
iii. Traverse the right subtree in inorder (R).
The algorithm in C for an inorder traversal is given as below:
void inorder (node *p)
{
if (tree!=NULL)
{
inorder (p->left);
printf (“%d\n”,p->data);
inorder (p->right);
}
}
Preorder traversal: The preorder traversal of non-empty binary tree is
defined as;
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 12
i. Visit the root node (V).
ii. Traverse the left subtree in preorder (L).
iii. Traverse the right subtree in preorder (R).
The algorithm in C for the preorder traversal is given as below:
void preorder (node *p)
{
if (tree!=NULL)
{
printf (“%d\n”,p->data);
preorder (p->left);
preorder (p->right);
}
}
Postorder traversal: The postorder traversal of non-empty binary tree is
defined as;
i. Traverse the left subtree in postorder (L).
ii. Traverse the right subtree in postorder(R).
iii. Visit the root node (V).
The algorithm in C for the postorder traversal is given as below:
void postorder (node *p)
{
if (tree!=NULL)
{
postorder (p->left);
postorder (p->right);
printf (“%d\n”,p->data);
}
}
Examples
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 13
Figure 1 Figure 2
Traversal :
Figure 1: Figure 2:
Preorder A B D C E G F H I A B C E F D G H I J
Inorder D B A E G C H F I E C F B D G I H J A
Postorder D B G E H I F C A E F C I J H G D B A
Binary Search Tree
A binary search tree is a binary tree which is either empty or satisfies the following
rules;
1. The value of the key in the left child or left subtree is less than the value of
the root.
2. The value of the key in the right child or right subtree is more than equal to
the value of the root.
A
B
D
C
E F
H
B
D
A
C
G
G I
E
J I
F
H
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 14
Examples:
Binary search tree Not binary search tree
Since , the keys in each node of left sub-tree are smaller than root key and keys in
the right sub-tree are greater than the key in root, the binary search tree can be used
for efficient search and sort technique.
Insertion in binary search tree:
Assume the data structure for binary search tree is:
typedef struct node
{
int key;
struct node *left, *right;
}treenode;
C function for insertion in binary search tree
treenode *insertnode (int x,treenode *T )
{
if (T==NULL)
{
T=getnode();
if (T==NULL)
printf (“ Out of range”);
else
15
13
9
28
17 30
15
18 25 25
22 21 35
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 15
{
T->key=x;
T->left=T->right=NULL;
}
}
else if (T->key>x)
T->left=insertnode (x, T->left);
else if (T->key<x)
T->right=insertnode (x,T->right);
else
printf (“Duplicate node ::program exited”);
return T;
}
C function for searching in binary search tree (Recursive)
treenode *Treesearch (treenode *p, int data)
{
if (p==NULL)
printf (“The node does no exits”);
{
if (data<p->key)
p= Treesearch (p->left,data);
else if (data> p->key)
p=Treesearch (p->right, data);
return p;
}
}
Non-recursive search:
In above search function there is recursion. In large size tree it can be stack
overhead due to recursion during search. The non-recursive version of search
function can be written as:
treenode *Treesearch (treenode *p, int data)
{
while (p && data<p->key)
{
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 16
if (target<p->key)
p=p->left;
else
p=p->right;
}
}
C function for deletion in binary search tree
treenode *delnum (int x, treenode *r)
{
treenode *Q;
if ( r->right!=NULL)
delnum (x,r->right);
else
Q->num=r->num;
Q=r;
r=r->left;
}
treenode *deletenode (int x,treenode *T )
{
treenode *r, *Q;
if (T==NULL)
{
printf (“T is empty”);
exit (0);
}
if (x<T->key)
deletenode (x, T->left);
else if (x>T->key)
deletenode (x, T->right);
q=T;
if ((Q->right==NULL)&&(Q->left==NULL))
Q=NULL;
else if ( Q->right==NULL)
T=Q->left;
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 17
else if (Q->left==NULL)
T=Q->right;
else
delnum (x,Q->left)
free (Q);
}
Extended Binary Tree: An extended binary tree is a transformation of any binary tree into a complete
binary tree. This transformation consists of replacing every null sub-tree of the
original tree with special nodes. The nodes from the original tree are then internal
nodes, which has two children, while the special nodes are external nodes which
have no children.
Notation: The internal nodes are represented by a circles.
The external nodes are represented by a box
Example
Every internal nodes in extended binary tree has exactly two children and every
enter node leaf.
Huffman Algorithm
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 18
Huffman algorithm is method for creating an extended binary tree with a minimum
weighted path length from a set of given weights. Initially construct a forest of
singleton trees. If there are at least two trees choose the two trees with the learnt
weight associated with their root and replace them with a new tree, constructed by
creating a root node whose weight is the sum of the weights of the roots of the two
trees removed and setting the two trees just removed as this new nodes child. This
process is repeated until the forests consist of one tree.
Algorithm:
Huffman (w,n)
Input: A list W of n weights
Output: an extended binary tree T with weights taken from W that gives the
minimum weighted path length.
Procedure: Create list F from Singleton trees formed from elements of W.
While (F has more than an element) Do
Find in F that have minimum values associated with their roots.
Construct new tree T by creating a new node and setting T1&T2 as its children.
Let the sum of the values associated with the roots of T1&T2 be associated with
the root of T. Add T to F. Huffman tree stored in F.
Example
Let us take an example for the set of weights (1,2,3,3,4). In these intermediate
steps we will display the temporary weights assigned by the algorithm in the
circled nodes. Initially our forest is
During the first step, the two tree with the weights 1 & 2 are ,merged, to create a
new tree with a root of weight 3.
1
3
3 3 4
3 3 4
2
1 2
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 19
3 4 6
3 3
1 2
6
3
1 2
3 3 4
7
13
33
6 7
3
1 2
3
3 4
Data Structure & Algorithms Chapter Trees
By Surya Bam Page 20
AVL Tree:
An AVL tree , after the Russian mathematician G.M. Adelson- Velskii and E.M.
Lendis, is a binary search tree in which the height of left and right sub-trees of the
root differ by at most 1 and in which the left and right sub-trees are again AVL
trees. It is also called height balanced binary search tree.
With each node of AVL tree, a balance factor is associated i.e. the left sub-
tree has height greater than, equal to, or less than the height of the right sub-tree.
But the height difference between left sub-trees is either 1 or 0 or -1. So the
balance factor of each node of AVL tree must be 1, 0 or -1.
Example: 0
+1
-1
0 0
0 +1
0 0 0
10
5
20
4 7
11
25
13
9 6