如何将rchild和lchild添加到我的BST? [英] How do I add rchild and lchild to my BST?

查看:140
本文介绍了如何将rchild和lchild添加到我的BST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我有以下C程序将节点添加到树中,然后用户可以选择排序方法。如何以允许我向每个节点添加LChild和RChild的方式修改程序?任何帮助都非常受欢迎,因为我对BST来说是全新的。



我尝试了什么:



Hello!
I have the following C program that adds Nodes to a tree and then the user can select a sorting method. How can I modify the program in a manner that will allow me to add a LChild and RChild to every node? Any help is highly appreciated since I am completely new to BSTs.

What I have tried:

#include <stdio.h>
    #include <stdlib.h>

    struct treenode {
        struct treenode *lchild;
        struct treenode *rchild;
        int data;
        } *root = NULL;

    void insertnode(struct treenode **pp,int d)
    {
        for( ;*pp; )
        {
             if (d < (*pp)->data) pp = &(*pp)->lchild;
             else  pp = &(*pp)->rchild;
        }
        *pp = malloc (sizeof **pp);
        (*pp)->data = d;
        (*pp)->lchild = NULL;
        (*pp)->rchild = NULL;
    }

    void preorder(struct treenode *p)
    {
        if(p==NULL)
        {
            printf("\nThe list is empty");
            return;
        }

        printf("%d,",p->data);
        if (p->lchild) preorder(p->lchild);
        if (p->rchild) preorder(p->rchild);
    }

    void postorder(struct treenode *p)
    {
        if(p==NULL)
        {
            printf("\nThe list is empty");
            return;
        }

        if (p->lchild) preorder(p->lchild);
        if (p->rchild) preorder(p->rchild);
        printf("%d,",p->data);
    }
    void inorder(struct treenode *p)
    {
        if(p==NULL)
        {
            printf("\nThe list is empty");
            return;
        }

        if (p->lchild) preorder(p->lchild);
        printf("%d,",p->data);
        if (p->rchild) preorder(p->rchild);
    }

    int main(void)
    {
        root=NULL;
        int choice,data;

        while(1)
        {
         printf("\nPress 1 for inserting a node in BST fashion: ");
         printf("\nPress 2 for traversing the tree in preorder fashion :");
         printf("\nPress 3 for traversing the tree in postorder fashion :");
         printf("\nPress 4 for traversing the tree in inorder fashion :");
         printf("\nPress 5 to exit :");


         printf("\nEnter your choice: ");
         scanf("%d", &choice);

        switch(choice)
        {
        case 1: printf("\nEnter the data to be inserted:");
            scanf("%d",&data);
            insertnode( &root,data);
          
            break;

        case 2: preorder(root);
            break;

        case 3: postorder(root);
            break;

        case 4: inorder(root);
            break;

        case 5: exit(0);
            break;
        default: printf("\nYou have entered an invalid choice. Please try     again");
        }
    }

    return 0;
    }

推荐答案

您的代码必须用另一棵树填充rchild和lchild数据。

Your code must fill the rchild and lchild data with another tree.
//allocate some tree
 struct treenode *lnode = malloc(sizeof(struct treenode));
//fill lnode with some data 
lnode->data = ....
(*pp)->lchildstruct = lnode;

最好是编写一些节省时间的功能,例如

Best is to write some time-saving functions like

struct treenode *treeWithData(int data);
void setLeftChild(struct treenode *node, struct treenode *lNode);


这篇关于如何将rchild和lchild添加到我的BST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