![]() |
| | #1 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 302
| parent in a binary search tree [insert] Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
void insert(struct node **root, int data);
void search(struct node** root, int data);
struct node * searchloc(struct node **root, int data);
void delete(struct node **root, int data);
struct node * searchparent(struct node **root, struct node *temp);
struct node * inorder(struct node *root, struct node *temp);
int main(void)
{
//printf("\n %d", SIZE);
struct node *root = NULL;
int data;
char ch;
do
{
printf("\n Enter the data\n");
scanf("%d", &data);
insert(&root, data);
printf("\n Do you want to add more elements\n");
scanf("\n%c", &ch);
}while(ch == 'y' || ch == 'Y');
printf("\n Do you want to search data");
scanf("\n %c", &ch);
while(ch == 'y' || ch == 'Y')
{
printf("\n Enter the data to search in the BST");
scanf("\n %d", &data);
search(&root, data);
printf("\n Do you want to search data");
scanf("\n %c", &ch);
}
delete(&root, 8);
return 0;
}
void delete(struct node **root, int data)
{
struct node *temp = NULL;
struct node ** r;
struct node *parent;
struct node *insuc;
r = root;
// find where is the data that is get the address
temp = searchloc(r, data);
if(temp != NULL)
printf("\n Location at which element was found is %p", temp);
else
{
printf("\n The data that you tried to delete doesnt exist in the list");
return;
}
r = root;
// get the parents address as well
parent = searchparent(*r, temp);
if(parent != NULL)
printf("\n Location at which parent was found is %p", parent);
// the location where the element has been found has no children
// simply delete the element and move ahead
if(temp->left == NULL && temp->right == NULL)
{
// set the pointer in the parent to null
if(parent->left == temp)
parent->left = NULL;
else if (parent->right == temp)
parent->right = NULL;
free(temp);
}
// the location where the element has been found has one children
// set the pointer in the parent to point to the child
if(temp->left == NULL && temp->right != NULL)
{
if(parent->left == temp)
parent->left = temp->right;
else if(parent->right == temp)
parent->right = temp->right;
free(temp);
return;
}
else if(temp->left != NULL && temp->right == NULL)
{
if(parent->left == temp)
parent->left = temp->left;
else if(parent->right == temp)
parent->right = temp->left;
free(temp);
return;
}
// the location where the element has been found has two children
// 1. Find the inorder successor
// 2. Copy its data to the node being deleted
// 3. The inorder successor will always have one or no child
// which reduces the problem to deletion of a node with one or zero child
if(temp->left != NULL && temp->right != NULL)
{
// find the location of the inorder successor
insuc = inorder(root, temp);
printf("\n Location of inorder successor is %p and its value is %d", insuc, insuc->data);
}
}
struct node * inorder(struct node **root, struct node *temp)
{
int i = 0;
if((*root) != NULL)
{
inorder((*root)->left, temp);
if(temp->data == (*root)->data)
{
printf("\n %d", (*root)->data);
++ i;
}
// next time i come here with value of i being 1 i would have got the inorder successor
else
{
if(i == 1)
{
return (*root);
}
else
printf("\n Still searching");
}
inorder((*root)->right, temp);
}
}
struct node * searchparent(struct node *root, struct node *temp)
{
if(root->left != NULL && root->left == temp)
{
return root;
}
else if(root->left != NULL)
{
searchparent(root->left, temp);
if(root->right != NULL)
searchparent(root->right, temp);
}
if(root->right != NULL && root->right == temp)
{
return root;
}
else if(root->left != NULL)
{
searchparent(root->left, temp);
if(root->right != NULL)
searchparent(root->right, temp);
}
}
struct node * searchloc(struct node **root, int data)
{
if((*root)->data == data)
{
printf("\n Data Found in the tree");
return (*root);
}
// search in the left of the root
else if((*root)->data < data)
{
if((*root)->right != NULL)
{
searchloc(&(*root)->right, data);
}
else
{
printf("\n Data not in the tree");
return NULL;
}
}
// search in the right of the root
else if((*root)->data > data)
{
if((*root)->left != NULL)
{
searchloc(&(*root)->left, data);
}
else
{
printf("\n Data not in the tree");
return NULL;
}
}
else
{
printf("\n Data does not exist in the tree");
return NULL;
}
}
void search(struct node** root, int data)
{
if((*root)->data == data)
{
printf("\n Data Found in the tree");
return;
}
// search in the left of the root
else if((*root)->data < data)
{
if((*root)->right != NULL)
{
search(&(*root)->right, data);
}
else
{
printf("\n Data not in the tree");
return;
}
}
// search in the right of the root
else if((*root)->data > data)
{
if((*root)->left != NULL)
{
search(&(*root)->left, data);
}
else
{
printf("\n Data not in the tree");
return;
}
}
else
{
printf("\n Data does not exist in the tree");
return;
}
}
void insert(struct node **root, int data)
{
struct node *temp;
// check if its the first element that i am going to add
if(*root == NULL)
{
*root = (struct node *) malloc(sizeof(struct node));
(*root)->left = NULL;
(*root)->right = NULL;
(*root)->data = data;
return;
}
// this is not the first element
// find its correct place in the tree
// data is larger than root so put in the right
else
{
if((*root)->data < data)
{
insert(&((*root)->right), data);
}
else if((*root)->data > data)
{
insert(&((*root)->left), data);
}
}
}
|
| roaan is offline | |
| | #2 | |
| Guest Join Date: Aug 2001
Posts: 5,249
| The program you posted doesn't even compile. Perhaps you should fix those problems first? Quote:
| |
| Sebastiani is offline | |
| | #3 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 302
| It compiles fine without giving any error. |
| roaan is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 10,163
| If you fix line 152 so that it agrees with the prototype, then yes it will compile. (If you don't, it won't.) Why do you left, then right, then left, then right in your searchparent? For that matter, don't you know which way to go? (If temp->data is bigger than root->data, go right, if it's smaller, go left.) |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 302
| Oh yes you were right. But then i dont know why my Visual Studio compiles everything. I posted the problem in another thread as well. |
| roaan is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| BST (Binary search tree) | praethorian | C++ Programming | 3 | 11-13-2005 09:11 AM |
| Binary Tree Search | nickname_changed | C++ Programming | 7 | 01-13-2004 12:40 AM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| Binary Search Tree, Inserting node | cheryl_li | C Programming | 1 | 09-13-2003 03:53 AM |
| BST/Red and Black Tree | ghettoman | C++ Programming | 0 | 10-24-2001 10:45 PM |