二叉树的创建和前中后三种遍历方法

头文件:

#include<stdlib.h>
#include<stdio.h>
typedef struct bnode
{
    int data;//结点数据类型
    struct bnode *lchild, *rchild;// 定义左右孩子为指针类型
}btree;
btree *insert_node(btree *root, int node) //插入结点的函数
{
    btree *newpointer;
    btree *currentpointer;
    btree *parentpointer;
    newpointer = (btree *)malloc(sizeof(btree));//向系统申请内存的函数malloc
    newpointer->data=node;
    newpointer->lchild=NULL;
    newpointer->rchild=NULL;
    if(root==NULL)
        return newpointer;
    else
    {
        currentpointer=root;
        while(currentpointer!=NULL)
        {
            parentpointer=currentpointer;
            //若当前插入的值比父结点小,放在左,否则放在右
            if(currentpointer->data>node)
                currentpointer=currentpointer->lchild;
            else
                currentpointer=currentpointer->rchild;
        }
        if(parentpointer->data>node)
            parentpointer->lchild=newpointer;
        else
            parentpointer->rchild=newpointer;
    }
    return root;
}
btree *create_btree(int data[], int len) //建立二叉树
{
    int i;
    btree *root = NULL;
    for(i=0; i<len; i++)
    {
        root=insert_node(root,data[i]);//调用插入二叉树结点的函数
    }
    return root;
}

二叉树的前序非递归遍历:

//前序遍历,从根节点开始,再到左子树 再到右子树
void preorder(btree *root)
{
    btree *p, *s[100];
    int top=0;
    p=root;
    while((p!=NULL)||(top>0))
    {
        while(p!=NULL)
        {
            printf("%d",p->data);//直接输出根节点,然后左然后右
            s[++top]=p;
            p=p->lchild;
        }
        p=s[top--];
        p=p->rchild;
    }
}
int main()
{
    btree *root=NULL;
    int value, index, nodelist[20];
    index=0;
    scanf("%d", &value);
    while(value!=0)
    {
        nodelist[index]=value;
        index++;
        scanf("%d",&value);
    }
    root= create_btree(nodelist,index);
    preorder(root);
    return 0;
}


二叉树的中序递归遍历:先遍历左子树,再根结点,再右子树

<pre name="code" class="csharp">void inorder(btree *root)
{
    if(root!=NULL)
    {
        inorder(root->lchild);// left child
        printf("%d",root->data); // root
        inorder(root->rchild);// right child
    }
}
int main()
{
    btree *root=NULL;
    int value, index, nodelist[100];
    index=0;
    scanf("%d",&value);
    while(value!=0)
    {
        nodelist[index]=value;
        index++;
        scanf("%d",&value);
    }
    root=create_btree(nodelist,index);
    inorder(root);
    return 0;
}

 

二叉树的后序递归遍历: 先左子树,再右子树,再根节点

void postorder(btree *root)
{
    if(root!=NULL)
    {
        postorder(root->lchild);// left 
        postorder(root->rchild);// right
        printf("%d",root->data); // root
    }
}
int main()
{
    btree *root = NULL;
    int value,index,nodelist[100];
    index=0;
    scanf("%d",&value);
    while(value!=0)
    {
        nodelist[index]=value;
        index++;
        scanf("%d",&value);
    }
    root=create_btree(nodelist,index);
    postorder(root);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值