Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node Node;
- struct node{
- int data;
- Node *left,*right;
- };
- Node* getmin(Node *root)
- {
- Node *ptr = root;
- while(ptr && ptr->left!=NULL)
- {
- getmin(ptr->left);
- }
- return ptr;
- }
- Node* deletenode(Node *root,int data)
- {
- if(root ==NULL)
- return root;
- if(root->data>data)
- root->left = deletenode(root->left,data);
- else if(root->data<data)
- root->right = deletenode(root->right,data);
- else
- {
- if(root->left == NULL)
- {
- Node *temp = root->right;
- free(root);
- return temp;
- }
- else if(root->right == NULL)
- {
- Node *temp = root->left;
- free(root);
- return temp;
- }
- Node *temp = getmin(root->right);
- root->data = temp->data;
- root->right = deletenode(root->right,temp->data);
- }
- }
- Node* in(Node *root,int data)
- {
- int x = 0;
- if(root == NULL)
- {
- Node *newnode = (Node*)malloc(sizeof(Node));
- newnode->data = data;
- newnode->left = NULL;
- newnode->right = NULL;
- root = newnode;
- x = 1;
- }
- if(root->data==data && x == 0)
- {
- printf("%d found at address %x.",data,root);
- }
- else if(root->data==data && x != 0)
- {
- printf("%d not found. It has been inserted in the tree at address %x.",data,root);
- }
- else if(data>root->data)
- root->right = in(root->right,data);
- else if(data<root->data)
- root->left = in(root->left,data);
- return root;
- }
- Node* insert(Node *root, int data)
- {
- if(root == NULL)
- {
- Node *newnode = (Node*)malloc(sizeof(Node));
- newnode->data = data;
- newnode->left = NULL;
- newnode->right = NULL;
- root = newnode;
- }
- else if(data>=root->data)
- root->right = insert(root->right,data);
- else if(data<root->data)
- root->left = insert(root->left,data);
- return root;
- }
- void preOrder(Node *root){
- if(root==NULL)
- return ;
- printf("%d ",root->data);
- preOrder(root->left);
- preOrder(root->right);
- }
- void inOrder(Node *root){
- if(root == NULL)
- return ;
- inOrder(root->left);
- printf("%d ",root->data);
- inOrder(root->right);
- }
- void postOrder(Node *root){
- if(root == NULL)
- return ;
- postOrder(root->left);
- postOrder(root->right);
- printf("%d ",root->data);
- }
- void nodeswithtwochild(Node *root){
- if(root == NULL)
- return ;
- if(root->left!=NULL && root->right!=NULL)
- printf("%d ",root->data);
- nodeswithtwochild(root->left);
- nodeswithtwochild(root->right);
- }
- int main(){
- int n,data;
- Node *root = NULL;
- printf("Enter number of nodes : ");
- scanf("%d",&n);
- printf("Enter %d data : ",n);
- while(n--){
- scanf("%d",&data);
- root = insert(root,data);
- }
- printf("PreOrder traversal : ");
- preOrder(root);
- printf("\nInOrder traversal : ");
- inOrder(root);
- printf("\nPostOrder traversal : ");
- postOrder(root);
- printf("\nNodes with two child : ");
- nodeswithtwochild(root);
- printf("\nEnter a data to search : ");
- scanf("%d",&data);
- root = in(root,data);
- preOrder(root);
- printf("\nEnter a data to delete : ");
- scanf("%d",&data);
- root = deletenode(root,data);
- preOrder(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement