码迷,mamicode.com
首页 > 其他好文 > 详细

博客测试

时间:2014-07-22 23:00:54      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   width   

#include <iostream> 
#include <string> 
using namespace std; 
struct BTNode 
{ 
    int v;  // default positive Integer. 
    BTNode *pLeft; 
    BTNode *pRight; 
    BTNode(int x) : v(x), pLeft(NULL), pRight(NULL) {} 
}; 
/********************************************************/
/*****        Basic functions          ***********/
BTNode* createBinaryTree(int r) 
{ 
    BTNode *pRoot = new BTNode(r); 
    int u, v; 
    cin >> u >> v; 
    if(u != 0) 
        pRoot->pLeft = createBinaryTree(u); 
    if(v != 0) 
        pRoot->pRight = createBinaryTree(v); 
    return pRoot; 
} 
void release(BTNode *root){ 
    if(root == NULL) return; 
    release(root->pLeft); 
    release(root->pRight); 
    delete[] root; 
    root = NULL; 
} 
void print(BTNode *root, int level = 1){ 
    if(root == NULL) { cout << "NULL"; return; }; 
    string s; 
    for(int i = 0; i < level; ++i) s += "   "; 
    cout << root->v << endl << s; 
    print(root->pLeft, level+1); 
    cout << endl << s; 
    print(root->pRight, level+1); 
} 
/******************************************************************/
void mirrorTree(BTNode *root) 
{ 
    if(!root || (!root->pLeft && !root->pRight)) return; 
    /*    交?换?左ó右ò子ó树÷           */
    BTNode *pTem = root->pLeft;  
    root->pLeft = root->pRight; 
    root->pRight = pTem; 

    mirrorTree(root->pLeft); 
    mirrorTree(root->pRight); 
} 


int main(){ 
    int TestTime = 3, k = 1; 
    while(k <= TestTime) 
    { 
        cout << "Test " << k++ << ":" << endl; 

        cout << "Create a tree: " << endl; 
        BTNode *pRoot = createBinaryTree(8); 
        print(pRoot); 
        cout << endl; 

        cout << "The mirror tree: " << endl; 
        mirrorTree(pRoot); 
        print(pRoot); 
        cout << endl; 

        release(pRoot); 
    } 
    return 0; 
}

 

mamicode.com,码迷

博客测试,码迷,mamicode.com

博客测试

标签:style   blog   http   color   os   width   

原文地址:http://www.cnblogs.com/liyangguang1988/p/3702933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!