简体   繁体   中英

Implementing a binary search tree

I'm trying to implement a binary search tree that holds an inventory of ordered stock. The stocked item attributes are stored in nodes as such:

typedef struct item item_t;
struct item{
    char name;
    int price;
    int quantity;
    item_t *left;
    item_t *right;
};

The idea is to prompt a user to enter the above attributes, and then add the entered item to a node. This is what I've written so far:

item_t *root = NULL;
item_t *current_leaf = NULL;

void prompt_user(){
    /*
    In here contains the code that prompts the user for the item attributes
    and stores it in a variable called input
    */
    insert_node(input);
}

void insert_node(char *input){
    /*If tree doesnt have a root...*/
    if (root == NULL){

        /*Create one...*/
        *root = create_node(input);
    }

    else{
        item_t *cursor = root;
        item_t *prev = NULL;
        int is_left = 0;
        int comparison;

        while(cursor != NULL){

            /*comparison will be 1 is the key of input is less than the key   
            of the cursor, and 2 otherwise...*/
            comparison = compare(input, cursor);
            prev = cursor;

            if(comparison == 1){
                is_left = 1;
                cursor = cursor->left;
            }
            else if (comparison == 2){
                is_left = 0;
                cursor = cursor->right;
            }
        }

        if(is_left){
            *prev->left = create_node(input);
            current_leaf = prev->left;
        }
        else{
            *prev->right = create_node(input);
            current_leaf = prev->right;
        }
    }
}

item_t create_node(char *input){

    item_t *new_node = (item_t*)malloc(sizeof(item_t));

    if (new_node == NULL){
        printf("Out of memory. Shutting down.\n");
        exit(EXIT_FAILURE);
    }

    /*Add data to the node...*/
    update_item(input, new_node);

    new_node->left = NULL;
    new_node->right = NULL;

    current_leaf = new_node;

    return *new_node;
}

I want root to always be pointing to the first item ever entered, and current_leaf to be pointing to the last item processed. compare returns 1 if the item being processed ( input ) is less than the last processed item ( current_leaf ). update_item is what sets the data for the new nodes (leaves).

The above isn't fully complete, but it's what I'm up to at the moment. I'm struggling to work out how to write add_node and how to keep current_leaf updated correctly.

EDIT: revised my code

It is an example of BST. Structure of the tree is:

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

Letting root global is not a good idea, so it is better to declare it inside main() and the insert() will be called from main like this:

int main()
{
    int n,data;
    node *root=NULL;
    printf("How many elements? ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         scanf("%d",&data);
         insert(&root,data);
    }
    inorder(root);
    return 0;
}

This is the code for insertion and traversing a binary tree-

void insert(node **root, int data)
{
    node *n1,*temp;
    n1=(node *)malloc(sizeof(node));

    n1->data=data;
    n1->left=n1->right=NULL;
    if(!(*root))
    {
        (*root)=n1;
        //printf("Node inserted\n");
        return;
    }
    temp=*root;
    while(temp!=NULL)
    {
        if(temp->data<temp->data)
        {
            //printf("To the right");
            if(temp->right==NULL)
            {
                temp->right=n1;
                break;
            }
            else
                temp=temp->right;
        }
        else if(temp->data>=n1->data)
        {
            if(temp->left==NULL)
            {
                temp->left=n1;
                break;
            }
            else
                temp=temp->left;
        }
    }
    //printf("Inserted\n");
}

void inorder(node *root)
{
    if(root==NULL)
        return;
    inorder(root->left);
    printf("item: %d",root->data);
    inorder(root->right);
}

The searching will be same almost same logic as insert, please develop it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM