数据结构上机——二叉树

#include <iostream>
#include<stdio.h>  
#include<stdlib.h> 
using namespace std;

typedef char TelemType;

typedef struct BinaryTreeNode {
    TelemType data;
    struct BinaryTreeNode *Left;
    struct BinaryTreeNode *Right;
} Node;
typedef Node * BinTree;

//创建二叉树,中间节点->左子树->右子树
Node* createBinaryTree() {
    Node *p;
    TelemType ch;
    cin>>ch;
    if(ch == '@') {   
        p = NULL;
    } else {
        p = (Node*)malloc(sizeof(Node));
        p->data = ch;
        p->Left  = createBinaryTree();  
        p->Right = createBinaryTree();  
    }
    return p;
}

//先序遍历
void preOrder(Node* root) {
    if( root ) {
        cout<<root->data<<' ';
        preOrder(root->Left);
        preOrder(root->Right);
    }
}

//中序遍历
void inOrder(Node* root) {
    if( root ) {
        inOrder(root->Left);
        cout<<root->data<<' ';
        inOrder(root->Right);
    }
}

//后序遍历
void lastOrder(Node* root) {
    if( root ) {
        lastOrder(root->Left);
        lastOrder(root->Right);
        cout<<root->data<<' ';
    }
}
//二叉树的深度
int DepthOfTree(Node* root) {
    if(root) {
        return DepthOfTree(root->Left) > DepthOfTree(root->Right)?  DepthOfTree(root->Left)+1:DepthOfTree(root->Right)+1;
    }
    if( root == NULL ) {
        return 0;
    }
}

//二叉树叶子节点数
int Leafnum(Node* root) {
    if(!root) {
        return 0;
    } else if(  (root->Left == NULL) && (root->Right == NULL) ) {
        return 1;
    } else {
        return  (Leafnum(root->Left) + Leafnum(root->Right)) ;
    }
}


int main() {//ABC@@DE@G@@F@@@
    BinTree root = NULL;
    root = createBinaryTree();
    printf("二叉树建立成功");
    cout<<"二叉树深度为:"<<DepthOfTree(root)<<endl;
    cout<<"二叉树叶子节点数为:"<<Leafnum(root)<<endl;
    cout<<endl;
    cout<<"前序遍历结果:"<<endl;
    preOrder(root);
    cout<<endl;
    cout<<"中序遍历结果:"<<endl;
    inOrder(root);
    cout<<endl;
    cout<<"后序遍历结果:"<<endl;
    lastOrder(root);
    cout<<endl;
    return 0;
}
/*ABC@@DE@G@@F@@@
二叉树建立成功二叉树深度为:5
二叉树叶子节点数为:3

前序遍历结果:
A B C D E G F
中序遍历结果:
C B E G D F A
后序遍历结果:
C G E F D B A
Press any key to continue*/
#include <cstdio>  
#include <cstring>  
const int maxn=105;  
char pre[maxn],in[maxn];  

struct node{  
    char data;  
    node* lch;  
    node* rch;  
};  

node* create(int prel,int prer,int inl,int inr){ //创建
    if(prel>prer) return NULL;//递归结束条件
    node* root=new node;  
    root->data=pre[prel];  
    int k;  
    for(k=inl;k<=inr;k++) if(in[k]==pre[prel]) break;  //每次递归找(子)树根
    int numleft=k-inl;  
    root->lch=create(prel+1,prel+numleft,inl,k-1);  
    root->rch=create(prel+numleft+1,prer,k+1,inr);  
    return root;  
}  

void postorder(node* root){  //输出
    if(root==NULL) return;  //递归结束条件
    postorder(root->lch);  
    postorder(root->rch);  
    printf("%c",root->data);  
}  

int main(){  

    while(scanf("%s%s",pre,in)==2){  
        int n=strlen(pre);  
        node* root=create(0,n-1,0,n-1);  
        postorder(root);  
        printf("\n");  
    }  
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值