博客 / 詳情

返回

C++ 構建並複製二叉樹

原文鏈接

使用C++構建一個二叉樹並複製、輸出。

程序

#include <stdio.h>
#include <stdlib.h>
//#include <cstdio>
#include <vector>
#include<iostream>
#include <stack> 
#include<cstdlib>

#include <string>
using namespace std;



struct TreeNode          // 定義二叉樹
{
    int val;                       // 當前節點值用val表示
    struct TreeNode *left;     // 指向左子樹的指針用left表示
    struct TreeNode *right;    // 指向右子樹的指針用right表示
    TreeNode(int x) :val(x), left(NULL), right(NULL) { } // 初始化當前結點值為x,左右子樹為空
};


//創建樹
TreeNode* insert(TreeNode* tree, int value)
{
    
    TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); // 創建一個節點
    node->val = value;      // 初始化節點                            // malloc函數可以分配長度
    node->left = NULL;
    node->right = NULL;

    TreeNode* temp = tree;      // 從樹根開始
    while (temp != NULL)
    {
        if (value < temp->val)  // 小於根節點就進左子樹
        {
            if (temp->left == NULL)
            {
                temp->left = node;  // 新插入的數為temp的左子樹
                return tree;
            }
            else           // 下一輪判斷
                temp = temp->left;
        }
        else           // 否則進右子樹
        {    

            if (temp->right == NULL)
            {
                temp->right = node;  // 新插入的數為temp的右子樹
                return tree;
            }
            else           // 下一輪判斷
                temp = temp->right;
        }
    }
    return tree;
}
 
//  ************* 輸出圖形二叉樹 *************
void output_impl(TreeNode* n, bool left, string const& indent)
{
    if (n->right)
    {
        output_impl(n->right, false, indent + (left ? "|     " : "      "));
    }
    cout << indent;
    cout << (left ? '\\' : '/');
    cout << "-----";
    cout << n->val << endl;
    if (n->left)
    {
        output_impl(n->left, true, indent + (left ? "      " : "|     "));
    }
}
void output(TreeNode* root)
{
    if (root->right)
    {
        output_impl(root->right, false, "");
    }
    cout << root->val << endl;
    if (root->left)
    {
        output_impl(root->left, true, "");
    }
    system("pause");
}
//  ******************************************



void CopyBiTree(TreeNode* root, TreeNode* newroot) //  複製二叉樹
{
    if (root == nullptr)
        return;
    else
    {
        newroot->val = root->val;
        if (root->left != nullptr)
            newroot->left = new TreeNode(0);
        if (root->right != nullptr)
            newroot->right = new TreeNode(0);

        CopyBiTree(root->left, newroot->left);
        CopyBiTree(root->right, newroot->right);
    }
    //output(newroot);
}


// ====================測試代碼====================
int main()
{
    TreeNode* tree =new TreeNode(10);       // 樹的根節點
    TreeNode* treeresult;

    treeresult = insert(tree, 6);         // 輸入n個數並創建這個樹
    treeresult = insert(tree, 4);
    treeresult = insert(tree, 8);
    treeresult = insert(tree, 14);
    treeresult = insert(tree, 12);
    treeresult = insert(tree, 16);

    TreeNode* mirroot = new TreeNode(10);
    CopyBiTree(treeresult, mirroot); //  複製二叉樹
    output(treeresult);         //  輸出原二叉樹
    output(mirroot);         //  輸出複製的二叉樹


}

結果

二叉樹1.png

學習更多編程知識,請關注我的公眾號:

代碼的路

公眾號二維碼.jpg

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.