• 二叉树的插入建立


    #include <stdlib.h>
    #include <stdio.h>
    typedef struct btnode
    {
        int data;
        struct btnode *lchild,*rchild;
    }btnode;
    int b[100];
    //在二叉树中插入元素
    btnode *insertnode(btnode *root,int node)
    {
        btnode *newnode;
        btnode *currentnode;
        btnode *parentnode;
        newnode=(btnode*)malloc(sizeof(btnode));
        newnode->lchild=NULL;
        newnode->rchild=NULL;
        newnode->data=node;
        if (root==NULL)
        return newnode;
        else
        {
            currentnode=root;
            while(currentnode!=NULL)
            {
                parentnode=currentnode;
                if (currentnode->data>node)
                currentnode=currentnode->lchild;
                else
                currentnode=currentnode->rchild;
            }
            if (parentnode->data>node)
            parentnode->lchild=newnode;
            else
            parentnode->rchild=newnode;
        }
        return root;
    }
    //建立二叉树
    btnode* createtree(int *s,int len)
    {
        btnode* root=NULL;
        for (int i=0;i<len;++i)
        root=insertnode(root,s[i]);
        return root;
    }
    //先序遍历输出二叉树
    void preorder(btnode *root)
    {
        if (root==NULL)
        return;
        else
        {
            printf ("%d",root->data);
            preorder(root->lchild);
            preorder(root->rchild);
        }
    }
    int main()
    {
      int s[20];
        btnode* root;
        root=NULL;
        int len=0;
        int n;
        scanf("%d",&n);
        while(n!=0)
        {
            s[len]=n;
            len++;
            scanf("%d",&n);
        }
        root=createtree(s,len);
        printf ("the tree is created!
    ");
        preorder(root);
        return 0;
    }
  • 相关阅读:
    浮点数小数点后开始非零数字的起始位置
    关于接口测试
    性能测试模型之曲线拐点模型
    2018春招实习笔试面试总结(PHP)
    mysql删除表中的记录
    浅析单点登录
    MySQL两种引擎的比较
    Redis初探(windows/linux安装)
    剑指offer试题(PHP篇三)
    剑指offer试题(PHP篇二)
  • 原文地址:https://www.cnblogs.com/1994two/p/3369122.html
Copyright © 2020-2023  润新知