0% found this document useful (0 votes)
7 views23 pages

Trees in Discrete Algorithm and Analysis

The document provides an overview of trees, particularly focusing on binary trees, their types (including strictly binary trees and complete binary trees), and binary search trees (BST). It explains tree structures, levels, and various tree traversal methods (inorder, preorder, postorder), along with examples of constructing and deleting nodes in BSTs. Additionally, it includes a C implementation for inserting, traversing, and searching in a BST, as well as applications of trees in expression trees.

Uploaded by

Charan Adabala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Trees in Discrete Algorithm and Analysis

The document provides an overview of trees, particularly focusing on binary trees, their types (including strictly binary trees and complete binary trees), and binary search trees (BST). It explains tree structures, levels, and various tree traversal methods (inorder, preorder, postorder), along with examples of constructing and deleting nodes in BSTs. Additionally, it includes a C implementation for inserting, traversing, and searching in a BST, as well as applications of trees in expression trees.

Uploaded by

Charan Adabala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Trees

A forest is a collection of Trees. A tree is nothing but collection of nodes or


elements. A tree can contain any number of Childs and there is no restriction
of number of Childs.
A

B C D

E F G H I J K L M

Fig(a).Tree
Binary Tree: It is a tree with finite set of nodes which consists of a root itself or
root with left sub tree or right sub tree or consists of both.

Left sub tree Right sub tree


B C

D E F G

H I

Fig(b). Binary Tree


In fig(b) A is root and B and C are Childs of A. A is parent of B and C therefore B
and c called as Siblings i.e B and C has same parent A.
Therefore the Binary tree contains zero children or one children or two
children.
A node with zero children is called as Leaf node.
Level: The level of root is zero and all the other nodes have one greater than
its parent. If we consider fig(b) .
Level of A=0
Level of B and C is 1.
Level of D,E,F and G has 2.
Level of H and I is 3.
The maximum level of a tree is called as depth of tree, here depth is 3.
If the binary tree with one node is nothing but root itself and its left and right
sub trees are empty.
If the binary trees with two nodes are one will be the root and the other may
be its left child or right child.
A C

B
D

Fig. Two binary trees with two nodes


If the binary tree consists of three nodes then we get 5 different binary trees,
these are shown in below.
A D D D
D

B E E F E
E

C F F
F

Fig. Five binary trees with three nodes


Similarly binary tree with 4 nodes we get 14 different binary trees.
Strictly Binary Tree: It is a binary tree and every non-leaf node has left sub tree
and right sub tree i.e each node in binary tree consists of 0 or 2 children.
Strictly binary tree with n leaves always contain (2n-1) nodes.
Strictly binary tree with n non-leaves always contain (2n+1) nodes.
A

B C

D E

F G

Fig. Strictly Binary Tree


Complete Binary Tree: It is a binary tree whose non leaf node has non empty
left and right sub tree and all the leaves must lie at the same level.
A

B C

D E F G

Fig. Complete Binary Tree


Binary Search Tree(BST): A BST is a binary tree in which every element in left
sub tree less than the root and every element in right sub tree greater than or
equal to root.
15 15
5

10 18

8
20
Fig. Binary Search Trees 0

Ex1: Construct the binary search tree with the following elements:
10,15,12,7,8,18,6,9,17,20
Step1: The first element 10 treated has root
root
10
5
step2:The element 15 greater than the root therefore it is inserted as right
child of root.
10
root
5

15
5
Step3:The element 12 is greater than the root and less than the 15 therefore it
is inserted as left child of 15. 10 root
5

15
5

12
5
Step4: The element 7 is less than the root therefore it is inserted as left child of
root. 10 root
5

7 15
5

12
5

step5: The element 8 is less than the root and greater than 7 therefore it is
inserted as right child of 7. root
10
5

7 15
5

8 12
5
step6: After insertion of element 18.
root
10
5

7 15
5

8 12 18
5 5
step7: After insertion of element 6.
root
10
5

7 15
5

6 8 12 18
5 5
step8: After insertion of element 9.
10 root
5

7 15
5

6 8 12 18
5 5

9
Step9: After insertion of element 17.

root
10
5

7 15
5

6 8 12 18
5 5

9 17
Step10: After insertion of element 20. 5
root
10
5

7 15
5

6 8 12 18
5 5

9 17 20
5

Ex2: Construct BST with elements D,F,B,C,E,A,G

Step1: The first element D treated has root


root
D

step2:The element F greater than the root therefore it is inserted as right child
of root.
D
root

Step3:The element B is less than the root therefore it is inserted as left child of
root. D root

B F
Step4: The element C is less than the root therefore it is inserted as right child
of B. D

B F

step5: The element E is greater than the root and less than F therefore it is
inserted as left child of F. root
D

B F

C E

step6: After insertion of element A.


root
D

B F

A C E

step7: After insertion of element G.


root
D

B F

A C E G
EX3: Construct BST with elements 25,15,40,20,30,85,10,5,50,45,8,18.

