Skip to content

Commit 07255ea

Browse files
authored
Merge pull request #105 from nb9960/master
added Data-Structures in Cpp
2 parents 69993c5 + 0bc22ae commit 07255ea

20 files changed

+1433
-0
lines changed
19.6 KB
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class node
5+
{
6+
public:
7+
int data;
8+
node* left, *right;
9+
};
10+
11+
void printPreorder(node *root){
12+
if(root==NULL)
13+
return;
14+
cout<<root->data<<" ";
15+
printPreorder(root->left);
16+
printPreorder(root->right);
17+
}
18+
19+
void printInorder(node *root){
20+
if(root==NULL)
21+
return;
22+
printInorder(root->left);
23+
cout<<root->data<<" ";
24+
printInorder(root->right);
25+
}
26+
27+
void printPostorder(node *root){
28+
if(root==NULL)
29+
return;
30+
printPostorder(root->left);
31+
printPostorder(root->right);
32+
cout<<root->data<<" ";
33+
}
34+
35+
node* newNode(int data){
36+
node* Node=new node();
37+
Node->data=data;
38+
Node->left=NULL;
39+
Node->right=NULL;
40+
return Node;
41+
}
42+
43+
int main(){
44+
node* root=newNode(1);
45+
root->left=newNode(2);
46+
root->right=newNode(3);
47+
root->left->left=newNode(4);
48+
root->left->right=newNode(5);
49+
50+
// root left right
51+
cout<<"PreOrder: ";
52+
printPreorder(root);
53+
cout<<"\n";
54+
55+
// left root right
56+
cout<<"InOrder: ";
57+
printInorder(root);
58+
cout<<"\n";
59+
60+
// left right root
61+
cout<<"PostOrder: ";
62+
printPostorder(root);
63+
cout<<"\n";
64+
return 0;
65+
}
66+
67+
/*
68+
1
69+
/ \
70+
2 3
71+
/ \
72+
4 5
73+
Time complexity: worst case: O(n^2)
74+
Space complexity: O(n) for worst case for skewed tree
75+
For balanced tree O(logn)
76+
*/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class node
5+
{
6+
public:
7+
int data;
8+
node* left, *right;
9+
};
10+
11+
void printGivenLevel(node* root, int level){
12+
if(root==NULL)
13+
return;
14+
if(level==1)
15+
cout<<root->data<<" ";
16+
else if(level>1){
17+
printGivenLevel(root->left,level-1);
18+
printGivenLevel(root->right,level-1);
19+
}
20+
}
21+
22+
int height(node* node){
23+
if(node==NULL)
24+
return 0;
25+
else{
26+
int lheight=height(node->left);
27+
int rheight=height(node->right);
28+
if(lheight>rheight)
29+
return (lheight+1);
30+
else return (rheight+1);
31+
}
32+
}
33+
34+
node* newNode(int data){
35+
node* Node=new node();
36+
Node->data=data;
37+
Node->left=NULL;
38+
Node->right=NULL;
39+
return Node;
40+
}
41+
42+
void printLevelOrder(node *root){
43+
int h=height(root);
44+
int i;
45+
for(i=1;i<=h;i++){
46+
printGivenLevel(root,i);
47+
}
48+
}
49+
50+
int main(){
51+
node* root=newNode(1);
52+
root->left=newNode(2);
53+
root->right=newNode(3);
54+
root->left->left=newNode(4);
55+
root->left->right=newNode(5);
56+
57+
printLevelOrder(root);
58+
return 0;
59+
}
60+
61+
/*
62+
1
63+
/ \
64+
2 3
65+
/ \
66+
4 5
67+
Time complexity: worst case: O(n^2)
68+
Space complexity: O(n) for worst case for skewed tree
69+
For balanced tree O(logn)
70+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class node
5+
{
6+
public:
7+
int data;
8+
node* left, *right;
9+
};
10+
11+
node* newNode(int data){
12+
node* Node=new node();
13+
Node->data=data;
14+
Node->left=NULL;
15+
Node->right=NULL;
16+
return Node;
17+
}
18+
19+
void printLevelOrder(node *root){
20+
if(root==NULL)
21+
return;
22+
queue<node *> q;
23+
q.push(root);
24+
while(q.empty()==false){
25+
node *Node=q.front();
26+
cout<<Node->data<<" ";
27+
q.pop();
28+
if(Node->left!=NULL)
29+
q.push(Node->left);
30+
if(Node->right!=NULL)
31+
q.push(Node->right);
32+
}
33+
34+
}
35+
36+
int main(){
37+
node* root=newNode(1);
38+
root->left=newNode(2);
39+
root->right=newNode(3);
40+
root->left->left=newNode(4);
41+
root->left->right=newNode(5);
42+
43+
printLevelOrder(root);
44+
return 0;
45+
}
46+
47+
/*
48+
1
49+
/ \
50+
2 3
51+
/ \
52+
4 5
53+
Time complexity: O(n)
54+
Space complexity: O(n)
55+
*/
19.9 KB
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class node
5+
{
6+
public:
7+
int data;
8+
node* left, *right;
9+
};
10+
11+
bool sameTree(node *tree1,node* tree2){
12+
if(tree1==NULL && tree2==NULL){
13+
return 1;
14+
}
15+
if(tree1!=NULL && tree2!=NULL){
16+
return(tree1->data==tree2->data && sameTree(tree1->left, tree2->left) && sameTree(tree1->right,tree2->right));
17+
}
18+
return 0;
19+
}
20+
21+
node* newNode(int data){
22+
node* Node=new node();
23+
Node->data=data;
24+
Node->left=NULL;
25+
Node->right=NULL;
26+
return Node;
27+
}
28+
29+
int main(){
30+
node* root1=newNode(1);
31+
root1->left=newNode(2);
32+
root1->right=newNode(3);
33+
root1->left->left=newNode(4);
34+
root1->left->right=newNode(5);
35+
36+
node* root2=newNode(1);
37+
root2->left=newNode(2);
38+
root2->right=newNode(5);
39+
root2->left->left=newNode(4);
40+
root2->left->right=newNode(5);
41+
42+
if(sameTree(root1,root2)==0){
43+
cout<<"NOT IDENTICAL"<<endl;
44+
}else{
45+
cout<<"Identical"<<endl;
46+
}
47+
return 0;
48+
}
49+
50+
/*
51+
1
52+
/ \
53+
2 3
54+
/ \
55+
4 5
56+
Time complexity: worst case: O(n^2)
57+
Space complexity: O(n) for worst case for skewed tree
58+
For balanced tree O(logn)
59+
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Author:Nishtha
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
class node
6+
{
7+
public:
8+
int key_value;
9+
node* left, *right;
10+
};
11+
12+
// node* newNode(int data){
13+
// node* Node=new node();
14+
// Node->data=data;
15+
// Node->left=NULL;
16+
// Node->right=NULL;
17+
// return Node;
18+
// }
19+
20+
void insert(int key, node *leaf)
21+
{
22+
if(key< leaf->key_value)
23+
{
24+
if(leaf->left!=NULL)
25+
insert(key, leaf->left);
26+
else
27+
{
28+
leaf->left=new node;
29+
leaf->left->key_value=key;
30+
leaf->left->left=NULL; //Sets the left child of the child node to null
31+
leaf->left->right=NULL; //Sets the right child of the child node to null
32+
}
33+
}
34+
else if(key>=leaf->key_value)
35+
{
36+
if(leaf->right!=NULL)
37+
insert(key, leaf->right);
38+
else
39+
{
40+
leaf->right=new node;
41+
leaf->right->key_value=key;
42+
leaf->right->left=NULL; //Sets the left child of the child node to null
43+
leaf->right->right=NULL; //Sets the right child of the child node to null
44+
}
45+
}
46+
}
47+
48+
// Drive Code
49+
int main()
50+
{
51+
cout<<"Enter the no. of numbbers to be entered:"<<endl;
52+
int n;cin>>n;
53+
cout<<"Enter numbers:";
54+
node* root=NULL;
55+
for(int i=0;i<n;i++){
56+
int x;cin>>x;
57+
if(root!=NULL)
58+
insert(x, root);
59+
else
60+
{
61+
root=new node;
62+
root->key_value=x;
63+
root->left=NULL;
64+
root->right=NULL;
65+
}
66+
}
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)