C++樹的實現

?C++樹的實現

STL里面沒有提供容器樹的模板實現,自已寫一個:
Tree.h

//tree.h 頭文件 #include <list> #include <algorithm> using namespace std; struct TreeNode; //定義一個結構體原型 classTree; //定義一個類原型 classIterator; //定義一個類原型 typedef list<TreeNode*> List; //重命名一個節點鏈表 TreeNode* clone(TreeNode*,List&,TreeNode*);//Clone復制函數 struct TreeNode{ int_data; //數據 TreeNode* _parent; //父節點 List_children; //子節點 TreeNode(int,TreeNode*); //構造函數 void SetParent(TreeNode&); //設置父節點 void InsertChildren(TreeNode&); //插入子節點 }; classTree{ public: //下面是構造器和運算符重載 Tree(); //默認構造函數 Tree(constTree&); //復制構造函數 Tree(constint); //帶參數構造函數 Tree(constint,constlist<Tree*>&);//帶參數構造函數 ~Tree(); //析構函數 Tree& operator=(constTree&); //=符號運算符重載 bool operator==(constTree&); //==符號運算符重載 bool operator!=(constTree&); //!=符號運算符重載 //下面是成員函數 void Clear(); //清空 boolIsEmpty()const; //判斷是否為空 intSize()const; //計算節點數目 intLeaves(); //計算葉子數 intRoot()const; //返回根元素 intHeight(); //計算樹的高度 //下面是靜態成員函數 static boolIsRoot(Iterator); //判斷是否是根 static boolisLeaf(Iterator); //判斷是否是葉子 static IteratorParent(Iterator); //返回其父節點 static intNumChildren(Iterator); //返回其子節點數目 //跌代器函數 Iteratorbegin(); //Tree Begin Iteratorend(); //Tree End friend classIterator; //Iterator SubClass private: list<TreeNode*> _nodes; //節點數組 list<TreeNode*>::iteratorLIt; //一個節點迭代器 intheight(TreeNode*); intlevel(TreeNode*,Iterator); }; //This is TreeSub Class Iterator classIterator{ private: Tree* _tree; //Tree data list<TreeNode*>::iterator_lit; //List Iterator public: Iterator(); //默認構造函數 Iterator(constIterator&); //復制構造函數 Iterator(Tree*,TreeNode*); //構造函數 Iterator(Tree*,list<TreeNode*>::iterator);//構造函數 //運算符重載 void operator=(constIterator&); //賦值運算符重載 bool operator==(constIterator&); //關系運算符重載 bool operator!=(constIterator&); //關系運算符重載 Iterator& operator++(); //前綴++運算符 Iterator operator++(int); //后綴++運算符 int operator*()const; //獲得節點信息 bool operator!(); //賦值運算符重載 typedef list<TreeNode*>::iteratorList; friend classTree; }; ?

Tree.cpp

