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
    
    #include <vector>
    
    struct TreeNode 
    {
        int                       m_nValue;    
        std::vector<TreeNode*>    m_vChildren;    
    };
    
    __declspec( dllexport ) TreeNode* CreateTreeNode(int value);
    __declspec( dllexport ) void ConnectTreeNodes(TreeNode* pParent, TreeNode* pChild);
    __declspec( dllexport ) void PrintTreeNode(const TreeNode* pNode);
    __declspec( dllexport ) void PrintTree(const TreeNode* pRoot);
    __declspec( dllexport ) void DestroyTree(TreeNode* 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 "Tree.h"
    
    TreeNode* CreateTreeNode(int value)
    {
        TreeNode* pNode = new TreeNode();
        pNode->m_nValue = value;
    
        return pNode;
    }
    
    void ConnectTreeNodes(TreeNode* pParent, TreeNode* pChild)
    {
        if(pParent != nullptr)
        {
            pParent->m_vChildren.push_back(pChild);
        }
    }
    
    void PrintTreeNode(const TreeNode* pNode)
    {
        if(pNode != nullptr)
        {
            printf("value of this node is: %d.
    ", pNode->m_nValue);
    
            printf("its children is as the following:
    ");
            std::vector<TreeNode*>::const_iterator i = pNode->m_vChildren.begin();
            while(i < pNode->m_vChildren.end())
            {
                if(*i != nullptr)
                    printf("%d	", (*i)->m_nValue);
            }
    
            printf("
    ");
        }
        else
        {
            printf("this node is nullptr.
    ");
        }
    
        printf("
    ");
    }
    
    void PrintTree(const TreeNode* pRoot)
    {
        PrintTreeNode(pRoot);
    
        if(pRoot != nullptr)
        {
            std::vector<TreeNode*>::const_iterator i = pRoot->m_vChildren.begin();
            while(i < pRoot->m_vChildren.end())
            {
                PrintTree(*i);
                ++i;
            }
        }
    }
    
    void DestroyTree(TreeNode* pRoot)
    {
        if(pRoot != nullptr)
        {
            std::vector<TreeNode*>::iterator i = pRoot->m_vChildren.begin();
            while(i < pRoot->m_vChildren.end())
            {
                DestroyTree(*i);
                ++i;
            }
    
            delete pRoot;
        }
    }
  • 相关阅读:
    使用EF进行简单的增删改查
    观察者模式(委托事件的小应用)
    lambda表达式和表达式树
    socket知识总结
    xml读写Demo
    winfrom DataGridView Demo
    6月26号.NET面试题(程序题部分)只要做懂这3道题肯定能脱离菜鸟称号!
    多线程与异步编程知识简单总结
    15年6月14号面试中没有回答出来的问题
    2020.5.15记一次阿里电话面试经历
  • 原文地址:https://www.cnblogs.com/music-liang/p/12610811.html
Copyright © 2011-2022 走看看