An STL like Tree Class

系统 1511 0
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again.
<!-- Main Page Contents Start --><!-- Article Starts -->

Sample Image

Introduction

For more than a year and a half I have had to use some kind of multi-node tree to do something. However, all I found from the Internet are binary trees or equivalents. I therefore decided to write my own. Because I had to finish it pretty fast, I spent only two days for it, so it may be buggy.

In the project, it has three classes working like in STL container-iterator style.
Collapse Copy Code
      
        template
      
      
        <
      
      
        class
      
       Key, 
      
        class
      
       T
      
        >
      
      
        class
      
       Tree

      
        template
      
      
        <
      
      
        class
      
       Key, 
      
        class
      
       T
      
        >
      
      
        class
      
       Tree_iterator

      
        template
      
      
        <
      
      
        class
      
       Key, 
      
        class
      
       T
      
        >
      
      
        class
      
       TreeNode
    

A tree contains many tree nodes, while tree nodes contain data. Tree nodes of a tree are called children. In the design each node is assigned a level and the root node is of level 1. Tree_iterator is for navigation within the tree. Two tree-walk styles are defined. One walks down from the root node, while the other one use post-style to walk through all children of the tree. For each walk action, a tree_iterator is returned, so the user can use the iterator to reference the tree node and get the data.

I wrapped all the classes under the namespace Tiffany. For example, declaration of the tree with key type of wstring , and node data type of string, and add two levels of element is like:

Collapse Copy Code
      Tiffany::Tree
      
        <
      
      wstring, 
      
        string
      
      
        >
      
       x(L
      
        "
      
      
        Node 1"
      
      , 
      
        "
      
      
        Root"
      
      );

px = x.AddChild(L
      
        "
      
      
        Key1"
      
      ,
      
        "
      
      
        A"
      
      );
px1 = x.AddChild(px, L
      
        "
      
      
        Key2"
      
      , 
      
        "
      
      
        1"
      
      );

    

For a walk down action, you can limit it to a part of a tree ("sub-tree") by using the function SetSubTreePivot(px) , and you can set the pivot to point back to the root node by calling SetWalkDownRootPivot. Try it out!. You can even delete part of the tree by calling DelSubTree() .

Unfortunately I didn't have much time to prepare this document, but it should be obvious how to the classes.

<!-- Main Page Contents End -->

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

An STL like Tree Class


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论