Module 4 DS
Module 4 DS
Module 4
Manjunatha B N
Assistant Professor
Department
1 of Computer Science, RLJIT
Outline
Terminology
Binary Trees
Properties of Binary trees
Array and linked Representation of Binary Trees
Binary Tree Traversals - Inorder, postorder, preorder
Additional Binary tree operations
Threaded binary trees
Binary Search Trees :Definition, Insertion, Deletion,
Traversal, Searching
Application of Trees-Evaluation of Expression
Programming Examples
Example: Consider the following tree. Let us identify the root node
and various subtrees:
The tree has 8 nodes: A, B, C, D, E, F,
G and H.
The node A is the root of the tree.
We normally draw the trees with root
at the top.
The node B, C and D are the children
of node A and hence there are 3
subtrees identified by B, C and D.
Department of Computer Science, RLJIT
The node A is the parent 5
of B, C and D.
Terminology
Root
• In a tree data structure, the first node is called as Root Node.
• Every tree must have root node.
• We can say that root node is the origin of tree data structure.
• In any tree, there must be only one root node.
• We never have multiple root nodes in a tree.
• Ex: „A‟ in the below tree.
– List Representation
Example:
Properties of Binary Tree:-
(i) The maximum number of nodes on level i of a binary tree = 2i for i>=0.
(ii) The maximum number of nodes in a binary tree of depth k = 2k – 1.
Proof: Consider the following binary tree and observe the following factors:
Number of nodes at level 0 = 1 = 20
Number of nodes at level 1 = 2 = 21
Number of nodes at level 2 = 4 = 22
.
.
.
Number of nodes at level i = 2i
Therefore, total number of nodes in the full binary tree of level
i = 20 +21 + 22 + 23 + ….. + 2i
The above series is a geometric progression whose sum is given by:
S = a (rn -1) / (r - 1)
Where a=1, n=i+1, and r=2
Therefore, Total number of nodes nt = a (rn – 1) / (r – 1)
= 1(2i+1 -1) /(2 – 1)
= 2i+1 -1
The depth of the tree k is maximum level + 1, so k = i+1
Substituting this value in above equation, we get nt = 2k -1
Lemma 2: [Relation between number of leaf nodes and degree-2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf nodes
and n2 the number of nodes of degree 2, then n0 = n2 + 1.
Proof: Let n1 be the number of nodes of degree one and n the total number
of nodes.
Since all nodes in T are at most of degree two, we have
n = n 0 + n 1+ n 2 (1)
Count the number of branches in a binary tree. If B is the number of
branches, then
n =B + 1
All branches stem from a node of degree one or two. Thus,
B =n 1+ 2n2
Hence, we obtain
n = B + 1= n 1+ 2n2 + 1 (2)
Subtracting Eq. (2) from Eq. (1) and rearranging terms, we get
n0 = n2 +1
Types of Binary Trees
• Right Skewed BT
• Left Skewed BT
Left Skewed BT
Here the tree grows only towards left.
Extended Binary Tree
• If i is the position of the left child i+1 gives the position of right
child.
Advantages of array representation
» Faster access
Disadvantages
systematic order.
• During traversal, one may print the info field of each node visited.
1. In - Order Traversal
I-D-J-B-F-A-G-K-C-H
Pre - Order Traversal ( root - leftChild - rightChild )
• In this traversal:
» The root node is visited first,
A-B-D-I-J-F-C-G-K-H
Post - Order Traversal ( leftChild - rightChild - root )
• If the preorder traversal is given, then the first node is the root
node.
• If the postorder traversal is given then the last node is the root
node.
• Once the root node is identified, all the nodes in the left sub-trees
and right sub-trees of the root node can be identified using
inorder.
• From the preorder sequence C E H I F, the root of the left sub tree
is: C
A A
B, C D, E, F, G, H, I B D
A
C E F
B D, E, F, G, H, I G I
C H
CHAPTER 5 67
Additional Binary Tree Operations:
1. More than 50% of linked fields have NULL (\0) values, hence
more memory space is wasted.
• This NULL pointer does not play any role except indicating
there is no link (no child).
• A. J. Perlis and C. Thornton have proposed new binary tree
called "Threaded Binary Tree", which make use of NULL
pointer to improve its traversal processes.
• All right child pointers that are NULL (in Linked list
representation) points to its in-order successor.
H-D-I-B-E-A-F-J-C-G
When we represent above binary tree using linked list representation
Binary Search Tree
• In a binary tree, every node can have maximum of two
children but there is no order of nodes based on their values.
• Every Binary Search Tree is a binary tree but all the Binary
Trees need not to be binary search trees.
» Search
» Insertion
» Deletion
» Traversal
Search Operation in BST
In a binary search tree, the search operation is performed with O(log n)
time complexity.
• The search operation is performed as follows...
Step 3: If both are matching, then display "Given node found!!!" and
terminate the function
Step 4: If both are not matching, then check whether search element is
smaller or larger than that root node value.
Step 5: If search element is smaller, then continue the search
process in left subtree
Step 6: If search element is larger, then continue the search
process in right subtree.
Step 7: Repeat the same until we find exact element or we
completed with a leaf node
Step 8 : If we reach the node with search value, then display
“Element is found” and terminate the function.
Step 9: If we reach a leaf node and it is also not matching
the display “Element not found” and terminate the function.
Function to search in BST
NODE search(NODE root, int key)
{
NODE cur;
if(root == NULL)
return NULL;
cur = root;
while(cur != NULL)
{
if(key == cur->info)
return cur;
if(key<cur->info)
cur = cur->llink;
else
cur = cur->rlink;
}
return NULL;
}
Insertion Operation in BST
• In a binary search tree, the insertion operation is performed with
Step 1: Create a newNode with given value and set its left and right to
NULL.
Step 5: If newNode is smaller than the node, then move to its left child.
If newNode is larger than or equal to the node, then move to its right
child.
Step 6: Repeat the above step until we reach to a leaf node (i.e, reach to
NULL).
Step 7: After reaching a leaf node, then insert the newNode as left child
if newNode is smaller that leaf else insert it as right child.
/* This function is for creating a binary search
tree */
NODE insert(NODE root) while(cur != NULL)
{ {
NODE temp, cur, prev; prev = cur;
int item; if (item< cur->info)
printf("\nEnter The Element "); cur = cur->llink;
scanf("%d", &item); else
temp = (NODE) malloc(sizeof(struct cur = cur->rlink;
BST));
}
temp->llink = NULL;
temp->rlink = NULL;
if (item <prev->info)
temp->info = item;
prev->llink = temp;
else
if (root == NULL)
prev->rlink = temp;
return temp;
return root;
prev = NULL;
}
cur = root;
Deletion Operation in BST
Step 2: Delete the node using free function (If it is a leaf) and
terminate the function.
Case 2: Deleting a node with one child
We use the following steps to delete a node with one child from BST...
Step 2: If it has only one child, then create a link between its parent
Step 3:Delete the node using free function and terminate the function
Case 3: Deleting a node with two children
We use the following steps to delete a node with two children from
BST...
Step 2: If it has two children, then find the largest node in its left
subtree (OR) the smallest node in its right subtree.
Step 3: Swap both deleting node and node which found in above
step.
Step 4: Then, check whether deleting node came to case 1 or case 2
Step 7: Repeat the same process until node is deleted from the tree.
Example
Construct a Binary Search Tree by inserting the
following sequence of numbers...
10,12,5,4,20,8,7,15 and 13
Other operation on BST:
1. To find Maximum value in a BST.
NODE max (NODE root)
{
NODE cur;
if(root == NULL)
return root;
cur = root;
while(cur->rlink != NULL)
cur = cur->rlink;
return cur;
}
Other operation on BST:
2. To find Minimum value in a BST.
NODE min (NODE root)
{
NODE cur;
if(root == NULL)
return root;
cur = root;
while(cur->llink != NULL)
cur = cur->llink;
return cur;
}
Other operation on BST:
3. To find the height of the tree.
int height(NODE root)
{
if(root==NULL)
return -1;
else
return(1+max(height(root->llink),height(root->rlink);
}
3) Define the following (with examples) i. Binary tree ii. Complete binary tree iii.
Binary search tree iv. Strictly Binary Tree v. Skewed binary tree.
4) Explain binary tree using Array representation and linked representation. Which
representation is more suitable and why?
5) What is the meaning of traversing a tree? What are the different traversal
techniques of a binary tree? Department
0r Write the routines to traverse the given string
of Computer Science, RLJIT 111
using i. Pre-order traversal ii. In-order traversal iii. Post-order traversal
Questions???
6) Define binary search tree. Write a function to insert an item and
recursive search algorithms for a binary search tree.
7) Write the routines for: i. Copying binary trees ii. Testing for equality of
binary trees
8) List the rules to construct the threads. Write the routines for in-order
traversal of a threaded binary tree.
9) Give in-order sequence: DJGBHEAFKIC and post-order sequence:
JGDHEBKIFCA. Construct BT for the same.
10) (a) Construct a binary tree for given expression: ((6+ (3-2)*5) ^2+3).
(b) Construct BST for the element step-by-step:
100, 85, 45, 55, 110, 20, 70, 65, 113, 145, 132, and 96.
11) For the given data, draw a binary search tree and show the array and
linked representation of the same: 100 ,85, 45, 55, 110, 20, 70, 65.
12) Construct a binary tree by using the following inorder and preorder
traversals:
inorder: BCAEDGHFI
preorder: ABCDEFGHI Department of Computer Science, RLJIT 112
Questions???
13) Consider the following tree T in (Fig. Q8(a)) write the preorder, inorder,
postorder for the tree T. Also find the depth of TREE in (Fig. Q8(a)).
14) Write recursive C function for inorder, preorder, postorder traversals of binary
tree. Also give the 3 traversals for the binary tree shown in Fig.Q7(b).