二叉树的一些算法实现 // test12.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #define N 10 using namespace std; typedef struct node { struct node * l_child,* r_child; int tag; int data; }node,* p_node; void mid_order(p_node root){//中根遍历 p_node stack[N]; int top=0; while(top||root){ if(root){ stack[top++]=root; root=root->l_child; } else{ cout<<stack[top-1]->data<<" "; root=stack[--top]->r_child; } } } void pre_order(p_node root){//先根遍历 p_node stack[N]; int top=0; while(top||root){ if(root){ cout<<root->data<<" "; stack[top++]=root; root=root->l_child; } else root=stack[--top]->r_child; } } void post_order2(p_node root){//后根遍历 typedef struct tag_node{ p_node node; int tag; }tag_node,*p_tag; tag_node stack[N]; int top=0; while(top||root){ if(root){ stack[top].node=root; stack[top++].tag=0; root=root->l_child; } else{ if(stack[top-1].tag) cout<<stack[--top].node->data<<" "; else{ stack[top-1].tag=1; root=stack[top-1].node->r_child; } } } } void level_order(p_node root){//普通的层次遍历 p_node queue[N]; int begin=0; int end=0; p_node temp; if(root) queue[end++]=root; while(begin!=end){ temp=queue[begin++]; cout<<temp->data<<" "; if(temp->l_child) queue[end++]=temp->l_child; if(temp->r_child) queue[end++]=temp->r_child; } } void advanced_level_order(p_node root){//分层的层次遍历 p_node queue[N]; int cur=0,last=0,end=0,level=0; if(root){ queue[end++]=root; last++; } while(cur!=end){ cout<<"level "<<level++<<": "; while(cur!=last){ p_node temp=queue[cur++]; cout<<temp->data<<" "; if(temp->l_child) queue[end++]=temp->l_child; if(temp->r_child) queue[end++]=temp->r_child; } last=end; cout<<endl; } } int level_order(p_node root,int level){//递归遍历二叉树第level层 if(!root) return 0; if(level==1){ cout<<root->data<<" "; return level-1; } else return level_order(root->l_child,level-1)* level_order(root->r_child,level-1); } int count_level1(p_node root){//递归求二叉树深度 if(!root) return 0; int l_depth=count_level1(root->l_child)+1; int r_depth=count_level1(root->r_child)+1; return l_depth>r_depth?l_depth:r_depth; } int count_level(p_node root){//求深度的牛逼写法 return root? (count_level(root->l_child)+1>count_level(root->r_child)+1? count_level(root->l_child)+1:count_level(root->r_child)+1):0; } p_node create_tree(){//创建树 p_node root=(p_node)malloc(sizeof(node)); root->l_child=NULL; root->r_child=NULL; int input; cin>>input; if(!input) root=NULL; else{ root->data=input; root->l_child=create_tree(); root->r_child=create_tree(); } return root; } p_node level_create(){//二叉树的层次建立 p_node array[N]; int cur=1; int input; cin>>input; while(input){ p_node temp=(p_node)malloc(sizeof(node)); temp->data=input; temp->l_child=temp->r_child=NULL; array[cur++]=temp; int j=(cur-1)/2; if(j&&((cur-1)%2)) array[j]->r_child=temp; if(j&&(!((cur-1)%2))) array[j]->l_child=temp; cin>>input; } return array[1]; } void main(int argc, char* argv[]) { //post_order2(create_tree()); p_node root=create_tree(); advanced_level_order(root); cout<<"/n深度为: "; cout<<count_level(root)<<" "; }