An STL like Tree Class

简介: 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.
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.

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.

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

目录
相关文章
|
2月前
|
缓存 索引
图解B Tree和B+ Tree
图解B Tree和B+ Tree
28 0
|
6月前
|
存储
数据结构之二叉查找树(Binary Search Tree)和红黑树(Red Black Tree)
二叉查找树又可以称之为 : 二叉搜索树 , 二叉排序树 , 它或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不空,则左子树上所有结点的值均小于它的根节点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉排序树。二叉搜索树作为一种经典的数据结构,它既有链表的快速插入与删除操作的特点,又有数组快速查找的优势 , 下图中这棵树,就是一棵典型的二叉查找树
110 1
|
2月前
|
编译器 容器
简易实现 STL--list
简易实现 STL--list
|
8月前
|
存储 C语言 C++
C++ STL list
上次我们详细的介绍了vector,今天我们继续来介绍一下TSTL中的另外一个容器list。list在基础的功能和结构上就是一个双向带头的循环链表,实现起来基本不难,但是list迭代器的封装是非常值得学习的。
|
10月前
|
存储 前端开发 数据库
飘乙己:List转Tree有4种写法!
飘乙己:List转Tree有4种写法!
81 1
|
10月前
|
存储 搜索推荐 C++
C++【STL】之list的使用
C++ STL list类常用接口详细讲解,干货满满!
59 0
C++【STL】之list的使用
|
算法 C++ 容器
【C++】STL——list的使用
【C++】STL——list的使用
【C++】STL——list的使用
|
安全 API C++
STL源码分析--vector
STL源码分析--vector
145 0
STL源码分析--vector