zoukankan      html  css  js  c++  java
  • 二叉树的创建和遍历

    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    #define ERROR 0
    
    typedef int status;
    typedef struct BiNode
    {
    	char data;
    	struct BiNode *lchild,*rchild;
    }BiTNode,*BiTree;  //定义二叉树
    
    status createBiNode(BiTree &t)
    {  
    	char ch;
        ch=getchar();
    	if(ch==' ')
    		t=NULL;
    	else
    	{   //先输入根节点 在左子树 再右子树
    		if(!(t=(BiTNode *)malloc(sizeof(BiTNode)))) exit(ERROR); //分配空间失败
    		t->data=ch;  
    		createBiNode(t->lchild);
    		createBiNode(t->rchild);
    	}
    	return OK;
    }
    
    //先序访问
    status previsit(BiTree t)
    {
    	if(t)
    	{
    		putchar(t->data);
    		previsit(t->lchild);
    		previsit(t->rchild);
    	}
    	return OK;
    }
    
    //后序访问
    status lastvisit(BiTree t)
    {
    	if(t)
    	{
    		lastvisit(t->lchild);
    		lastvisit(t->rchild);
    		putchar(t->data);
    	}//return OK;
    	return OK;
    }
    
    //中序访问
    status midvisit(BiTree t)
    {
    	if(t)
    	{
    	   midvisit(t->lchild);
    	   putchar(t->data);
    	   midvisit(t->rchild);
    	}
    	return OK;
    }
    
    //二叉树的销毁
    void  destory(BiTree &t)
    {
    	if(t)
    	{
    		destory(t->lchild);
    		destory(t->rchild);
    		free(t);
    	}
    	t=NULL;
    }
    int main()
    {
        BiTree t=NULL;
        createBiNode(t);
        //previsit(t);  //先序访问
        //midvisit(t);  //中序访问
    	lastvisit(t);//后续访问
    	destory(t);
    	printf("\n");
    	lastvisit(t);
    	return 0;
    }
    


  • 相关阅读:
    ajax
    cookie
    JavaScript基本语法
    HTML css 格式布局
    HTML 表单
    html 一般标签 常用标签 表格
    WinForm——操作word文档
    WinForm进程 线程
    音乐
    注册 传值
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3063457.html
Copyright © 2011-2022 走看看