Skip to content

Commit 6f327e6

Browse files
committed
Added circumference of tree program
1 parent 0d92f62 commit 6f327e6

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

BinaryTreeAlgorithms/READ_ME.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Command to create Binary
2+
3+
make -f Makefile
4+
5+
# Output file binarytree

BinaryTreeAlgorithms/binarytree.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,47 @@ struct tree* findLargestBSTSubtree(struct tree *root) {
432432
findLargestBSTSubtree(root, min, max, maxNodes, largestBST);
433433
return largestBST;
434434
}
435+
436+
437+
void findCircumference(struct tree *root)
438+
{
439+
int currLevel = 0, nextLevel =0 , count = 0;
440+
struct tree *itr;
441+
stack<struct tree *> s1;
442+
stack<struct tree *> s2;
443+
stack<struct tree *> temp;
444+
if(root == NULL)
445+
return ;
446+
s1.push(root);
447+
++currLevel;
448+
while(!s1.empty())
449+
{
450+
itr= s1.top();
451+
s1.pop();
452+
if(itr->left !=NULL)
453+
{
454+
s2.push(itr->left);
455+
++nextLevel;
456+
}
457+
if(itr->right != NULL)
458+
{
459+
s2.push(itr->right);
460+
++nextLevel;
461+
}
462+
if((itr->left == NULL && itr->right == NULL) && (count != 0) && (count != currLevel))
463+
printf("\t%d", itr->data);
464+
if(count == 0 || count == currLevel-1)
465+
printf("\t%d", itr->data);
466+
++count;
467+
if(s1.empty())
468+
{
469+
s1 = s2;
470+
s2 = temp;
471+
count=0;
472+
currLevel = nextLevel;
473+
nextLevel = 0;
474+
}
475+
476+
}
477+
478+
}

BinaryTreeAlgorithms/binarytree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void printPreorderRecursive(struct tree *root);
3131

3232
void printPostorderRecursive(struct tree *root);
3333

34+
void findCircumference(struct tree *root);
3435

3536
void printLevelorder(struct tree *root);
3637

BinaryTreeAlgorithms/btappln.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ while(1)
2727
"\n17 Find Diamter of the tree"<<
2828
"\n18 Convert sorted array to BST"<<
2929
"\n19 Find Max BST"<<
30+
"\n20 Find Nodes in Circumference"<<
3031
"\n50 Exit\n";
3132
scanf("%d",&i);
3233
switch (i)
@@ -48,6 +49,8 @@ while(1)
4849
printLevelorder(root);
4950
break;
5051
case 6:
52+
printf("\nEnter the value \n");scanf("%d",&input);
53+
insertNodeBT(root,input);
5154
break;
5255
case 7:
5356
input=countNode(root);
@@ -109,6 +112,9 @@ while(1)
109112
// printLevelorder(itr);
110113
cout<<"not implemented yet\n";
111114
break;
115+
case 20:
116+
findCircumference(root);
117+
break;
112118
case 50:
113119
exit(1);
114120
default:

0 commit comments

Comments
 (0)