//tree.cpp 實現文件 #include "Tree.h" //***** 下面是對于TreeNode結構體的定義實現*****/// TreeNode::TreeNode(inttype= 0,TreeNode* Parent = 0){ _data = type; _parent = Parent; } void TreeNode::SetParent(TreeNode& node){ _parent = &node; } void TreeNode::InsertChildren(TreeNode& node){ TreeNode* p = &node; _children.push_back(p); } //***** 下面是對于Tree類的定義實現*****/// Tree::Tree(){ } Tree::Tree(constinttype){ _nodes.push_back(new TreeNode(type)); } Tree::Tree(constTree& t){ if(t._nodes.empty())return; clone(t._nodes.front(),_nodes,0); } Tree::Tree(constinttype,constlist<Tree*>& lit){ TreeNode* root = new TreeNode(type);//建立根節點 _nodes.push_back(root);//放入樹中 list<Tree*>::const_iteratorit; for(it = lit.begin();it!=lit.end();it++){ if(!((*it)->_nodes.empty())){//如果當前節點元素不為空 Tree* tp = newTree(**it); TreeNode* p = tp->_nodes.front(); root->_children.push_back(p); //設置根的子節點 p->_parent = root; //設置節點的父節點為根 list<TreeNode*>::iteratorlit1 = tp->_nodes.begin(); list<TreeNode*>::iteratorlit2 = tp->_nodes.end(); list<TreeNode*>::iteratorlit3 = _nodes.end(); _nodes.insert(lit3,lit1,lit2); } } } Tree::~Tree(){ for(list<TreeNode*>::iteratorit = _nodes.begin();it!=_nodes.end();it++){ delete* it; } } Tree& Tree::operator =(constTree & t){ Clear(); Tree* p = newTree(t); _nodes = p->_nodes; return *this; } boolTree::operator ==(constTree& t){ if(_nodes.size()!=t._nodes.size()){ return false; } list<TreeNode*>::iteratorit = _nodes.begin(); list<TreeNode*>::const_iterator_it = t._nodes.begin(); while(it!=_nodes.end()&&_it!=t._nodes.end()){ if((*it)->_data!=(*_it)->_data){ return false; } it++; _it++; } return true; } boolTree::operator !=(constTree& t){ if(_nodes.size()!=_nodes.size()){ return true; } else{ list<TreeNode*>::iteratorit = _nodes.begin(); list<TreeNode*>::const_iterator_it = t._nodes.begin(); while(it!=_nodes.end()&&_it!=t._nodes.end()){ if((*it)->_data!=(*_it)->_data){ return true; } it++; _it++; } return false; } } void Tree::Clear(){ for(list<TreeNode*>::iteratorit = _nodes.begin();it!=_nodes.end();it++){ delete* it; } _nodes.clear(); } boolTree::IsEmpty()const{ return _nodes.empty(); } intTree::Size()const{ return (int)_nodes.size(); } intTree::Leaves(){ inti = 0; list<TreeNode*>::iteratorit = _nodes.begin(); while(it!=_nodes.end()){ if((*it)->_children.size()==0){ i++; } it++; } return i; } intTree::Height(){ if(_nodes.size()!=0){ TreeNode* TNode = _nodes.front(); return height(TNode); } else{ return -1; //判斷為空樹 } } intTree::height(TreeNode* node){ if(!node){ return -1; } else{ list<TreeNode*> plist = node->_children; if(plist.size()==0){ return 0; } inthA = 0; for(list<TreeNode*>::iteratorit = plist.begin();it!=plist.end();it++){ inthB = height(*it); if(hB>hA){ hA = hB; } } return hA+1; } } IteratorTree::begin(){ return Iterator(this,_nodes.begin()); } IteratorTree::end(){ return Iterator(this,_nodes.end()); } intTree::Root()const{ return (*_nodes.begin())->_data; } boolTree::IsRoot(Iteratorit){ TreeNode p = *it; if(p._parent == 0){ return true; } return false; } boolTree::isLeaf(Iteratorit){ TreeNode p = *it; if(p._children.size() == 0){ return true; } return false; } IteratorTree::Parent(Iteratorit){ TreeNode p = *it; Tree* t = it._tree; IteratorIte(t,p._parent); return Ite; } intTree::NumChildren(Iteratorit){ TreeNode p = *it; return (int)p._children.size(); } //***** 下面是對于Tree::Iterator類的定義實現*****/// Iterator::Iterator(){ } Iterator::Iterator(constIterator& it){ _tree = it._tree; _lit = it._lit; } Iterator::Iterator(Tree* t, TreeNode* n){ _tree = t; list<TreeNode*>& nodes = _tree->_nodes; _lit = find(nodes.begin(),nodes.end(),n);//<algorithm> Members } Iterator::Iterator(Tree * t, list<TreeNode*>::iteratorlt){ _tree = t; _lit = lt; } void Iterator::operator =(constIterator& it){ _tree = it._tree; _lit = it._lit; } boolIterator::operator ==(constIterator & it){ return _tree == it._tree && _lit == it._lit; } boolIterator::operator !=(constIterator & it){ return _tree != it._tree || _lit != it._lit; } Iterator& Iterator::operator ++(){ ++_lit; return *this; } IteratorIterator::operator ++(int){ Iteratorit(*this); ++_lit; return it; } intIterator::operator *() const{ return ((*_lit)->_data); } boolIterator::operator !(){ return _lit == _tree->_nodes.end(); } //Clone函數 TreeNode* clone(TreeNode* node,List& nodes,TreeNode* nodep){ TreeNode* cp = new TreeNode(node->_data,nodep); nodes.push_back(cp); List& l = node->_children; List& cl = cp->_children; for(list<TreeNode*>::iteratorlt = l.begin();lt!=l.end();lt++){ cl.push_back(clone(*lt,nodes,cp)); } return cp; }?

