实验4 按照满二叉树的特点生成一棵二叉树

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#define MAXSIZE 100


typedef struct tnode{
char data;
struct tnode *lchild, *rchild;
}tnode,*bitree;

int createBiTree(bitree &T,char s[],int n)
{
	    if(s[n-1]=='#')
		{
		T=NULL;
	    return 0;
		}
        else
		{
			if(n>strlen(s))
			{
				T=NULL;
				return 0;
			}
			else
			{
				T=(bitree)malloc(sizeof(tnode));
			    T->data=s[n-1];
			    createBiTree(T->lchild,s,2*n);
			    createBiTree(T->rchild,s,2*n+1);
			}
		}

	return 0;
}


void preorder(bitree T)
{
	if(T!=NULL)
	{
		printf("  %c",T->data);
        preorder(T->lchild);
        preorder(T->rchild);
	}
}

void inorder(bitree T)
{
 if(T!=NULL)
	{
     inorder(T->lchild);
     printf("  %c",T->data);
     inorder(T->rchild);
	}
}

void postorder(bitree T)
{
 if(T!=NULL)
	{
     postorder(T->lchild);
     postorder(T->rchild);
     printf("  %c",T->data);
	}
}





int main()
{
	bitree h;
	char s[MAXSIZE];
	printf("创建的二叉树为:\n");
	scanf("%s",s);
    createBiTree(h,s,1);
	printf("先序遍历的二叉树:\n");
    preorder(h);
    printf("\n");
    printf("中序遍历的二叉树:\n");
    inorder(h);
    printf("\n");
    printf("后序遍历的二叉树:\n");
    postorder(h);
    printf("\n");
return 0;
}

    原文作者:满二叉树
    原文地址: https://blog.csdn.net/qq_40124195/article/details/78587236
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