Thread: Search and delete function for binary trees.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    12

    Search and delete function for binary trees.

    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);
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A few pointers:

    int main should return 0 and have "void" as it's argument.

    Your indentation is very bad.

    If it is no longer part of your assignment, why can't you Google for the code?... C Binary Tree with an Example C Code (Search, Delete, Insert Nodes)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ABt Binary Search Trees
    By ging ging in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2009, 12:58 PM
  2. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  3. Binary Search Trees
    By chaos in forum C Programming
    Replies: 1
    Last Post: 06-14-2005, 05:42 AM
  4. STL and Binary Search Trees
    By xshapirox in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2004, 12:12 PM
  5. binary search trees
    By sweets in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2004, 11:02 AM