I had an assignment that I completed and it was just inserting values into a binary tree and then displaying it. I was wondering what the code would be if I wanted to delete a number in the binary tree, or if I wanted to search for a number. Here is my code for my assignment if you can work off that. Thanks a lot in advance.
Code:
#include <stdio.h>
#include<stdlib.h>
typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;
}BST;
BST* insert(BST* root, int value);
void printTree(BST* root);
void displayBST(BST* root, int depth);
void padding(char toPrint, int numTimes);
int main(){
int value = 0;
BST *new_BST;
while(value != -1){
printf("\nEnter a number to put into the tree: ");
scanf("%d", &value);
new_BST = insert(new_BST, value);
}
printTree(new_BST);
}
BST* insert(BST* root, int value){
if(root != 0){
if(value < root->value)
root->left = insert(root->left, value);
else if( value > root->value)
root->right = insert(root->right, value);
return root;
}
BST *n = (BST*) malloc(sizeof(BST));
n->value = value;
n->left = n->right = 0;
return n;
}
void printTree(BST* root)
{
displayBST(root,0);
}
void displayBST(BST* root, int depth){
if(root==NULL)
{
padding(' ', depth);
printf("-\n");
return;
}
displayBST(root->left,depth+4);
padding(' ', depth);
printf("%d\n", root->value);
displayBST(root->right, depth+4);
}
void padding(char toPrint, int numTimes)
{
int i;
for(i=0; i < numTimes; i++)
printf("%c", toPrint);
}