⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preorder.cpp

📁 文件夹中包括常用的数据结构的算法
💻 CPP
字号:
#include<malloc.h>
#include <stdio.h>
#define m0 100   //定义字符串结构中的最大长度
#define m2 20
#define null 0
#define Len  sizeof (BinNode)

enum tag{zero,one};

typedef struct BinNode             // 结点结构
{ 
	char data;
    struct BinNode *lch,*rch;
    enum tag ltag,rtag;
}BinNode,*Bintree;


struct chtp{ int len;      char ch[m0];    };  //定义字符串结构
struct BinNode *bt,*m,*stack[m2];       //全局变量,   定义堆栈,中序遍历二叉树的非递归算法要用
struct chtp s;
int top=0,ii=0;          //两个全局变量

void  crt_pre(Bintree *t)      //指向指针的指针,可以用引用来代替这种用法
{ 
	char c;      
//	int i;
    c=s.ch[ii];  
	ii=ii+1;

    if (c=='.')  *t=null;
    else 
	{ 
		*t=(BinNode *)malloc(Len);
		(*t)->data=c;
		crt_pre(&(*t)->lch);
		crt_pre(&(*t)->rch);
    };
}

void preorder(BinNode *bt)                      //先序遍历二叉树的递归
{  
	if(bt!=NULL)
    { 
	    printf("%c\t",bt->data);               // 访问结点
        preorder(bt->lch);                 // 遍历左子树
        preorder(bt->rch);               //遍历右子树
    }
}



void main()
{
    char    c[]={'a','b','c','.','.','d','e','.','g','.','.','f','.','.','.','!'};
    int i=0;
    for(i=0,s.len=0;c[i]!='!';i++,s.len++) s.ch[i]=c[i];
    
	crt_pre(&bt);                     //bt为全局变量
	preorder(bt);
	printf("\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -