GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Oct-2009, 05:33
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 156
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about
Question

Deleting node - binary search tree


Hello folks.

been trying to delete a node from a binary search tree (sorted tree that is). The delete does not work as expected.

Heres the code:
CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>

struct node{
    struct node *right;
    struct node *left;
    int data;
};

void insertnode(struct node **p_treetop,int data1);
void inorder(struct node *treetop);
void preorder(struct node *treetop);
void postorder(struct node *treetop);
int depth(struct node *treetop);
signed int del(struct node **p_treetop,int elmt);

int main() {
    struct node *treetop = NULL;
    int elmt,choice;

    puts("1.depth");
    puts("2.insert element");
    puts("3.inorder");	
    puts("4.pretorder");
    puts("5.postoder");
    puts("6.delete elmt");
    puts("7.exit");

    while(1) {	
        puts("pls enter choice:");
        scanf("%d",&choice); 	
	
        switch(choice) {
        case 1:
            printf("depth = %d\n",(elmt=depth(treetop))?elmt-1:0);
            break;       
        case 2:			
            puts("pls enter elmt to insert");
            scanf("%d",&elmt);
            insertnode(&treetop,elmt);				
            break;
        case 3:			
            inorder(treetop);
            puts("");
            break;				
        case 4:			
            preorder(treetop);
            puts("");
            break;				
        case 5:			
            postorder(treetop);
            puts("");
            break;				
        case 6:			
            puts("pls enter elmt to delete");
            scanf("%d",&elmt);
            elmt = del(&treetop,elmt);
            if(elmt < 0) puts("elmt not found");
            else printf("deleted %d\n",elmt);
            break;
        case 7:
            exit(0);
        default:
            puts("wrong choice");
        }
    }
    return 0;
}

void insertnode(struct node **p_treetop,int data1) {

    if(*p_treetop == NULL) {
        struct node *new = (struct node*) malloc(sizeof(struct node));
        new -> data = data1;
        *p_treetop = new;
    }
    else {
        if((*p_treetop) -> data > data1)
            insertnode(&((*p_treetop)->left),data1);
        else
            insertnode(&((*p_treetop)->right),data1);
    }
}

void inorder(struct node *treetop) {
    if(treetop) {
        inorder(treetop->left);
        printf("%d ",treetop->data);
        inorder(treetop->right);
    }
    else
        return;	
}

void preorder(struct node *treetop) {
    if(treetop) {
        printf("%d ",treetop->data);
        preorder(treetop->left);
        preorder(treetop->right);
    }
    else
        return;	
}

void postorder(struct node *treetop) {
    if(treetop) {
        postorder(treetop->left);
        postorder(treetop->right);
        printf("%d ",treetop->data);
    }
    else
        return;	
}

int depth(struct node *treetop) {
    int max,temp;

    if(!treetop)
        return 0;
    else {
        temp = max;
        max = depth(treetop -> left);
        if(max < (temp = depth(treetop -> right)))            
            max = temp;
        return (max+1);
    }
}

signed int del(struct node **p_treetop,int elmt) {
    signed int tmp;
    struct node **tmp1;

    if ((*p_treetop) == NULL)
        return -1; 
    else if((*p_treetop) -> data == elmt) {        
        if((*p_treetop) -> left) { //does node to del has left sub tree            
            tmp = (*p_treetop) -> data;

            struct node **trav = &((*p_treetop) -> left);

            while((*trav) -> right) //search for largest elmt in right sub tree
                *trav = (*trav) -> right;
            (*p_treetop) -> data = (*trav) -> data; //replace node data with this elmt

            while((*trav) -> left) { //does it have further elmt to the left
                (*trav) -> data = (*trav) -> left -> data; //shift tree one step up
                (*trav) = (*trav) -> left;
            }
            free(*trav);

            *trav = NULL;
            return tmp;
        }
        else if((*p_treetop) -> right) { //does node to del has a right subtree
            tmp = (*p_treetop) -> data;

            struct node **trav = &((*p_treetop) -> right);


            while((*trav) -> left) { //search for smallest elmt in left sub tree
                *tmp1 = *trav;
                *trav = (*trav) -> left;
            }

            (*p_treetop) -> data = (*trav) -> data; //replace node data with this elmt

            while((*trav) -> right) { //does it have further elmt to the right?
                (*trav) -> data = (*trav) -> right -> data; //shift tree one step up
                (*trav) = (*trav) -> right;
            }

            free(*trav);
            *trav = NULL;

            return tmp;
        }
        else {
            tmp = (*p_treetop) -> data;
            free((*p_treetop));
            *p_treetop = NULL;
            return tmp;
        }

    }
    else if((*p_treetop) -> data > elmt)
        return(del(&((*p_treetop) -> left),elmt));
    else if((*p_treetop) -> data < elmt)
        return(del(&((*p_treetop) -> right),elmt));    
}

If given the following input to insert node: 13 5 14 10 9, 13 becomes the root, with 5 on its left and 14 on its right. Again 10 goes to the right of 5 and then 9 goes to the left of the newly inserted 10. Thus an inorder traversal after this shows 9 13 14 and theres no mention of 10.

When im trying to remove 5, it does the right thing by replacing 5 with 9 (the smallest element in the right i.e. 'greater than' sub tree), but it also removes the intermediate 10 (which precedes 9). I do not understand where in this world am I erring

Again when I use free inside a function, as I am using now, I make the target memory location available for writes if im right?
Any input or suggestings would be high appreciated.
__________________
Hope to hear from you guys!

--------------------------------------------------

Best Regards,
Aijaz Baig.
  #2  
Old 21-Oct-2009, 06:11
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Deleting node - binary search tree


When I run your program with that input I get the following:
Quote:
Originally Posted by output
5 9 10 13 14
. When I run this through valgrind I get the following (edited for brevity)
Quote:
Originally Posted by valgrind
==18789== ERROR SUMMARY: 10 errors from 10 contexts (suppressed: 13 from 1)
==18789==
==18789== 1 errors in context 1 of 10:
==18789== Conditional jump or move depends on uninitialised value(s)
==18789== at 0x8048738: inorder (t.c:86)
==18789== by 0x804876D: inorder (t.c:89)
==18789== by 0x804876D: inorder (t.c:89)
==18789== by 0x80485D8: main (t.c:43)
==18789==
==18789== 1 errors in context 2 of 10:
==18789== Conditional jump or move depends on uninitialised value(s)
==18789== at 0x8048738: inorder (t.c:86)
==18789== by 0x8048747: inorder (t.c:87)
==18789== by 0x804876D: inorder (t.c:89)
==18789== by 0x80485D8: main (t.c:43)
==18789==
==18789== 1 errors in context 3 of 10:
==18789== Conditional jump or move depends on uninitialised value(s)
==18789== at 0x8048738: inorder (t.c:86)
==18789== by 0x804876D: inorder (t.c:89)
==18789== by 0x804876D: inorder (t.c:89)
==18789== by 0x8048747: inorder (t.c:87)
==18789== by 0x80485D8: main (t.c:43)
Changing
CPP / C++ / C Code:
struct node *new = (struct node*) malloc(sizeof(struct node));
to
CPP / C++ / C Code:
struct node *new = (struct node*) calloc(1,sizeof(struct node));
Fixes those errors. Try that and see what you get.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 21:50
Deleting a node from binary search tree earachefl C++ Forum 3 28-Jun-2006 08:26
can anyone help me with my tree :) bioeng_mtm C++ Forum 5 22-Apr-2006 13:50
Binary Search Tree penance C Programming Language 2 07-Aug-2005 05:37
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 11:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.