Tree Traversals:
It is nothing but process each node exactly once without leaving any node. It is
used to print node values in binary tree. But in lists we start from root node,
the root consists of first node address and we follow the links until we
encounter the NULL value. In trees we start from root after root there are two
ways i.e left or right.
Therefore 3 traversing techniques available.
1. Inorder Traversal (left, root, right)
2. Preorder Traversal (root, left, right)
3. Postorder Traversal (left, right, root)
Ex1: root Inorder Traversal: B A C
A
Preorder Traversal: A B C
B C Postorder Traversal: B C A
Fig. Binary Tree

Ex2: Consider the Binary tree then find the Inorder, Preorder and Postorder
Traversal
10

7 15
5

6 8 12 18
5 5

Fig. Binary Tree


Inorder Traversal: 6 7 8 10 12 15 18
Preorder Traversal: 10 7 6 8 15 12 18
Postorder Traversal: 6 8 7 12 18 15 10
Ex3: Consider the Binary tree then find the Inorder, Preorder and Postorder
Traversal
A root

D E
Fig. Binary Tree
Inorder Traversal: B D C E A
Preorder Traversal: A B C D E
Postorder Traversal: D E C B A

Ex5:Consider the Inorder: 5 15 18 20 22 25 28


Preorder:20 15 5 18 25 22 28 and construct the BST?
Ans: The following steps are used to construct the BST
1. The first element in preorder treated as root of the BST and finds this
element in Inorder and the elements left of this element comes to left sub
tree and elements right of this element comes under the right sub tree.

2.The first element after the root in preorder inserted as left child in BST and
examine the inorder and preorder to identify the occurrence of each element.

3.This process is repeated until left sub tree and right sub tree constructed.

20 root

15 25
5

5 18 22 28

Fig.BST
Ex6: Consider the Inorder: D B A E C F
Preorder: A B D C E F and construct the BST?
A root

B C

D E F
Fig.BST
Binary Tree Deletions:
Consider the Binary Tree

root
50
5

40 60

35 45 55 70

The node deletion in Binary Tree consists of 3 cases


Case 1: Deletion of a leaf node
Case 2: Deletion of a node with One Children
Case 3: Deletion of a node with Two Children
Case 1: Deletion of a leaf node
It consists of two sub cases a) The leaf node is right child of a parent
b) The leaf node is left child of a parent
a) The leaf node is right child of a parent: if we consider the above binary tree
then 60 parent

70 temp

here right child of parent i.e temp set to NULL

b) The leaf node is left child of a parent: : if we consider the above binary tree
then 60 parent

55 temp

here left child of parent i.e temp set to NULL


Case 2: Deletion of a node with One Children:
If we consider the above binary tree then it consists of four sub cases

parent
(a) 50 50 (b)parent (c)
parent
50 (d)
50
5 parent 5 5
5
40 temp 40 temp temp
temp 60
60

child child child


35 45 70
55 child

In (a)left child of temp is assigned to left child of parent then ,we get
50 parent
5

35 child

In (b)right child of temp is assigned to left child of parent then ,we get
50 parent
5

45 child
In (c)left child of temp is assigned to right child of parent then ,we get

50 parent
5

55 child

In (d)right child of temp is assigned to right child of parent then ,we get
parent
50
5

child
70
Case 3: Deletion of a node with Two Children
Here first find we find the Inorder successor for the deleted node then that
node is replaced with Inorder successor.
Consider the following binary tree
root
50
5

40 60 temp

35 45 55 70

68 Junior Parent(JP)

65 Inorder Successor(IS)

66
Fig. Binary Tree
Here we delete the node 60 its inorder successor is 65 .The IS is available at
leftmost children of right child, once we find the IS then right child of IS
attached to left child of JP. Finally temp is replaced with IS, Then we get
50
5

40 65 Inorder Successor(IS)

35 45 55 70

68 Junior Parent(JP)

66
Fig. Binary tree after deletion
//Implementation of BST

#include<stdio.h>

struct node

int data;

struct node *left,*right;

};

typedef struct node nodeptr;

nodeptr *ins(nodeptr *root,int x);

void inorder(nodeptr *root);

void preorder(nodeptr *root);

void postorder(nodeptr *root);

void search(nodeptr *root,int x);

void main()

int ch,x;

nodeptr *root=NULL;

clrscr();

do

printf("\n 1.Insertion");

printf("\n 2.Inorder");

printf("\n 3.Preorder");

printf("\n 4.Postorder");

printf("\n 5.Search");
printf("\n 6.Exit");

printf("\n enter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:printf("\n ele:");

scanf("%d",&x);

root=ins(root,x);

break;

case 2:inorder(root);

break;

case 3:preorder(root);

break;

case 4:postorder(root);

break;

case 5:printf("\n ele:");

scanf("%d",&x);

search(root,x);

break;

case 6:exit(0);

}
}while(ch<=6);

nodeptr *ins(nodeptr *root,int x)

nodeptr *p,*temp,*parent;

p=(nodeptr *)malloc(sizeof(nodeptr));

p->data=x;

p->left=p->right=NULL;

if(root==NULL)

root=p;

printf("\n The ele %d inserted as a root",x);

return(root);

else

temp=root;

parent=NULL;

while(temp!=NULL)

if(x>temp->data)

parent=temp;

temp=temp->right;

}
else if(x<temp->data)

parent=temp;

temp=temp->left;

if(x>parent->data)

parent->right=p;

printf("\n The ele %d inserted as right child of


%d",x,parent->data);

return(root);

if(x<parent->data)

parent->left=p;

printf("\n The ele %d inserted as left child of


%d",x,parent->data);

return(root);

void inorder(nodeptr *root)

nodeptr *p;

p=root;
if(p!=NULL)

inorder(p->left);

printf("%4d",p->data);

inorder(p->right);

void preorder(nodeptr *root)

nodeptr *p;

p=root;

if(p!=NULL)

printf("%4d",p->data);

preorder(p->left);

preorder(p->right);

void postorder(nodeptr *root)

nodeptr *p;

p=root;

if(p!=NULL)

postorder(p->left);

postorder(p->right);
printf("%4d",p->data);

void search(nodeptr *root,int x)

nodeptr *temp;

int f=0;

temp=root;

while(temp!=NULL)

if(x==temp->data)

f=1;

break;

else if(x>temp->data)

temp=temp->right;

else if(x<temp->data)

temp=temp->left;

if(f==1)

printf("\n The ele %d found",x);

else

printf("\n The ele %d not found",x);

}
Applications of Trees or Expression Trees:
The binary tree is constructed by using infix expression or postfix expression
then those tree are called as Expression trees.
(a) Construct of Expression tree by using Infix Expression:
Here the middle of the operator is treated as root for Expression tree
the two operands inserted as left child and right child of operator.
Ex: Consider the Infix Expression A + B
+

A B

Fig Expression tree for Infix expression A + B


If we traverse Inorder in Expression tree then we get infix expression i.e A+B
If we traverse Preorder in Expression tree then we get Prefix expression i.e +AB
If we traverse Postorder in Expression tree we get Postorder expression i.e
AB+
Ex2: Consider the Infix Expression (A + B) * ( C - D)
*

+ -

A B C D

Fig Expression tree for Infix expression (A + B) * ( C - D)

(b) Construct of Expression tree by using Postfix Expression:


Here we select the stack of pointers whenever we get the operand we
assign pointer to that node, whenever we get operator we pop top two
pointers from the stack and first popped pointer node inserted as right
child and second popped pointer node inserted as left child to operator.
Ex: Consider the Postfix Expression A B +
Step1: After the insertion of node A

A
Step2: After the insertion of node B

A B

Step3: After the insertion of Operator +

A B

Fig Expression tree for Infix expression A B +

Ex2: Consider the Postfix Expression A B + C D - *

Step1: After the insertion of node A

Step2: After the insertion of node B

A B

Step3: After the insertion of Operator +

A B
Step4: After the insertion of node C

+ C

A B

Step5: After the insertion of node D

+ C D

A B

Step6: After the insertion of Operator -

+ -

A B C D

Step7: After the insertion of Operator *

+ -

A B C D

Fig Expression tree for Infix expression A B + C D - *


Threaded Binary Tree: Consider the Binary Tree

20

15 25
5

5 18 22 28

Fig(a). Binary Tree

The above Binary Tree is represented as the following

1000 20 2000

500

3000 15 4000 5000 25 6000

1000 2000

5 18 22 28

3000 4000 5000 6000

Fib (b).Binary Tree for above tree

It consists of 14 pointers in those 14 pointers we are using 6 pointers and


wasting 8 pointers by storing the NULL value. Therefore we are using 40%
pointers and wasting 60%, this is avoided by using Threaded Binary Tree. In
this first we generate the inorder for the given binary tree then we maintain
the threads from the pointers of unused nodes to inorder successor and
predecessor. If there is still unused pointers are available then these are
connected to root node by using threads.

Inorder for the above Binary tree 5 15 18 20 22 25 28

Threaded binary tree for above binary tree


1000 20 2000

500

3000 15 4000 5000 25 6000

1000 2000

5 18 22 28

3000 4000 5000 6000

Fig(c) Threaded Binary tree for above Binary Tree

Binary Tree Representations: The Binary tree represented in two ways

(i)Linked List Representation

(ii)Array Representation or Sequential Representation.

(i)Linked List Representation: In this we maintain three parallel arrays, each


node in binary tree must be store in some location for example it is stored at
location k then it consists of three values.

data(k)- it gives the data value

left(k)-it gives the address of left child

right(k)-it gives the address of right child


A

B C

D E F G

H I J K

Fig.(a) Binary Tree


The Linked List Representation for

Data left right

0 G -1 2

1
H -1 -1

2 K -1 -1

0
3 A 5 7
root
4 D 1 -1

5 B 4 8

6 F -1 -1

7 C 6 0

8
E 9 10

9 I -1 -1

0
10 J -1 -1

(ii)Array or Sequential Representation: Here we use only one array. The root
node stored at 0th location and its left child is stored at 1st location and its right
child is available at 2 nd location and so on….
In general the root node is stored at k th location and its
left child is available at (2k+1) and its
right child is available at (2k+2)
Array or sequential Representation for above binary tree
A B C D E F G H I J K
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

You might also like