zoukankan      html  css  js  c++  java
  • 数据结构--二叉树

    /*******************************************************************
    Copyright(c) 2016, Harry He
    All rights reserved.
    Distributed under the BSD license.
    (See accompanying file LICENSE.txt at
    https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
    *******************************************************************/
    
    //==================================================================
    // 《剑指Offer——名企面试官精讲典型编程题》代码
    // 作者:何海涛
    //==================================================================
    
    #pragma once
    
    struct BinaryTreeNode 
    {
        int                    m_nValue; 
        BinaryTreeNode*        m_pLeft;  
        BinaryTreeNode*        m_pRight; 
    };
    
    __declspec( dllexport ) BinaryTreeNode* CreateBinaryTreeNode(int value);
    __declspec( dllexport ) void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
    __declspec( dllexport ) void PrintTreeNode(const BinaryTreeNode* pNode);
    __declspec( dllexport ) void PrintTree(const BinaryTreeNode* pRoot);
    __declspec( dllexport ) void DestroyTree(BinaryTreeNode* pRoot);
    /*******************************************************************
    Copyright(c) 2016, Harry He
    All rights reserved.
    Distributed under the BSD license.
    (See accompanying file LICENSE.txt at
    https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
    *******************************************************************/
    
    //==================================================================
    // 《剑指Offer——名企面试官精讲典型编程题》代码
    // 作者:何海涛
    //==================================================================
    
    #include <cstdio>
    #include "BinaryTree.h"
    
    BinaryTreeNode* CreateBinaryTreeNode(int value)
    {
        BinaryTreeNode* pNode = new BinaryTreeNode();
        pNode->m_nValue = value;
        pNode->m_pLeft = nullptr;
        pNode->m_pRight = nullptr;
    
        return pNode;
    }
    
    void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
    {
        if(pParent != nullptr)
        {
            pParent->m_pLeft = pLeft;
            pParent->m_pRight = pRight;
        }
    }
    
    void PrintTreeNode(const BinaryTreeNode* pNode)
    {
        if(pNode != nullptr)
        {
            printf("value of this node is: %d
    ", pNode->m_nValue);
    
            if(pNode->m_pLeft != nullptr)
                printf("value of its left child is: %d.
    ", pNode->m_pLeft->m_nValue);
            else
                printf("left child is nullptr.
    ");
    
            if(pNode->m_pRight != nullptr)
                printf("value of its right child is: %d.
    ", pNode->m_pRight->m_nValue);
            else
                printf("right child is nullptr.
    ");
        }
        else
        {
            printf("this node is nullptr.
    ");
        }
    
        printf("
    ");
    }
    
    void PrintTree(const BinaryTreeNode* pRoot)
    {
        PrintTreeNode(pRoot);
    
        if(pRoot != nullptr)
        {
            if(pRoot->m_pLeft != nullptr)
                PrintTree(pRoot->m_pLeft);
    
            if(pRoot->m_pRight != nullptr)
                PrintTree(pRoot->m_pRight);
        }
    }
    
    void DestroyTree(BinaryTreeNode* pRoot)
    {
        if(pRoot != nullptr)
        {
            BinaryTreeNode* pLeft = pRoot->m_pLeft;
            BinaryTreeNode* pRight = pRoot->m_pRight;
    
            delete pRoot;
            pRoot = nullptr;
    
            DestroyTree(pLeft);
            DestroyTree(pRight);
        }
    }
  • 相关阅读:
    6年工作总结反思
    中级美语 L013:Health Comes First 解析
    中级美语 L011:Power without Pollution 解析
    中级美语 L009:Be Thoughtful 解析
    中级美语 L007:Doctor Death 解析
    中级美语 L005:Bungee jumping 解析
    中级美语 L003:The City of Song 解析
    中级美语 L001:Rome Wasn't Built in a Day 解析
    初级美语 L147:Television Addiction 解析
    初级美语 L145:Billy's Goal in Life 解析
  • 原文地址:https://www.cnblogs.com/music-liang/p/12610826.html
Copyright © 2011-2022 走看看