Skip to content

Commit 8781c41

Browse files
committed
Maximum Element of Binary Tree added
1 parent 02fb304 commit 8781c41

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Given a Binary Tree , Print the value of maximum node key
3+
4+
Argument/Return Type
5+
Input of total no.of nodes is taken
6+
Input of key values of nodes of tree are taken in level order form
7+
Incase of a null node , -1 is taken as input
8+
A function which returns the maximum element
9+
If the root is NULL it prints -1
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
using namespace std;
14+
15+
//Define Node as structure
16+
struct Node
17+
{
18+
int key;
19+
Node* left;
20+
Node* right;
21+
};
22+
23+
// Function to create a node with 'value' as the data stored in it.
24+
// Both the children of this new Node are initially null.
25+
struct Node* newNode(int value)
26+
{
27+
Node* n = new Node;
28+
n->key = value;
29+
n->left = NULL;
30+
n->right = NULL;
31+
return n;
32+
}
33+
34+
// Function to build tree with given input
35+
struct Node* createTree(vector<int>v)
36+
{
37+
int n=v.size();
38+
if(n==0)
39+
return NULL;
40+
vector<struct Node* >a(n);
41+
//Create a vector of individual nodes with given node values
42+
for(int i=0;i<n;i++)
43+
{
44+
//If the data is -1 , create a null node
45+
if(v[i]==-1)
46+
a[i] = NULL;
47+
else
48+
a[i] = newNode(v[i]);
49+
}
50+
//Interlink all created nodes to create a tree
51+
//Use two pointers using int to store indexes
52+
//One to keep track of parent node and one for children nodes
53+
for(int i=0,j=1;j<n;i++)
54+
{
55+
//If the parent node is NULL , advance children pointer twice
56+
if(!a[i])
57+
{
58+
j=j+2;
59+
continue;
60+
}
61+
//Connect the two children nodes to parent node
62+
//First left and then right nodes
63+
a[i]->left = a[j++];
64+
if(j<n)
65+
a[i]->right = a[j++];
66+
}
67+
return a[0];
68+
}
69+
70+
//Function to return maximum value of root key of the given tree
71+
int FindMax(struct Node* root)
72+
{
73+
74+
// If root is NULL , return -1
75+
if (root == NULL)
76+
return -1;
77+
//Initialise a maximum variable with -1 value
78+
// keep track of it traversing the tree in Pre Order Fashion
79+
int maximum=-1;
80+
81+
// Create an empty stack and push root to it
82+
stack<struct Node*> stack;
83+
84+
while (1)
85+
{
86+
while(root)
87+
{
88+
//keep visiting left branch , untill we reach extreme left node
89+
//During each visit , keep updating the maximum value
90+
maximum=max(maximum,root->key);
91+
stack.push(root);
92+
root=root->left;
93+
}
94+
if(stack.empty())
95+
break;
96+
// Now consider the most recently visited node
97+
root=stack.top();
98+
stack.pop();
99+
//and run Pre Order traversal for its right child
100+
root=root->right;
101+
//repeat the process till there are no more nodes left
102+
}
103+
return maximum;
104+
}
105+
106+
// Driver code
107+
int main()
108+
{
109+
110+
int n;
111+
cout<<"Enter total no.of nodes of the input Tree ( including NULL nodes ) : ";
112+
cin>>n;
113+
114+
vector<int>v(n);
115+
cout<<"Enter value of each node of the tree in level order ( if a node is NULL , enter -1 ) with spaces"<<endl;
116+
for(int i=0;i<n;i++)
117+
{
118+
cin>>v[i]; //store the input values in a vector
119+
}
120+
121+
//create the tree using input node values
122+
struct Node* root=createTree(v);
123+
124+
//Call the function and print the result
125+
cout<<"The maximum value of the node key of given tree is "<<FindMax(root);
126+
127+
return 0;
128+
}
129+
130+
/*
131+
if node is NULL , -1 is entered as it's key
132+
Sample Test Case
133+
Input Binary Tree :
134+
10
135+
/ \
136+
11 12
137+
/ \ / \
138+
5 NULL 6 13
139+
/ \ / \ / \ / \
140+
4 NULL NULL NULL 8 9 10 NULL
141+
Input :
142+
Enter total no.of nodes of the input Tree ( including NULL nodes ) : 15
143+
Enter value of each node of the tree in level order ( if a node is NULL , enter -1 ) with spaces
144+
10 11 12 5 -1 6 13 4 -1 -1 -1 8 9 10 -1
145+
146+
Output :
147+
The maximum value of the node key of given tree is 13
148+
149+
Time Complexity : O(n)
150+
Where n is the no.of nodes
151+
Space Complexity : O(n)
152+
Where n is the no.of nodes
153+
*/

0 commit comments

Comments
 (0)