c语言数据结构---三叉树

#include
#include
#include
using namespace std;
typedef struct node{
	struct node*lchild;
	struct node*rchild;
	struct node*parent;
	char data;
}Node,*Bit;


Bit creat(){//先序创建 
char b;
int t;
scanf("%c",&b);
t=getchar();//吸收空格 
	if(b=='#')return NULL;
	else{
		Bit tree=(Bit)malloc(sizeof(Node));
		tree->parent=NULL;
		tree->data=b;
	printf("please input %c lefttree:",b);
        tree->lchild=creat();
		printf("please input %c righttree:",b); 
		tree->rchild=creat();
		return tree;
	}
}

void jian(Node *tree){//建立parents连接 

	tree->parent=NULL;
	if(tree->lchild!=NULL){
		tree->lchild->parent=tree;
		jian(tree->lchild);
	}
	if(tree->rchild!=NULL){
		tree->rchild->parent=tree;
		jian(tree->rchild);
	}
	else return;
	
}
void printzhong(Node *tree){//中序打印 
	if(tree){
		
		printzhong(tree->lchild);
		printf("%c\t",tree->data);
		printzhong(tree->rchild);
	}
}

int main(){
Bit tree;
printf("please input root data:\n");
tree=creat();
printf("success_init_tree\n");
jian(tree);
printf("success contact\n");
printzhong(tree); 
}

你可能感兴趣的:(c语言,数据结构,c++)