重建二叉树

来源:互联网 发布:python 开发环境 免费 编辑:程序博客网 时间:2024/05/22 17:44
</pre><pre name="code" class="cpp">#include<iostream>#include<queue>using namespace std;typedef struct  BTree{      int data;      BTree * leftchild;      BTree * rightchild;  }BTNode;  //重建树的递归算法  BTNode * ConstructCore(int * preStart, int * preEnd, int * inStart, int * inEnd){      int value = preStart[0];    BTNode * root = new BTNode();      root->data = value;      root->leftchild =NULL;    root->rightchild=NULL;     if(preStart==preEnd){        if(inStart==inEnd && *preStart==*inStart)              return root;        else            throw std::exception();      }    int * rootIndex = inStart;      while(*rootIndex != value && rootIndex <= inEnd)          rootIndex++;      if(*rootIndex != value && rootIndex==inEnd)        throw std::exception();    int left_len = rootIndex - inStart;      int *left_pre_end = preStart + left_len;      if(left_len>0)//左子树          root->leftchild = ConstructCore(preStart+1, left_pre_end, inStart, rootIndex -1);      if(left_len < preEnd-preStart)//右子树          root->rightchild = ConstructCore(left_pre_end +1, preEnd, rootIndex+1, inEnd);      return root;  }  //重建树  BTree * Construct(int * Pre, int * In, int length){      if(length<=0 || Pre==NULL || In==NULL)          return NULL;      return ConstructCore(Pre, Pre+length-1, In, In+length-1);  }  /*先序遍历*/void PreviousOrder(BTree *root){     if(root==NULL)         return;     cout<<root->data<<" ";     PreviousOrder(root->leftchild);     PreviousOrder(root->rightchild);}/*中序遍历*/void MiddleOrder(BTree *root){     if(root==NULL)         return;     PreviousOrder(root->leftchild);     cout<<root->data<<" ";     PreviousOrder(root->rightchild);}/*后序遍历*/  void PostOrder(BTree *root)  {      if(root == NULL)          return;      //左子树      if(root->leftchild != NULL)          PostOrder(root->leftchild);      //右子树      if(root->rightchild != NULL)          PostOrder(root->rightchild);      //根      cout<<root->data<<' ';  }  /*层序遍历*/void LevelTraverse(BTree *root){     if(root==NULL)        return;     queue<BTNode*> q;     q.push(root);     while(!q.empty()){         BTNode * pNode = q.front();         cout<<q.front()->data<<' ';         q.pop();         if(pNode->leftchild != NULL)             q.push(pNode->leftchild);         if(pNode->rightchild != NULL)             q.push(pNode->rightchild);      }     return;} /*二叉树层数*/int TreeDepth(BTree *root){     if(root==NULL)         return 0;     return TreeDepth(root->leftchild) > TreeDepth(root->rightchild) ? (TreeDepth(root->leftchild)+1) : (TreeDepth(root->rightchild)+1);}/*叶子节点个数*/int LeafNodeNum(BTree *root){    if(root==NULL)        return 0;    if(root->leftchild==NULL && root->leftchild==NULL)        return 1;    int leftLeaf = LeafNodeNum(root->leftchild);//左子树叶节点个数     int rightLeaf= LeafNodeNum(root->rightchild);//右子树叶节点个数     return leftLeaf+rightLeaf;} /*二叉树第K层节点个数*/int GetKthLevelNode(BTree *root,int k){    if(root==NULL || k < 1)        return 0;    if(k==1)        return 1;    int leftNum = GetKthLevelNode(root->leftchild,k-1);//左孩子第k-1层节点个数     int rightNum= GetKthLevelNode(root->rightchild,k-1);//右孩子第k-1层节点个数     return leftNum+rightNum;} /*二叉树的镜像*/BTree * MirrorTree(BTree *root){      if(root==NULL)          return NULL;      BTree * leftMirror = MirrorTree(root->leftchild);      BTree * rightMirror= MirrorTree(root->rightchild);      root->leftchild = rightMirror;      root->rightchild = leftMirror;      return root;} int main(){    int Pre[8] = {1,2,4,7,3,5,6,8};    int In[8] =  {4,7,2,1,5,3,8,6};    BTree* root = Construct(Pre, In, 8);    PostOrder(root);    cout<<endl;    PreviousOrder(root);    cout<<endl;    MiddleOrder(root);    cout<<endl;    LevelTraverse(root);    cout<<endl;    cout<<"Tree Depth is: "<<TreeDepth(root)<<endl;    cout<<"Num of Tree's leaves are: "<<LeafNodeNum(root)<<endl;    cout<<"第4层的节点个数为:"<<GetKthLevelNode(root, 4)<<endl;     MirrorTree(root);    LevelTraverse(root);    cout<<endl;    PreviousOrder(root);    system("pause");}
0 0
原创粉丝点击