Skip to content

Commit c480fbc

Browse files
author
Prateek Narang
committed
added Generic ,Binary Trees and BST's
1 parent 39b1599 commit c480fbc

File tree

44 files changed

+2023
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2023
-0
lines changed

Trees/10. Print Mirror of Tree

34.6 KB
Binary file not shown.

Trees/10. Print Mirror of Tree.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
5+
struct node{
6+
int data;
7+
struct node*left;
8+
struct node*right;
9+
};
10+
11+
void printLevelOrder(node*root){
12+
13+
queue<node*> q;
14+
q.push(root);
15+
q.push(NULL);
16+
17+
while(!q.empty()){
18+
node*temp = q.front();
19+
q.pop();
20+
21+
if(q.empty())
22+
{
23+
cout<<endl;
24+
break;
25+
}
26+
27+
else if(temp==NULL)
28+
{
29+
q.push(NULL);
30+
cout<<endl;
31+
}
32+
else{
33+
cout<<temp->data<<" ";
34+
if(temp->left!=NULL){
35+
q.push(temp->left);
36+
}
37+
if(temp->right!=NULL){
38+
q.push(temp->right);
39+
}
40+
}
41+
}
42+
}
43+
44+
45+
//-------------------------------------------------------------Create BST
46+
struct node*createBST(int *a,int low,int high){
47+
48+
49+
if(low<=high){
50+
struct node*root = new node;
51+
int mid = (low+high)/2;
52+
root->data = a[mid];
53+
root->left = createBST(a,low,mid-1);
54+
root->right = createBST(a,mid+1,high);
55+
56+
return root;
57+
}
58+
else
59+
return NULL;
60+
}
61+
//-------------------------------------------------------PRINT MIRROR OF TREE
62+
struct node* createMirror(struct node*root){
63+
if(root==NULL){
64+
return NULL;
65+
}
66+
struct node*temp = createMirror(root->left);
67+
root->left = createMirror(root->right);
68+
root->right = temp;
69+
70+
return root;
71+
}
72+
73+
int main(){
74+
int a[8]={1,2,3,4,5,6,7,8};
75+
struct node*root = NULL;
76+
root = createBST(a,0,7);
77+
printLevelOrder(root);
78+
createMirror(root);
79+
cout<<"Mirror : "<<endl;
80+
printLevelOrder(root);
81+
return 0;
82+
}

Trees/10. Print Mirror of Tree.o

60.9 KB
Binary file not shown.
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
5+
struct node{
6+
int data;
7+
struct node*left;
8+
struct node*right;
9+
};
10+
11+
void printLevelOrder(node*root){
12+
13+
queue<node*> q;
14+
q.push(root);
15+
q.push(NULL);
16+
17+
while(!q.empty()){
18+
node*temp = q.front();
19+
q.pop();
20+
21+
if(q.empty())
22+
{
23+
cout<<endl;
24+
break;
25+
}
26+
27+
else if(temp==NULL)
28+
{
29+
q.push(NULL);
30+
cout<<endl;
31+
}
32+
else{
33+
cout<<temp->data<<" ";
34+
if(temp->left!=NULL){
35+
q.push(temp->left);
36+
}
37+
if(temp->right!=NULL){
38+
q.push(temp->right);
39+
}
40+
}
41+
}
42+
}
43+
44+
45+
//-------------------------------------------------------------Create BST
46+
struct node*createBST(int *a,int low,int high){
47+
48+
49+
if(low<=high){
50+
struct node*root = new node;
51+
int mid = (low+high)/2;
52+
root->data = a[mid];
53+
root->left = createBST(a,low,mid-1);
54+
root->right = createBST(a,mid+1,high);
55+
56+
return root;
57+
}
58+
else
59+
return NULL;
60+
}
61+
//-------------------------------------------------------PRINT MIRROR OF TREE
62+
bool areIdentical(struct node*root1,struct node*root2){
63+
if(root==NULL){
64+
return true;
65+
}
66+
67+
68+
return root;
69+
}
70+
71+
int main(){
72+
int a[8]={1,2,3,4,5,6,7,8};
73+
struct node*root = NULL;
74+
root = createBST(a,0,7);
75+
76+
struct node*root2 = createBST()
77+
78+
printLevelOrder(root);
79+
80+
if(areIdentical(root1,root2))
81+
cout<<"Identical Trees "<<endl;
82+
else
83+
cout<<"Not Identical "<<endl;
84+
85+
return 0;
86+
}

Trees/12. BST Insertion

34.6 KB
Binary file not shown.

Trees/12. BST Insertion.cpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
5+
struct node{
6+
int data;
7+
struct node*left;
8+
struct node*right;
9+
};
10+
11+
void printLevelOrder(node*root){
12+
13+
queue<node*> q;
14+
q.push(root);
15+
q.push(NULL);
16+
17+
while(!q.empty()){
18+
node*temp = q.front();
19+
q.pop();
20+
21+
if(q.empty())
22+
{
23+
cout<<endl;
24+
break;
25+
}
26+
27+
else if(temp==NULL)
28+
{
29+
q.push(NULL);
30+
cout<<endl;
31+
}
32+
else{
33+
cout<<temp->data<<" ";
34+
if(temp->left!=NULL){
35+
q.push(temp->left);
36+
}
37+
if(temp->right!=NULL){
38+
q.push(temp->right);
39+
}
40+
}
41+
}
42+
}
43+
44+
//----------------------------------------------------Create from Array
45+
struct node*createBST(int *a,int low,int high){
46+
47+
if(low==high)
48+
{
49+
struct node*leaf = new node;
50+
leaf->data = a[low];
51+
leaf->left = NULL;
52+
leaf->right = NULL;
53+
return leaf;
54+
}
55+
else if(low<high){
56+
struct node*root = new node;
57+
int mid = (low+high)/2;
58+
root->data = a[mid];
59+
root->left = createBST(a,low,mid-1);
60+
root->right = createBST(a,mid+1,high);
61+
62+
return root;
63+
}
64+
return NULL;
65+
}
66+
67+
void printPreorder(struct node*root){
68+
if(root==NULL)
69+
return ;
70+
71+
printPreorder(root->left);
72+
cout<<root->data<<" ";
73+
printPreorder(root->right);
74+
}
75+
76+
//----------------------------------------------Insert a node
77+
struct node* insert(struct node*root,int data){
78+
if(root==NULL){
79+
struct node*insertedNode = new node;
80+
insertedNode->data = data;
81+
insertedNode->left = insertedNode->right = NULL;
82+
return insertedNode;
83+
}
84+
if(data< root->data)
85+
root->left = insert(root->left,data);
86+
else
87+
root->right = insert(root->right,data);
88+
return root;
89+
}
90+
91+
92+
int main(){
93+
int a[8]={1,2,3,4,5,6,7,9};
94+
struct node*root = NULL;
95+
root = createBST(a,0,7);
96+
cout<<"Originial "<<endl;
97+
printLevelOrder(root);
98+
insert(root,8);
99+
cout<<endl<<"After inserting 8 "<<endl;
100+
101+
printLevelOrder(root);
102+
cout<<endl<<"Inorder : "<<endl;
103+
printPreorder(root);
104+
return 0;
105+
}

Trees/12. BST Insertion.o

61.9 KB
Binary file not shown.

Trees/13. BST Deletion

34.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)