#include <cstdio> #include <cstdlib> #include <cstring> char ch; typedef struct BitNode{ char data; struct BitNode *lchid, *rchid; }BitNode, *BitTree; void Create_BitTree(BitTree &T){ if(ch == ' ') return; scanf("%c", &ch); if(ch == '#') T = NULL; else{ T=(BitTree)malloc(sizeof(BitNode)); T->data=ch; Create_BitTree(T->lchid); Create_BitTree(T->rchid); } } void Pre_Order(BitTree &T){ if(T) { printf("%c ", T->data); Pre_Order(T->lchid); Pre_Order(T->rchid); } } void In_Order(BitTree &T){ if(T){ In_Order(T->lchid); printf("%c ", T->data); In_Order(T->rchid); } } void Post_Order(BitTree &T){ if(T){ Post_Order(T->lchid); Post_Order(T->rchid); printf("%c ", T->data); } } int TreeNode(BitTree &T){ if(T){ return TreeNode(T->lchid) + TreeNode(T->rchid) + 1; } return 0; } int Leaf(BitTree &T){ if(T==NULL) return 0; else if(!T->lchid&&!T->rchid) return 1; else return Leaf(T->lchid) + Leaf(T->rchid); } int BitTreeDepth(BitTree &T){ int m, n; if(!T) return 0; else{ m = BitTreeDepth(T->lchid); n = BitTreeDepth(T->rchid); if(m > n) return(m + 1); else return(n+1); } } // XAC#D##B##EF### int main(){ BitTree T; Create_BitTree(T); printf("PreOrder: "); Pre_Order(T); printf(" InOrder "); In_Order(T); printf(" PostOrder "); Post_Order(T); printf(" TreeNode num: %d ", TreeNode(T)); printf(" BitTree Depth: %d ", BitTreeDepth(T)); printf(" BitTree Leaf: %d ", Leaf(T)); return 0; }