Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tress C++ #1741

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Trees/BST implementation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<iostream>
#include<string>
using namespace std;

struct BSTNode
{
int data;
BSTNode *left;
BSTNode *right;

};

struct BSTNode *GetNewNode(int data)
{
struct BSTNode *NewNode = new BSTNode();
NewNode->data=data;
NewNode->left = NULL ;
NewNode->right = NULL;

return NewNode;
}


struct BSTNode *Insert(BSTNode* root, int data)
{
if(root==NULL)
root= GetNewNode(data);

else if(data <= root->data)
root->left = Insert(root->left , data);

else
root->right = Insert(root->right , data);

return root;
}




int main()
{
struct BSTNode *rootPtr = NULL;

rootPtr = Insert(rootPtr , 5);
rootPtr = Insert(rootPtr , 7);
rootPtr = Insert(rootPtr , 4);
rootPtr = Insert(rootPtr , 15);
rootPtr = Insert(rootPtr , 17);
rootPtr = Insert(rootPtr , 14);
rootPtr = Insert(rootPtr , 11);


return 0;
}


104 changes: 104 additions & 0 deletions Trees/Checking is tree BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct BSTNode
{
int data;
BSTNode *left;
BSTNode *right;

};

struct BSTNode *GetNewNode(int data)
{
struct BSTNode *NewNode = new BSTNode();
NewNode->data=data;
NewNode->left = NULL ;
NewNode->right = NULL;

return NewNode;
}

struct BSTNode *Insert(BSTNode* root, int data)
{
if(root==NULL)
root= GetNewNode(data);

else if(data <= root->data)
root->left = Insert(root->left , data);

else
root->right = Insert(root->right , data);

return root;
}

bool IsSubtreeLesser(BSTNode *root , int d)
{
if(root==NULL)
return 1;

else if(root->data<=d && IsSubtreeLesser(root->left , d) &&IsSubtreeLesser(root->right , d))
return 1;

else
return 0;

}

bool IsSubtreeGreater(BSTNode *root , int d)
{
if(root==NULL)
return 1;

else if(root->data >d && IsSubtreeGreater(root->left , d) &&IsSubtreeGreater(root->right , d))
return 1;

else
return 0;

}


bool IsBST(BSTNode *root)
{
if(root==NULL)
return 1;

else if(IsSubtreeLesser(root->left , root->data) && IsSubtreeGreater(root->right,root->data) && IsBST(root->left) && IsBST(root->right))
return 1;

else
return 0;



}


int main()
{
struct BSTNode *rootPtr = NULL;

rootPtr = Insert(rootPtr , 5);
rootPtr = Insert(rootPtr , 7);
rootPtr = Insert(rootPtr , 4);
rootPtr = Insert(rootPtr , 15);
rootPtr = Insert(rootPtr , 17);
rootPtr = Insert(rootPtr , 14);
rootPtr = Insert(rootPtr , 11);

if(IsBST(rootPtr))
cout<<"Tree is BST.";
else
cout<<"Tree is not BST.";




return 0;
}


133 changes: 133 additions & 0 deletions Trees/Deleting nodes from BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct BSTNode
{
int data;
BSTNode *left;
BSTNode *right;

};

struct BSTNode *GetNewNode(int data)
{
struct BSTNode *NewNode = new BSTNode();
NewNode->data=data;
NewNode->left = NULL ;
NewNode->right = NULL;

return NewNode;
}

struct BSTNode *Insert(BSTNode* root, int data)
{
if(root==NULL)
root= GetNewNode(data);

else if(data <= root->data)
root->left = Insert(root->left , data);

else
root->right = Insert(root->right , data);

return root;
}

BSTNode *FindMin(BSTNode *root)
{
if(root==NULL)
return NULL;

if(root->left==NULL)
return root;

while(root->left!=NULL)
{
root=root->left;
}

return root;

}



struct BSTNode* Delete(BSTNode *root , int data)
{
if(root==NULL)
return root;

else if(root->data < data)
root->right=Delete(root->right,data);

else if(root->data > data)
root->left=Delete(root->left , data);

else // if data matches
{
// case 1 : no child

if(root->left==NULL && root->right==NULL )
{
delete root;
root = NULL;
return root;
}

// case 2: 1 child
else if(root->left == NULL)
{
BSTNode *temp =root;
root = root->right;
delete temp;
return root;

}
else if (root->right == NULL)
{
BSTNode *temp =root;
root = root->left;
delete temp;
return root;

}

//case 3 : two children
else
{
BSTNode *temp = FindMin(root->right);
root->data= temp->data;
root->right= Delete(root->right , temp->data);

}


}

return root;
}


int main()
{
struct BSTNode *rootPtr = NULL;

rootPtr = Insert(rootPtr , 5);
rootPtr = Insert(rootPtr , 7);
rootPtr = Insert(rootPtr , 4);
rootPtr = Insert(rootPtr , 15);
rootPtr = Insert(rootPtr , 17);
rootPtr = Insert(rootPtr , 14);
rootPtr = Insert(rootPtr , 11);


rootPtr=Delete(rootPtr,15);



return 0;
}


90 changes: 90 additions & 0 deletions Trees/Finding minimum and maximum in BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include<iostream>
#include<string>
using namespace std;

struct BSTNode
{
int data;
BSTNode *left;
BSTNode *right;

};

struct BSTNode *GetNewNode(int data)
{
struct BSTNode *NewNode = new BSTNode();
NewNode->data=data;
NewNode->left = NULL ;
NewNode->right = NULL;

return NewNode;
}


struct BSTNode *Insert(BSTNode* root, int data)
{
if(root==NULL)
root= GetNewNode(data);

else if(data <= root->data)
root->left = Insert(root->left , data);

else
root->right = Insert(root->right , data);

return root;
}



int Min(BSTNode *root)
{
if(root==NULL)
return -1;

else if(root->left == NULL)
return root->data;

else
return Min(root->left);

}

int Max(BSTNode *root)
{
if(root==NULL)
return -1;

else if(root->right == NULL)
return root->data;

else
return Max(root->right);

}


int main()
{
struct BSTNode *rootPtr = NULL;

rootPtr = Insert(rootPtr , 5);
rootPtr = Insert(rootPtr , 7);
rootPtr = Insert(rootPtr , 4);
rootPtr = Insert(rootPtr , 15);
rootPtr = Insert(rootPtr , 17);
rootPtr = Insert(rootPtr , 14);
rootPtr = Insert(rootPtr , 11);

cout<<"Minimum value = "<<Min(rootPtr)<<endl;
cout<<"Maximum value = "<<Max(rootPtr)<<endl;






return 0;
}


Loading