数据结构上机——二叉树
2017-11-20 14:48
351 查看
#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); la 4000 stOrder(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; }
相关文章推荐
- 【1291】数据结构上机测试4.1:二叉树的遍历与应用1 SDUTOJ
- 数据结构上机测试4.1:二叉树的遍历与应用1 (*详解
- sdutacm-数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1【根据二叉树的前序序列和中序序列求后序序列方法1,2】
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1 oj
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 1489 求二叉树的先序遍历 1291 数据结构上机测试4.1:二叉树的遍历与应用1【二叉树遍历顺序转换】
- C语言 数据结构之二叉树 数据结构上机测试4.1:二叉树的遍历与应用1
- 二叉树及其应用-二叉树的遍历及哈夫曼树-数据结构上机实验
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- SDUTOJ 1291 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用