二叉树周游

lastOne.h

#ifndef __lastOne_H__
#define __lastOne_H__
#include <iostream>
#include <stdio.h>   
#include <stdlib.h>  
typedef char TElemType;
using namespace std;
typedef struct BiTNode {
	TElemType data;
	struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
struct Node;
typedef struct Node * PNode;
typedef int DataType;
struct Node{
	BiTree bnode;
	PNode link;
};
struct LinkStack{
	PNode top;
};
typedef struct LinkStack * PLinkStack;
PLinkStack createEmptyStack_link(void);
int isEmptyStack_link(PLinkStack plstack);
void push_link(PLinkStack plstack,BiTree x);
BiTree pop_link(PLinkStack plstack);
void CreateBiTree(BiTree &T);
void recPostOrderTraverse(BiTree T);
void recInOrderTraverse(BiTree T);
void recPreOrderTraverse(BiTree T);
void InOrderTraverse(BiTree T);
void PreOrderTraverse(BiTree T);
void PostOrderTraverse(BiTree T);
void menu();
void childrenLeaves(BiTree T);
int Depth(BiTree T);
int NodeCount(BiTree T);
int sum=0;
void childrenLeaves(BiTree T){
	if(T==NULL){
	}
	else{
		if(T->lchild!=NULL&&T->rchild!=NULL){
			sum++;
			childrenLeaves(T->lchild);
			childrenLeaves(T->rchild);
		}
	}
}
int Depth(BiTree T){
	int n,m;
	if(T==NULL)
		return 0;
	else {
		m=Depth(T->lchild);
		n=Depth(T->rchild);
		if(m>n)
			return(m+1);
		else
			return(n+1);
	}
}
int NodeCount(BiTree T){
	if(T==NULL)
		return 0;
	else
		return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
void menu(){
	cout<<"1.recPre\n2.recIn\n3.recPost\n4.pre\n5.in\n6.post\n7.depth\n8.leaves\n9.childrenLeaves\n";
}
void recPreOrderTraverse(BiTree T){
	if(T){
		cout<<T->data;
		recPreOrderTraverse(T->lchild);
		recPreOrderTraverse(T->rchild);
	}
}
void recInOrderTraverse(BiTree T){
	if(T){
		recInOrderTraverse(T->lchild);
		cout<<T->data;
		recInOrderTraverse(T->rchild);
	}
}
void recPostOrderTraverse(BiTree T){
	if(T){
		recPostOrderTraverse(T->lchild);
		recPostOrderTraverse(T->rchild);
		cout<<T->data;
	}
}
void CreateBiTree(BiTree &T){  
    char ch;  
    scanf("%c",&ch);  
    if(ch=='#'){  
        T=NULL;  
        return;  
    }  
    else{  
        T=(BiTree)malloc(sizeof(BiTNode));  
        if(!T)exit(1);  
        T->data=ch;  
        CreateBiTree(T->lchild);  
        CreateBiTree(T->rchild);  
    }  
}  
void InOrderTraverse(BiTree T){     
    PLinkStack plstack=createEmptyStack_link();
    if(!T){  
        printf("empty tree£¡\n");  
        return;  
    }  
    while(T||!isEmptyStack_link(plstack)){  
		while(T){  
			push_link(plstack,T); 
            T=T->lchild;  
        }
		T=pop_link(plstack); 
        printf("%c",T->data);  
        T=T->rchild;  
    }                                                                                                                                     
}  
void PreOrderTraverse(BiTree T){  
	PLinkStack plstack=createEmptyStack_link();  
    if(!T){  
        printf("empty tree£¡\n");  
        return;  
    }  
    while(T||!isEmptyStack_link(plstack)){  
        while(T){  
            push_link(plstack,T);  
            printf("%c",T->data);  
            T=T->lchild;  
        }
        T=pop_link(plstack);       
        T=T->rchild;          
    }                                                                                                                                     
}  
void PostOrderTraverse(BiTree T){
    PLinkStack plstack=createEmptyStack_link();  
    BiTree p=T;
    BiTree t;
    while(p!=NULL||!isEmptyStack_link(plstack)){
        while(p!=NULL){
        	push_link(plstack,p); 
        	p=p->lchild?p->lchild:p->rchild;
		}
		p=pop_link(plstack);
		printf("%c",p->data);
		if(plstack->top!=NULL)t=plstack->top->bnode;
		if(!isEmptyStack_link(plstack)&&t->lchild==p){
			p=t->rchild;
		}
		else p=NULL;
	}
}
PLinkStack createEmptyStack_link(void){
	PLinkStack plstack;
	plstack=(PLinkStack)malloc(sizeof(struct LinkStack));
	if(plstack!=NULL){
		plstack->top=NULL;
		printf("succeed\n");
	}
	else
		printf("out of space\n");
	return plstack;
}
int isEmptyStack_link(PLinkStack plstack){
	return (plstack->top==NULL);
}
void push_link(PLinkStack plstack,BiTree T){
	PNode p;
	p=(PNode)malloc(sizeof(struct Node));
	if(p==NULL)printf("out of space\n");
	else{
		p->bnode=T; 
		p->link=plstack->top;
		plstack->top=p;
	}
}
BiTree pop_link(PLinkStack plstack){
	PNode p;
	if(isEmptyStack_link(plstack)) printf("Empty stack pop\n");
	else{
		BiTree num;
		num=plstack->top->bnode;
		p=plstack->top;
		plstack->top=plstack->top->link;
		free(p);
		return num;
	}
}
#endif	

accept_linkTree_traverse

//ab#c###
//ab##cdf###e##
#include"lastOne.h"
int main() 
{
	BiTree a;
	int height,num;
	int in;
	cout<<"please all the elements\n";
	CreateBiTree(a);
	num=NodeCount(a);
	menu();
	while(1){
		scanf("%d",&in);
		switch(in){
		case 1:
			cout<<"recPre:";
			recPreOrderTraverse(a);
			cout<<"\n";
			break; 
		case 2:
			cout<<"recIn:";
			recInOrderTraverse(a);
			cout<<"\n";
			break; 
		case 3:
			cout<<"recPost:";
			recPostOrderTraverse(a);
			cout<<"\n";
			break; 
		case 4:
			cout<<"pre:";
			PreOrderTraverse(a);
			cout<<"\n";
			break; 
		case 5:
			cout<<"in:";
			InOrderTraverse(a);
			cout<<"\n";
			break;
		case 6:
			cout<<"post:";
			PostOrderTraverse(a);
			cout<<"\n";
			break;	
		case 7:
			cout<<"depth:";
			height=Depth(a);
			cout<<height<<endl;
			cout<<"\n";
			break;
		case 8:
			cout<<"leaves:";
			num=NodeCount(a);
			cout<<num;
			cout<<"\n";
			break;
		case 9:
			cout<<"childrenLeaves:";
			childrenLeaves(a);
			cout<<sum+1;
			cout<<"\n";
			break;
		} 
	}
	return 0;
}

    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/qq_37767282/article/details/80217183
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