zoukankan      html  css  js  c++  java
  • 二叉树的遍历算法 递归算法

    ////////////////////////////////////////////
    //二叉树的遍历算法 递归算法 			  //
    //Author:Wang Yong				  		  //	
    //Date:	2010.8.19				  		  //
    ////////////////////////////////////////////
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char ElemType;
    ///////////////////////////////////////////
    
    //定义树的结点类型
    
    typedef struct BNode 
    {
    	ElemType data;
    	struct BNode *lchild,*rchild;	
    };
    ///////////////////////////////////////////
    
    //二叉树的创建
     
    BNode *CreatBitTree()
    {
    	BNode *p;
    	char c;
    	scanf("%c",&c);
    	if(c == '#') p = NULL; //截止二叉树的建立 
    	else
    	{
    		p = (BNode *) malloc(sizeof(BNode)); //申请结点空间 
    		p->data = c;
    		p->lchild = CreatBitTree();
    		p->rchild = CreatBitTree();
    	}
    	return  p;
    } 
    
    ////////////////////////////////////////////
    
    //二叉树的先序遍历
    
    void  PreOrderTraverse(BNode *p)
    {
    	if(p != NULL)
    	{
    		printf("%c*",p->data);
    		PreOrderTraverse(p->lchild);
    		PreOrderTraverse(p->rchild);
    	}
    }
    
    /////////////////////////////////////////////
    
    //二叉树的中序遍历
    
    void InOrderTraverse(BNode *p)
    {
    	if(p != NULL)
    	{
    		InOrderTraverse(p->lchild);
    		printf("%c*",p->data);
    		InOrderTraverse(p->rchild);
    	}
    }
    
    ////////////////////////////////////////////// 
    
    //二叉树的后序遍历
    
    void PostOrderTraverse(BNode *p)
    {
    	if(p != NULL)
    	{
    		PostOrderTraverse(p->lchild);
    		PostOrderTraverse(p->rchild);
    		printf("%c*",p->data);
    	}
    } 
    
    /////////////////////////////////////////////
    
    int main()
    {
    	BNode *tree;
    	tree = CreatBitTree();
    	PreOrderTraverse(tree);
    	printf("\n");
    	InOrderTraverse(tree);
    	printf("\n");
    	PostOrderTraverse(tree);
    	printf("\n");
    	
    	return 0;
    } 
    
  • 相关阅读:
    Distinctive Image Features from ScaleInvariant
    Natural Language Toolkit
    Regression analysis
    泌尿系统 Excretory system
    file_get_contents preg_match php nameisboy
    wWAITING
    instructionset architecture Processor Architecture
    improve performance whilemaintaining the functionality of a simpler and more abstract model design of processor hardware
    cluster analysis in data mining
    Maximum Likelihood
  • 原文地址:https://www.cnblogs.com/newwy/p/1847472.html
Copyright © 2011-2022 走看看