Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define None 32767
- #define True 1
- #define False 0
- typedef struct BSTNode
- {
- int data;
- struct BSTNode *left;
- struct BSTNode *right;
- } BSTNode;
- BSTNode *root = NULL;
- BSTNode* init(int val)
- {
- //initializer for the BST node
- BSTNode *temp;
- temp = (BSTNode*) malloc (sizeof(BSTNode));
- temp -> data = val;
- temp -> right = NULL;
- temp -> left = NULL;
- return temp;
- }
- void insert_node(int val)
- {
- //inserts the node with value val in the right place in the tree
- BSTNode *new_tnode;
- BSTNode *temp;
- new_tnode = init(val);
- if (root == NULL) //the tree is empty
- {
- root = new_tnode;
- return;
- }
- temp = root;
- while (1) //we need to descend to the second last level
- {
- if (val < temp -> data) //left subtree
- {
- if (temp -> left == NULL)
- {
- temp -> left = new_tnode;
- break;
- }
- else
- temp = temp -> left;
- }
- else
- {
- if (temp -> right == NULL)
- {
- temp -> right = new_tnode;
- break;
- }
- else
- temp = temp -> right;
- }
- }
- }
- int search(int val)
- {
- //searches the BST for the node valued val
- BSTNode *temp;
- temp = root;
- while (temp != NULL)
- {
- if (val > temp -> data)
- temp = temp -> right;
- else if (val < temp -> data)
- temp = temp -> left;
- else
- return True;
- }
- return False;
- }
- BSTNode* get_node(int val)
- {
- //returns the node in the tree having the value val
- BSTNode *temp;
- temp = root;
- if (!search(val))
- {
- printf("\nThe node doesnt exist in the BST");
- return NULL;
- }
- while (temp != NULL)
- {
- if (val > temp -> data)
- temp = temp -> right;
- else if (val < temp -> data)
- temp = temp -> left;
- else if (val == temp -> data)
- return temp;
- }
- }
- BSTNode* min_finder(BSTNode *tnode)
- {
- //returns the node with the minimum value of the subtree rooted at tnode
- if (tnode -> left != NULL) //if you can go further left, go left!
- min_finder(tnode -> left);
- else //return the leaf
- return tnode;
- }
- BSTNode* max_finder(BSTNode *tnode)
- {
- //returns the node with the minimum value of the subtree rooted at tnode
- if (tnode -> right != NULL) //if you can go further right, go right
- max_finder(tnode -> right);
- else
- return tnode;
- }
- BSTNode *get_inorder_predecessor(BSTNode *tnode)
- {
- //returns the inorder predecessor of the tnode
- //go left once, find the max of that subtree
- if (tnode -> left == NULL)
- return NULL;
- else
- return max_finder(tnode -> left);
- }
- BSTNode *get_inorder_successor(BSTNode *tnode)
- {
- //returns the inorder successor of the tnode
- //go right once, find the min of that subtree
- if (tnode -> right == NULL)
- return NULL;
- else
- return min_finder(tnode -> right);
- }
- void delete_leaf(BSTNode *tnode)
- {
- //deletes the leaf node tnode from the BST - works
- BSTNode *ptr, *pre_ptr;
- if (tnode -> data == root -> data)
- {
- ptr = root;
- root = NULL;
- free(ptr);
- }
- else
- {
- ptr = root;
- pre_ptr = root; //pre_ptr is the parent of the root
- while (ptr -> data != tnode -> data)
- {
- pre_ptr = ptr;
- //printf("\ntnode -> data = %d, ptr -> data =%d\n", tnode -> data, ptr -> data);
- if (ptr -> data > tnode -> data)
- ptr = ptr -> left;
- else
- ptr = ptr -> right;
- }
- if (tnode -> data > pre_ptr -> data)
- pre_ptr -> right = NULL;
- else
- pre_ptr -> left = NULL;
- free(ptr);
- }
- }
- void delete_right(BSTNode *tnode)
- {
- //deletes the node tnode which has no left child
- BSTNode *ptr, *pre_ptr;
- if (tnode -> data == root -> data)
- {
- ptr = root;
- root = root -> right;
- free(ptr);
- }
- else
- {
- ptr = root;
- pre_ptr = root;
- while (ptr -> data != tnode -> data)
- {
- pre_ptr = ptr;
- if (ptr -> data > tnode -> data)
- ptr = ptr -> left;
- else
- ptr = ptr -> right;
- }
- //pre_ptr is now the parent of tnode
- if (ptr -> data > pre_ptr -> data)
- pre_ptr -> right = ptr -> right;
- else
- pre_ptr -> left = ptr -> right;
- ptr -> left = NULL;
- //ptr -> right = NULL;
- free(ptr);
- }
- }
- void delete_left(BSTNode *tnode)
- {
- //deletes the node tnode which has no right child
- BSTNode *ptr, *pre_ptr;
- if (tnode -> data == root -> data)
- {
- ptr = root;
- root = root -> left;
- free(ptr);
- }
- else
- {
- ptr = root;
- pre_ptr = root;
- while (ptr -> data != tnode -> data)
- {
- pre_ptr = ptr;
- if (ptr -> data > tnode -> data)
- ptr = ptr -> left;
- else
- ptr = ptr -> right;
- }
- //pre_ptr is now the parent of tnode
- if (ptr -> data > pre_ptr -> data)
- pre_ptr -> right = ptr -> left;
- else
- pre_ptr -> left = ptr -> left;
- //ptr -> left = NULL;
- ptr -> right = NULL;
- free(ptr);
- }
- }
- void super_delete(BSTNode *tnode)
- {
- //deletes the node tnode with two children
- BSTNode *replacement_node;
- int data;
- replacement_node = get_inorder_successor(tnode);
- data = replacement_node -> data;
- delete_left(replacement_node); tnode -> data = data;
- }
- void master_delete(int val)
- {
- //deletes the node with value val in the BST
- //has 3 sub routines - 1. delete leaf, 2. delete nodes having one child, 3. delete nodes with two children
- BSTNode *temp;
- temp = get_node(val);
- if (temp == NULL)
- printf("\nUnable to delete %d from the BST\n", val);
- else if ((temp -> left == NULL) && (temp -> right == NULL))
- {
- if (temp -> data == root -> data)
- {
- delete_leaf(temp);
- root = NULL;
- }
- else
- delete_leaf(temp);
- }
- else if ((temp -> left) == NULL)
- delete_right(temp);
- else if ((temp -> right == NULL))
- delete_left(temp);
- else
- super_delete(temp);
- }
- void inorder(BSTNode *temp)
- {
- if (temp != NULL)
- {
- inorder(temp -> left);
- printf("%d ", temp -> data);
- inorder(temp -> right);
- }
- else
- return;
- }
- void preorder(BSTNode *temp)
- {
- if (temp != NULL)
- {
- printf("%d ", temp -> data);
- preorder(temp -> left);
- preorder(temp -> right);
- }
- else
- return;
- }
- void postorder(BSTNode *temp)
- {
- if (temp != NULL)
- {
- postorder(temp -> left);
- postorder(temp -> right);
- printf("%d ", temp -> data);
- }
- else
- return;
- }
- int main()
- {
- int choice, val;
- printf("\nBST operations!!");
- while (1)
- {
- printf("\n1. insert\n2. delete\n3. print\n4. quit: ");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- //insert
- printf("\nEnter value of the node: ");
- scanf("%d", &val);
- printf("\n");
- insert_node(val);
- break;
- case 2:
- //delete
- if (root == NULL)
- printf("\nEmpty BST!\n");
- else
- {
- printf("\nEnter node value to delete: ");
- scanf("%d", &val);
- master_delete(val);
- }
- printf("\n");
- break;
- case 3:
- //display
- printf("\nPreorder traversal: ");
- preorder(root);
- printf("\n");
- printf("\nInorder traversal: ");
- inorder(root);
- printf("\n");
- printf("\nPostorder traversal: ");
- postorder(root);
- printf("\n");
- break;
- default:
- exit(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement