File tree 25 files changed +101
-69
lines changed
03 Types of Tree Traversals
08 Print Nodes at K Distance
10 Level Order Traversal Line by Line
12 Maximum in Binary Tree
13 Left View of Binary Tree
15 Check for Balanced Binary Tree
16 Maximum Width of Binary Tree
17 Convert Binary Tree to Doubly Linked List
25 Iterative Inorder Traversal
25 files changed +101
-69
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
#include < bits/stdc++.h>
2
2
using namespace std ;
3
3
4
+ /*
5
+ Binary Tree:
6
+ Every node has almost two
7
+ children.
8
+ */
9
+
4
10
class Node
5
11
{
6
12
public:
Original file line number Diff line number Diff line change @@ -5,12 +5,12 @@ int main()
5
5
{
6
6
/*
7
7
8
- Traversals:
8
+ Tree Traversals:
9
+ It means printing every key of tree exactly ones.
9
10
10
11
1. Breadth First Traversal / level Order Traversal
11
12
12
13
2. Depth First Traversal
13
-
14
14
2.1 Inorder Traversal (Left Root Right)
15
15
2.2 Preorder Traversal (Root Left Right)
16
16
2.3 Postorder Traversal (Left Right Root)
Original file line number Diff line number Diff line change @@ -17,18 +17,18 @@ class Node
17
17
};
18
18
19
19
// 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)
23
23
{
24
24
if (root==NULL )
25
25
{
26
26
return ;
27
27
}
28
28
29
+ inorder (root->left );
29
30
cout<<root->data <<" " ;
30
- preorder (root->left );
31
- preorder (root->right );
31
+ inorder (root->right );
32
32
}
33
33
34
34
int main ()
@@ -44,7 +44,8 @@ int main()
44
44
root->right ->left =new Node (60 );
45
45
root->right ->right =new Node (70 );
46
46
47
- preorder (root);
47
+ inorder (root);
48
+ cout<<endl;
48
49
49
50
return 0 ;
50
51
}
Original file line number Diff line number Diff line change @@ -17,18 +17,18 @@ class Node
17
17
};
18
18
19
19
// 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)
23
23
{
24
24
if (root==NULL )
25
25
{
26
26
return ;
27
27
}
28
28
29
- postorder (root->left );
30
- postorder (root->right );
31
29
cout<<root->data <<" " ;
30
+ preorder (root->left );
31
+ preorder (root->right );
32
32
}
33
33
34
34
int main ()
@@ -44,7 +44,8 @@ int main()
44
44
root->right ->left =new Node (60 );
45
45
root->right ->right =new Node (70 );
46
46
47
- postorder (root);
47
+ preorder (root);
48
+ cout<<endl;
48
49
49
50
return 0 ;
50
51
}
Original file line number Diff line number Diff line change @@ -17,18 +17,18 @@ class Node
17
17
};
18
18
19
19
// 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)
23
23
{
24
24
if (root==NULL )
25
25
{
26
26
return ;
27
27
}
28
28
29
- inorder (root->left );
29
+ postorder (root->left );
30
+ postorder (root->right );
30
31
cout<<root->data <<" " ;
31
- inorder (root->right );
32
32
}
33
33
34
34
int main ()
@@ -44,7 +44,8 @@ int main()
44
44
root->right ->left =new Node (60 );
45
45
root->right ->right =new Node (70 );
46
46
47
- inorder (root);
47
+ postorder (root);
48
+ cout<<endl;
48
49
49
50
return 0 ;
50
51
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ class Node
17
17
};
18
18
19
19
// Time: O(N)
20
- // Space: O(logN )
20
+ // Space: O(H )
21
21
int height (Node* root)
22
22
{
23
23
if (root==NULL )
@@ -42,6 +42,5 @@ int main()
42
42
root->right ->right =new Node (70 );
43
43
44
44
cout<<height (root)<<endl;
45
-
46
45
return 0 ;
47
46
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ class Node
17
17
};
18
18
19
19
// Time: O(N)
20
- // Space: O(logN )
20
+ // Space: O(H )
21
21
// K values: [0,Height-1]
22
22
void nodesAtKDistance (Node* root,int k)
23
23
{
@@ -51,6 +51,7 @@ int main()
51
51
root->right ->right =new Node (70 );
52
52
53
53
nodesAtKDistance (root,2 );
54
-
54
+ cout<<endl;
55
+
55
56
return 0 ;
56
57
}
Original file line number Diff line number Diff line change @@ -55,7 +55,6 @@ void levelOrder(Node* root)
55
55
for (int i=0 ;i<h;i++)
56
56
{
57
57
nodesAtKDistance (root,i);
58
- cout<<endl;
59
58
}
60
59
}
61
60
@@ -73,6 +72,7 @@ int main()
73
72
root->right ->right =new Node (70 );
74
73
75
74
levelOrder (root);
75
+ cout<<endl;
76
76
77
77
return 0 ;
78
78
}
Original file line number Diff line number Diff line change @@ -17,8 +17,7 @@ class Node
17
17
};
18
18
19
19
// Time: O(N)
20
- // Space: O(N)
21
- // when complete level is traversed,push NULL into queue
20
+ // Space: O(W)
22
21
void levelOrder (Node* root)
23
22
{
24
23
if (root==NULL )
@@ -28,20 +27,12 @@ void levelOrder(Node* root)
28
27
29
28
queue<Node*> q;
30
29
q.push (root);
31
- q.push (NULL );
32
30
33
- while (q.size ()> 1 )
31
+ while (q.empty ()== false )
34
32
{
35
33
Node* curr=q.front ();
36
34
q.pop ();
37
35
38
- if (curr==NULL )
39
- {
40
- cout<<endl;
41
- q.push (NULL );
42
- continue ;
43
- }
44
-
45
36
cout<<curr->data <<" " ;
46
37
47
38
if (curr->left )
@@ -70,6 +61,7 @@ int main()
70
61
root->right ->right =new Node (70 );
71
62
72
63
levelOrder (root);
73
-
64
+ cout<<endl;
65
+
74
66
return 0 ;
75
67
}
Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ void levelOrder(Node* root)
55
55
for (int i=0 ;i<h;i++)
56
56
{
57
57
nodesAtKDistance (root,i);
58
+ cout<<endl;
58
59
}
59
60
}
60
61
@@ -72,6 +73,5 @@ int main()
72
73
root->right ->right =new Node (70 );
73
74
74
75
levelOrder (root);
75
-
76
76
return 0 ;
77
77
}
Original file line number Diff line number Diff line change @@ -17,7 +17,8 @@ class Node
17
17
};
18
18
19
19
// Time: O(N)
20
- // Space: O(W)
20
+ // Space: O(N)
21
+ // when complete level is traversed,push NULL into queue
21
22
void levelOrder (Node* root)
22
23
{
23
24
if (root==NULL )
@@ -27,12 +28,20 @@ void levelOrder(Node* root)
27
28
28
29
queue<Node*> q;
29
30
q.push (root);
31
+ q.push (NULL );
30
32
31
- while (q.empty ()== false )
33
+ while (q.size ()> 1 )
32
34
{
33
35
Node* curr=q.front ();
34
36
q.pop ();
35
37
38
+ if (curr==NULL )
39
+ {
40
+ cout<<endl;
41
+ q.push (NULL );
42
+ continue ;
43
+ }
44
+
36
45
cout<<curr->data <<" " ;
37
46
38
47
if (curr->left )
@@ -61,6 +70,7 @@ int main()
61
70
root->right ->right =new Node (70 );
62
71
63
72
levelOrder (root);
64
-
73
+ cout<<endl;
74
+
65
75
return 0 ;
66
76
}
File renamed without changes.
Original file line number Diff line number Diff line change @@ -18,14 +18,14 @@ class Node
18
18
19
19
// Time: O(N)
20
20
// Space: O(W)
21
- int maximum (Node* root)
21
+ int size (Node* root)
22
22
{
23
23
if (root==NULL )
24
24
{
25
25
return 0 ;
26
26
}
27
27
28
- int max_val=INT_MIN ;
28
+ int count= 0 ;
29
29
30
30
queue<Node*> q;
31
31
q.push (root);
@@ -35,10 +35,7 @@ int maximum(Node* root)
35
35
Node* curr=q.front ();
36
36
q.pop ();
37
37
38
- if (curr)
39
- {
40
- max_val=max (max_val,curr->data );
41
- }
38
+ count++;
42
39
43
40
if (curr->left )
44
41
{
@@ -51,7 +48,7 @@ int maximum(Node* root)
51
48
}
52
49
}
53
50
54
- return max_val ;
51
+ return count ;
55
52
}
56
53
57
54
int main ()
@@ -67,7 +64,6 @@ int main()
67
64
root->right ->left =new Node (60 );
68
65
root->right ->right =new Node (70 );
69
66
70
- cout<<maximum (root)<<endl;
71
-
67
+ cout<<size (root)<<endl;
72
68
return 0 ;
73
69
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ class Node
17
17
};
18
18
19
19
// Time: O(N)
20
- // Space: O(logN );
20
+ // Space: O(H );
21
21
int size (Node* root)
22
22
{
23
23
if (root==NULL )
@@ -42,6 +42,5 @@ int main()
42
42
root->right ->right =new Node (70 );
43
43
44
44
cout<<size (root)<<endl;
45
-
46
45
return 0 ;
47
46
}
You can’t perform that action at this time.
0 commit comments