树-1-二叉树的三种遍历

   日期:2020-10-12     浏览:89    评论:0    
核心提示:#include <iostream>#include <malloc.h>using namespace std; typedef struct BTNode{ int data; struct BTNode *lchild; struct BTNode *rchild;}BTNode;void createTree(BTNode *&T){ BTNode *a,*b,*c,*d,*e,*f,*g,*h,*i; T = (BTNode*)mall
#include <iostream>
#include <malloc.h>
using namespace std; 

typedef struct BTNode{ 
	int data;
	struct BTNode *lchild;
	struct BTNode *rchild;
}BTNode;

void createTree(BTNode *&T){ 
	BTNode *a,*b,*c,*d,*e,*f,*g,*h,*i;
	
	T = (BTNode*)malloc(sizeof(BTNode));T->data = 1;
	a = (BTNode*)malloc(sizeof(BTNode));a->data = 2;
	b = (BTNode*)malloc(sizeof(BTNode));b->data = 3;
	c = (BTNode*)malloc(sizeof(BTNode));c->data = 4;
	d = (BTNode*)malloc(sizeof(BTNode));d->data = 5;
	e = (BTNode*)malloc(sizeof(BTNode));e->data = 6;
	f = (BTNode*)malloc(sizeof(BTNode));f->data = 7;
	g = (BTNode*)malloc(sizeof(BTNode));g->data = 8;
	h = (BTNode*)malloc(sizeof(BTNode));h->data = 9;
	i = (BTNode*)malloc(sizeof(BTNode));i->data = 10;

	T->lchild = a;T->rchild = b;
	a->lchild = c;a->rchild = NULL;
	b->lchild = d;b->rchild = e;
	c->lchild = NULL;c->rchild = f;
	d->lchild = g;d->rchild = h;
	e->lchild = NULL;e->rchild = i;
	f->lchild = NULL;f->rchild = NULL;
	g->lchild = NULL;g->rchild = NULL;
	h->lchild = NULL;h->rchild = NULL;
	i->lchild = NULL;i->rchild = NULL;	
}
void visit(BTNode *T){ 
	cout<<T->data<<"**"<<endl;
}
void preOrder(BTNode *T){ 
	if(T!=NULL){ 
	visit(T);
	
	preOrder(T->lchild);
	preOrder(T->rchild);
	}	
}
void inOrder(BTNode *T){ 
	if(T!=NULL){ 
	
	
	inOrder(T->lchild);
	visit(T);
	inOrder(T->rchild);
	}	
}
void postOrder(BTNode *T){ 
	if(T!=NULL){ 
	
	
	postOrder(T->lchild);
	postOrder(T->rchild);
	visit(T);
	}	
}

int main(int argc, char** argv) { 	
	BTNode *T;
	
	createTree(T);
	
	postOrder(T);
	

	return 0;
}

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服