Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int payload;
- struct node *left, *right;
- };
- struct node* createNode(int payload)
- {
- struct node* returnMe = malloc(sizeof(struct node));
- returnMe->payload = payload;
- returnMe->left = NULL;
- returnMe->right = NULL;
- return returnMe;
- };
- void insert(struct node* currentNode, int value)
- {
- if(value > currentNode->payload)
- {
- if (currentNode->right == NULL)
- {
- currentNode->right = createNode(value);
- return;
- }
- insert(currentNode->right, value);
- }
- else
- {
- if (currentNode->left == NULL)
- {
- currentNode->left = createNode(value);
- return;
- }
- insert(currentNode->left, value);
- }
- }
- void inOrder(struct node* currentNode)
- {
- if (currentNode == NULL) return;
- inOrder(currentNode->left);
- printf("%d, ", currentNode->payload);
- inOrder(currentNode->right);
- }
- int delete(struct node* currentNode, int value)
- {
- if (currentNode->payload == value);
- else if(value > currentNode->payload)
- {
- if (currentNode->right == NULL)
- return 0;
- delete(currentNode->right, value);
- }
- else
- {
- if (currentNode->left == NULL)
- return 0;
- delete(currentNode->left, value);
- }
- return 0;
- }
- int main()
- {
- struct node *root = createNode(5);
- insert(root, 11);
- insert(root, 32);
- insert(root, 4);
- insert(root, 5);
- insert(root, 71);
- insert(root, 1);
- inOrder(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment