C Program to find Min and Max in binary tree without Recursion

By | 19.04.2017

Find Min and Max in binary tree


Write a C Program to find Min and Max in binary tree without Recursion. Here’s simple Program to find minimum and maximum value in binary search tree without Recursion in C Programming Language.


What is Tree ?


In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.

A tree data structure can be defined as follows…

  • Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.

A tree data structure can also be defined as follows…

  • Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.

Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.


Below is the source code for C Program to find Min and Max in binary search tree without Recursion which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

/*  C Program to find Min and Max in binary tree without Recursion  */

 

#include<stdio.h>

#include<stdlib.h>

#define MAX 50

 

struct node

{

struct node *lchild;

int info;

struct node *rchild;

};

 

struct node *min_nrec(struct node *root);

struct node *max_nrec(struct node *root);

struct node *insert_nrec(struct node *root, int ikey );

void display(struct node *ptr,int level);

 

 

int main( )

{

struct node *root=NULL, *ptr;

int choice,k;

 

while(1)

{

printf(“\n”);

printf(“1.Insert\n”);

printf(“2.Display\n”);

printf(“3.Find minimum and maximum\n”);

printf(“4.Quit\n”);

printf(“\nEnter your choice : “);

scanf(“%d”,&choice);

 

switch(choice)

{

 

 

case 1:

printf(“\nEnter the key to be inserted : “);

scanf(“%d”,&k);

root = insert_nrec(root, k);

break;

 

        case 2:

printf(“\n”);

display(root,0);

printf(“\n”);

break;

 

        case 3:

ptr = min_nrec(root);

if(ptr!=NULL)

printf(“\nMinimum key is %d\n”, ptr->info );

ptr = max_nrec(root);

if(ptr!=NULL)

printf(“\nMaximum key is %d\n”, ptr->info );

break;

 

case 4:

exit(1);

 

default:

printf(“\nWrong choice\n”);

}/*End of switch*/

}/*End of while */

 

return 0;

 

}/*End of main( )*/

 

 

struct node *insert_nrec(struct node *root, int ikey)

{

struct node *tmp,*par,*ptr;

 

ptr = root;

par = NULL;

 

while( ptr!=NULL)

{

par = ptr;

if(ikey < ptr->info)

ptr = ptr->lchild;

else if( ikey > ptr->info )

ptr = ptr->rchild;

else

{

printf(“Duplicate key”);

return root;

}

}

 

tmp=(struct node *)malloc(sizeof(struct node));

tmp->info=ikey;

tmp->lchild=NULL;

tmp->rchild=NULL;

 

if(par==NULL)

root=tmp;

else if( ikey < par->info )

par->lchild=tmp;

else

par->rchild=tmp;

 

return root;

}/*End of insert_nrec( )*/

 

 

struct node *min_nrec(struct node *ptr)

{

if(ptr!=NULL)

while(ptr->lchild!=NULL)

ptr=ptr->lchild;

return ptr;

}/*End of min_nrec()*/

 

struct node *max_nrec(struct node *ptr)

{

if(ptr!=NULL)

while(ptr->rchild!=NULL)

ptr=ptr->rchild;

return ptr;

}/*End of max_nrec()*/

 

void display(struct node *ptr,int level)

{

int i;

if(ptr == NULL )/*Base Case*/

return;

else

    {

display(ptr->rchild, level+1);

printf(“\n”);

for (i=0; i<level; i++)

printf(”    “);

printf(“%d”, ptr->info);

display(ptr->lchild, level+1);

}

}/*End of display()*/


OUTPUT : :


C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

/* C Program for Min and Max in binary tree without Recursion */

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 8

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 4

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 7

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 1

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 9

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 11

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 1

 

Enter the key to be inserted : 10

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 2

 

 

        11

            10

    9

8

        7

    4

        1

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 3

 

Minimum key is 1

 

Maximum key is 11

 

1.Insert

2.Display

3.Find minimum and maximum

4.Quit

 

Enter your choice : 4

 

Process returned 1


If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments