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. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again.

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.

CollapseCopy 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:

CollapseCopy 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

时间: 2024-07-30 06:06:55

An STL like Tree Class的相关文章

详细解说STL hash_map系列 〔转载〕

详细解说STL hash_map系列 0 为什么需要hash_map 1 数据结构:hash_map原理 2 hash_map 使用 2.1 一个简单实例 2.2 hash_map 的hash函数 2.3 hash_map 的比较函数 2.4 hash_map 函数 3 相关hash容器 4 其他 4.1 hash_map和map的区别在哪里? 4.2 什么时候需要用hash_map,什么时候需要用map? 4.3 如何在hash_map中加入自己定义的类型? 4.4如何用hash_map替换程

STL学习小结 .

from:http://blog.csdn.net/byxdaz/article/details/4633826#comments STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些"容器"有list, vector,set,map等,STL也是算法和其它一些组件的集合.这里的"容器"和算法的集合指的是世界上很多聪明人很多年的杰作

关于STL中set容器的一些总结_C 语言

1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作.vector封装数组,list封装了链表,map和set封装了二叉树等,在封装这些数据结构的时候,STL按照程序员的使用习惯,以成员函数方式提供的常用操作,如:插入.排序.删除.查找等.让用户在STL使用过程中,并不会感到陌生. 关于set,必须说明的是set关联式容器.set作为一个容器也是

stl2pov 2.5.0发布 一个读取STL文件的工具

stl2pov是一个读取STL文件的工具,并可以输出POVray网格.已测试通过Pro/Engineer生成的STL文件. stl2pov 2.5.0对于一个错误的二进制STL文件阅读进行了修正,面是正常的第一个顶点问题.当写入POV -ray文件时,正确坐标系的转换与应用(交换x和y坐标). 2.5.0: Bug fixes. Do a coordinate transformation to fix the mirrored part bug when writing POV files.

STL中的set容器的一点总结

1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作.vector封装数组,list封装了链表,map和set封装了二叉树等,在封装这些数据结构的时候,STL按照程序员的使用习惯,以成员函数方式提供的常用操作,如:插入.排序.删除.查找等.让用户在STL使用过程中,并不会感到陌生. 关于set,必须说明的是set关联式容器.set作为一个容器也是

STL之Map

概述 Map是标准关联式容器(associative container)之一,一个map是一个键值对序列,即(key ,value)对.它提供基于key的快速检索能力,在一个map中key值是唯一的.map提供双向迭代器,即有从前往后的(iterator),也有从后往前的(reverse_iterator). map要求能对key进行<操作,且保持按key值递增有序,因此map上的迭代器也是递增有序的.如果对于元素并不需要保持有序,可以使用hash_map. map中key值是唯一的,如果马匹

c++,关于stl的应用,定义map

问题描述 c++,关于stl的应用,定义map 我想定义一个map容器,key是一个string,值是一个指向vector的指针,但是不同的string会对应不同类型的vector,这就不好定义了.求帮忙解决 解决方案 似乎没有很好的解决方案,因为stl是泛型,类型参数不同,就相当于两个不同的类型,不能存储在一个容器中.下面是一个勉强能用的方法,供你参考:可以用map<string void*>,然后再对void*强制类型转换,得到你想要的指针.前提是你已经知道每个string对应什么类型的指

treegrid-Ext.ux.tree.TreeGrid的默认排序如何改为中文?

问题描述 Ext.ux.tree.TreeGrid的默认排序如何改为中文? 我用Ext的treeGrid,它的排序默认是英文的,是这样的效果"sortAscText.sortDescText.columnsText",如何将这个改为中文显示,效果是"正序排列.倒序排列.显示/隐藏列"? 求大神解答.

STL学习笔记二(仿函式)

简介:<functional>文件包含的大量的模版类unary_function,binary_function,plus,miuns, multiplies, divides, modulus, negate, equal_to, not_equal_to, greater, less, greater_equal, less_equal, logical_and, logical_or, logical_not, unary_negate, binary_negate,binder1st,