Skip to content

Commit 5d8cdc3

Browse files
author
Parth Garg
committed
Binary Tree
1 parent be6dff8 commit 5d8cdc3

File tree

25 files changed

+101
-69
lines changed

25 files changed

+101
-69
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
/*
7+
8+
-->Tree stores data in the form of
9+
hierarchical form.
10+
11+
-->Non-linear data structure.
12+
13+
Applications of Tree Data Structure:
14+
1.used to represent hierarchical data.
15+
-->organisation structure
16+
-->folder structure
17+
-->XML/HTML content
18+
-->In OOPs(inheritance)
19+
2.binary search trees
20+
3.binary heap
21+
4.B and B+ trees in DBMS
22+
5.Spanning and shortest path trees
23+
6.Parse trees, Comparison Trees in Compilers.
24+
25+
*/
26+
}

15. Binary Tree/01 Tree Class/01.cpp 15. Binary Tree/02 Tree Class/02.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#include<bits/stdc++.h>
22
using namespace std;
33

4+
/*
5+
Binary Tree:
6+
Every node has almost two
7+
children.
8+
*/
9+
410
class Node
511
{
612
public:

15. Binary Tree/02 Types of Tree Traversals/02.cpp 15. Binary Tree/03 Types of Tree Traversals/03.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ int main()
55
{
66
/*
77
8-
Traversals:
8+
Tree Traversals:
9+
It means printing every key of tree exactly ones.
910
1011
1. Breadth First Traversal / level Order Traversal
1112
1213
2. Depth First Traversal
13-
1414
2.1 Inorder Traversal (Left Root Right)
1515
2.2 Preorder Traversal (Root Left Right)
1616
2.3 Postorder Traversal (Left Right Root)

15. Binary Tree/04 Preorder Traversal/04.cpp 15. Binary Tree/04 Inorder Traversal/04.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(logN)
21-
//Preorder: Root Left Right
22-
void preorder(Node* root)
20+
//Space: O(H)
21+
//Inorder: left Root Right
22+
void inorder(Node* root)
2323
{
2424
if(root==NULL)
2525
{
2626
return ;
2727
}
2828

29+
inorder(root->left);
2930
cout<<root->data<<" ";
30-
preorder(root->left);
31-
preorder(root->right);
31+
inorder(root->right);
3232
}
3333

3434
int main()
@@ -44,7 +44,8 @@ int main()
4444
root->right->left=new Node(60);
4545
root->right->right=new Node(70);
4646

47-
preorder(root);
47+
inorder(root);
48+
cout<<endl;
4849

4950
return 0;
5051
}

15. Binary Tree/05 Postorder Traversal/05.cpp 15. Binary Tree/05 Preorder Traversal/05.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(logN)
21-
//Postorder: Left Right Root
22-
void postorder(Node* root)
20+
//Space: O(H)
21+
//Preorder: Root Left Right
22+
void preorder(Node* root)
2323
{
2424
if(root==NULL)
2525
{
2626
return ;
2727
}
2828

29-
postorder(root->left);
30-
postorder(root->right);
3129
cout<<root->data<<" ";
30+
preorder(root->left);
31+
preorder(root->right);
3232
}
3333

3434
int main()
@@ -44,7 +44,8 @@ int main()
4444
root->right->left=new Node(60);
4545
root->right->right=new Node(70);
4646

47-
postorder(root);
47+
preorder(root);
48+
cout<<endl;
4849

4950
return 0;
5051
}

15. Binary Tree/03 Inorder Traversal/03.cpp 15. Binary Tree/06 Postorder Traversal/06.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(logN)
21-
//Inorder: left Root Right
22-
void inorder(Node* root)
20+
//Space: O(H)
21+
//Postorder: Left Right Root
22+
void postorder(Node* root)
2323
{
2424
if(root==NULL)
2525
{
2626
return ;
2727
}
2828

29-
inorder(root->left);
29+
postorder(root->left);
30+
postorder(root->right);
3031
cout<<root->data<<" ";
31-
inorder(root->right);
3232
}
3333

3434
int main()
@@ -44,7 +44,8 @@ int main()
4444
root->right->left=new Node(60);
4545
root->right->right=new Node(70);
4646

47-
inorder(root);
47+
postorder(root);
48+
cout<<endl;
4849

4950
return 0;
5051
}

15. Binary Tree/06 Height of Tree/06.cpp 15. Binary Tree/07 Height of Tree/07.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(logN)
20+
//Space: O(H)
2121
int height(Node* root)
2222
{
2323
if(root==NULL)
@@ -42,6 +42,5 @@ int main()
4242
root->right->right=new Node(70);
4343

4444
cout<<height(root)<<endl;
45-
4645
return 0;
4746
}

15. Binary Tree/07 Print Nodes at K Distance/07.cpp 15. Binary Tree/08 Print Nodes at K Distance/08.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(logN)
20+
//Space: O(H)
2121
//K values: [0,Height-1]
2222
void nodesAtKDistance(Node* root,int k)
2323
{
@@ -51,6 +51,7 @@ int main()
5151
root->right->right=new Node(70);
5252

5353
nodesAtKDistance(root,2);
54-
54+
cout<<endl;
55+
5556
return 0;
5657
}

15. Binary Tree/09 Level Order Traversal Line by Line/09a.cpp 15. Binary Tree/09 Level Order Traversal/09a.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ void levelOrder(Node* root)
5555
for(int i=0;i<h;i++)
5656
{
5757
nodesAtKDistance(root,i);
58-
cout<<endl;
5958
}
6059
}
6160

@@ -73,6 +72,7 @@ int main()
7372
root->right->right=new Node(70);
7473

7574
levelOrder(root);
75+
cout<<endl;
7676

7777
return 0;
7878
}

15. Binary Tree/09 Level Order Traversal Line by Line/09b.cpp 15. Binary Tree/09 Level Order Traversal/09b.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(N)
21-
//when complete level is traversed,push NULL into queue
20+
//Space: O(W)
2221
void levelOrder(Node* root)
2322
{
2423
if(root==NULL)
@@ -28,20 +27,12 @@ void levelOrder(Node* root)
2827

2928
queue<Node*> q;
3029
q.push(root);
31-
q.push(NULL);
3230

33-
while(q.size()>1)
31+
while(q.empty()==false)
3432
{
3533
Node* curr=q.front();
3634
q.pop();
3735

38-
if(curr==NULL)
39-
{
40-
cout<<endl;
41-
q.push(NULL);
42-
continue;
43-
}
44-
4536
cout<<curr->data<<" ";
4637

4738
if(curr->left)
@@ -70,6 +61,7 @@ int main()
7061
root->right->right=new Node(70);
7162

7263
levelOrder(root);
73-
64+
cout<<endl;
65+
7466
return 0;
7567
}

15. Binary Tree/08 Level Order Traversal/08a.cpp 15. Binary Tree/10 Level Order Traversal Line by Line/10a.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void levelOrder(Node* root)
5555
for(int i=0;i<h;i++)
5656
{
5757
nodesAtKDistance(root,i);
58+
cout<<endl;
5859
}
5960
}
6061

@@ -72,6 +73,5 @@ int main()
7273
root->right->right=new Node(70);
7374

7475
levelOrder(root);
75-
7676
return 0;
7777
}

15. Binary Tree/08 Level Order Traversal/08b.cpp 15. Binary Tree/10 Level Order Traversal Line by Line/10b.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(W)
20+
//Space: O(N)
21+
//when complete level is traversed,push NULL into queue
2122
void levelOrder(Node* root)
2223
{
2324
if(root==NULL)
@@ -27,12 +28,20 @@ void levelOrder(Node* root)
2728

2829
queue<Node*> q;
2930
q.push(root);
31+
q.push(NULL);
3032

31-
while(q.empty()==false)
33+
while(q.size()>1)
3234
{
3335
Node* curr=q.front();
3436
q.pop();
3537

38+
if(curr==NULL)
39+
{
40+
cout<<endl;
41+
q.push(NULL);
42+
continue;
43+
}
44+
3645
cout<<curr->data<<" ";
3746

3847
if(curr->left)
@@ -61,6 +70,7 @@ int main()
6170
root->right->right=new Node(70);
6271

6372
levelOrder(root);
64-
73+
cout<<endl;
74+
6575
return 0;
6676
}

15. Binary Tree/11 Maximum in Binary Tree/11a.cpp 15. Binary Tree/11 Size of Binary Tree/11a.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class Node
1818

1919
//Time: O(N)
2020
//Space: O(W)
21-
int maximum(Node* root)
21+
int size(Node* root)
2222
{
2323
if(root==NULL)
2424
{
2525
return 0;
2626
}
2727

28-
int max_val=INT_MIN;
28+
int count=0;
2929

3030
queue<Node*> q;
3131
q.push(root);
@@ -35,10 +35,7 @@ int maximum(Node* root)
3535
Node* curr=q.front();
3636
q.pop();
3737

38-
if(curr)
39-
{
40-
max_val=max(max_val,curr->data);
41-
}
38+
count++;
4239

4340
if(curr->left)
4441
{
@@ -51,7 +48,7 @@ int maximum(Node* root)
5148
}
5249
}
5350

54-
return max_val;
51+
return count;
5552
}
5653

5754
int main()
@@ -67,7 +64,6 @@ int main()
6764
root->right->left=new Node(60);
6865
root->right->right=new Node(70);
6966

70-
cout<<maximum(root)<<endl;
71-
67+
cout<<size(root)<<endl;
7268
return 0;
7369
}

15. Binary Tree/10 Size of Binary Tree/10b.cpp 15. Binary Tree/11 Size of Binary Tree/11b.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Node
1717
};
1818

1919
//Time: O(N)
20-
//Space: O(logN);
20+
//Space: O(H);
2121
int size(Node* root)
2222
{
2323
if(root==NULL)
@@ -42,6 +42,5 @@ int main()
4242
root->right->right=new Node(70);
4343

4444
cout<<size(root)<<endl;
45-
4645
return 0;
4746
}

0 commit comments

Comments
 (0)