轉載于:https://www.cnblogs.com/xieyunc/archive/2009/04/30/2793611.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/276572.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/276572.shtml
英文地址,請注明出處:http://en.pswp.cn/news/276572.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

加密文件忘記密碼怎么解密_MyBatis 配置文件 用戶密碼加密存儲

properties配置文件一般是使用properties保存配置文件內容,然后在mybatis配置文件中進行讀取在resource文件下新建db.properties文件內容如下# 數據庫配置文件 driver com.mysql.cj.jdbc.Driver url jdbc:mysql:// /mybatis username password 然后,接著把文件放入源碼包…

PHP,如何防止同一用戶同一時間多次登錄

轉載鏈接&#xff1a;http://blog.sina.com.cn/s/blog_4832ea590101djnp.html PHP&#xff0c;如何防止同一用戶同一時間多次登錄&#xff1f; 創建表 username password sessionId 張三 123456 ksw9dkw9ksl92w3 備注&#xff1a;用戶…

科技前沿智能創新 2019北京智能家居 全屋智能博覽會

2019北京智能家居大型展覽會 2019北京全屋智能家居博覽會報道布展&#xff1a;2019年6月26日-27日 展會開幕&#xff1a;2019年6月28日上午9&#xff1a;00時展會交易&#xff1a;2019年6月28日-30日 展會撤展&#xff1a;2019年6月30日下午14&#xff1a;00時 展覽會在北京市政…

java 容器_我也來聊聊,JAVA容器與迭代器

java的容器與迭代器是一個老生常談的話題了。本文旨在與大家分享一些關于雙向鏈表與迭代器的運用小技巧&#xff0c;并希望本篇文章的內容能夠在項目中給你帶來幫助。Stack與LinkedListStack是一個LIFO(后進先出)的容器。若要在java中定義一個Stack應該怎么辦&#xff1f;也許你…

VS2005調試時變慢解決辦法

vs2005生成代碼以及調試運行時&#xff0c;如果設置斷點系統運行非常緩慢&#xff0c;從網上查閱一些資料后解決&#xff1a; 主要解決辦法是&#xff1a; 打開VS2005菜單項"工具"---->"導入導出設置"----->"重置所有設置" 本文參考:http:…

apache目錄的訪問控制

轉載鏈接&#xff1a;http://blog.sina.com.cn/s/blog_7be8a2150100trml.html 1.根目錄的訪問控制 DocumentRoot "/var/www/html" <Directory /> Options FollowSymLinks AllowOverride None </Directory> 解釋一下&#xff1a; <Dir…

廣東高院駁回快播對深圳市場監管局2.6億罰款案上訴

雷帝網 樂天 12月29日報道據廣東高院官方微信消息&#xff0c;廣東省高級人民法院對深圳市快播科技有限公司&#xff08;簡稱快播&#xff09;訴深圳市市場監督管理局&#xff08;簡稱市場監管局&#xff09;著作權行政處罰糾紛案作出終審宣判&#xff0c;駁回上訴&#xff0c;…

【Vegas原創】恢復Oracle Package的笨方法

imp沒有恢復package的參數&#xff0c;所以&#xff0c;只能用笨辦法&#xff1a;rowsn&#xff0c;只導入表結構和物件。 步驟&#xff1a; 1&#xff0c;dbca新建一個test數據庫&#xff1b; 2&#xff0c;新增user&#xff0c;表空間&#xff0c;給user賦予權限 3&#xff0…

python enumerate函數_關于python中enumerate和zip函數的用法及舉例

關于python中enumerate和zip函數的用法及舉例關于enumerate函數&#xff1a;enumerate函數可以同時返回列表或元組等可迭代對象的下標和內容&#xff0c;但實際上&#xff0c;enumerate函數實際返回的是一個enumerate類型的可迭代對象&#xff0c;下面是用法舉例&#xff1a;se…

php 解析xml 的四種方法(轉)

轉載鏈接&#xff1a;http://www.cnblogs.com/likwo/archive/2011/08/24/2151793.html XML處理是開發過程中經常遇到的&#xff0c;PHP對其也有很豐富的支持&#xff0c;本文只是對其中某幾種解析技術做簡要說明&#xff0c;包括&#xff1a;Xml parser, SimpleXML, XMLReader,…

Golang 微服務系列 go-kit(Log,Metrics,Tracing)

go-kit Log & Metrics & Tracing 微服務監控3大核心 Log & Metrics & Tracing Log Log 模塊源碼有待分析&#xff08;分析完再補上&#xff09; Metrics 主要是封裝 Metrics 接口&#xff0c;及各個 Metrics(Prometheus,InfluxDB,StatsD,expvar) 中間件的封裝。…

GDI+

載解壓GDI開發包&#xff1b; 2&#xff0e; 正確設置include & lib 目錄&#xff1b; 3&#xff0e; stdafx.h 添加&#xff1a; #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include <gdiplus.h> 4&#xff0e; 程序中添加GDI的包含文件gdip…

shell 練習3

1、編寫腳本/root/bin/createuser.sh&#xff0c;實現如下功能&#xff1a;使用一個用戶名做為參數&#xff0c;如果指定參數的用戶存在&#xff0c;就顯示其存在&#xff0c;否則添加之&#xff1b;顯示添加的用戶的id號等信息2、編寫腳本/root/bin/yesorno.sh&#xff0c;提示…

HTML5文件實現拖拽上傳

轉載鏈接&#xff1a;http://www.cnblogs.com/caonidayecnblogs/archive/2010/09/09/1821925.html 通過HTML的文件API &#xff0c;Firefox、Chrome等瀏覽器已經支持從操作系統直接拖拽文件&#xff0c;并上傳到服務器。 相對于使用了十多年的HTML表單&#xff0c;這是一個革命…

兩個數組結果相減_學點算法(三)——數組歸并排序

今天來學習歸并排序算法。分而治之歸并算法的核心思想是分而治之&#xff0c;就是將大問題轉化為小問題&#xff0c;在解決小問題的基礎上&#xff0c;再去解決大問題。將這句話套用到排序中&#xff0c;就是將一個大的待排序區間分為小的待排序區間&#xff0c;對小的排序區間…

python實習生面試題_大數據分析實習生面試題庫

原標題&#xff1a;大數據分析實習生面試題庫大數據分析是一個有吸引力的領域&#xff0c;因為它不僅有利可圖&#xff0c;而且您有機會從事有趣的項目&#xff0c;而且您總是在學習新事物。如果您想從頭開始&#xff0c;請查看大數據分析實習生面試題庫以準備面試要點。大數據…

JavaScript編程語言 基礎 (1)

問題&#xff1a;什么是web前端前端&#xff1a;指界面&#xff0c;計算機&#xff08;PC&#xff09;軟件桌面的界面&#xff1b; 計算機端的瀏覽器界面&#xff1b; 移動端的軟件&#xff08;app&#xff09;界面&#xff1b; 移動端的瀏覽器界面。HtmlcssJavaScript 使用網頁…

shell結合expect寫的批量scp腳本工具

轉載鏈接&#xff1a;http://www.jb51.net/article/34005.htm expect用于自動化地執行linux環境下的命令行交互任務&#xff0c;例如scp、ssh之類需要用戶手動輸入密碼然后確認的任務。有了這個工具&#xff0c;定義在scp過程中可能遇到的情況&#xff0c;然后編寫相應的處理語…

ASP記數器

這兩天有好幾個老的ASP網站要改&#xff0c;其中有要求加記數器&#xff0c;為圖簡單&#xff0c;就用文本文件的形式存儲記數。以前用ifream的形式嵌入&#xff0c;不能很好的控制記數器顯示的風格&#xff0c;現在改進了一下&#xff0c;可以很好的與嵌入板塊風格結合了。把做…

php利用openssl實現RSA非對稱加密簽名

轉載鏈接&#xff1a;http://liuxufei.com/weblog/jishu/376.html 1. 先用php生成一對公鑰和私鑰 $res openssl_pkey_new(); openssl_pkey_export($res,$pri); $d openssl_pkey_get_details($res); $pub $d[key]; var_dump($pri,$pub); 2. 保存好自己的私鑰&#xff0c;把公…