BinaryTree.h
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供睢縣網(wǎng)站建設、睢縣做網(wǎng)站、睢縣網(wǎng)站設計、睢縣網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、睢縣企業(yè)網(wǎng)站模板建站服務,十年睢縣做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
#pragma once template <class T> struct BinaryTreeNode { BinaryTreeNode<T>* _right; BinaryTreeNode<T>* _left; T _data; BinaryTreeNode(const T& d) :_right(NULL) ,_left(NULL) ,_data(d) {} }; template <class T> class BinaryTree { typedef BinaryTreeNode<T> Node; public: BinaryTree() :_root(NULL) {} BinaryTree(const T* a, size_t size, const T& invalid) { size_t index = 0; _root = _CreatTree(a, size, index, invalid); } BinaryTree(const BinaryTree<T>& t) { _root = _CopyTree(t._root); } ~BinaryTree() { _Destory(_root); _root = NULL; } size_t Size() //求二叉樹結(jié)點數(shù)目 { return _Size(_root); } size_t Depth() //求二叉樹深度 { return _Depth(_root); } void PrevOrder() //前序遍歷 { _PrevOrder(_root); cout<<endl; } protected: Node* _CreatTree(const T* a, size_t size, size_t& index, const T& invalid) { Node* root = NULL; if(index<size && a[index]!=invalid) { root = new Node(a[index]); root->_left = _CreatTree(a, size, ++index, invalid); root->_right = _CreatTree(a, size, ++index, invalid); } return root; } Node* _CopyTree(const Node* root) { if(root == NULL) { return NULL; } Node* newRoot = new Node(root->_data); newRoot->_left = _CopyTree(root->_left); newRoot->_right = _CopyTree(root->_right); return newRoot; } void _Destory(Node* root) { if(root == NULL) { return; } _Destory(root->_left); _Destory(root->_right); delete root; } size_t _Size(Node* root) { if(root == NULL) { return 0; } return _Size(root->_left)+_Size(root->_right)+1; } size_t _Depth(Node* root) { if(root == NULL) { return 0; } size_t leftDepth = _Depth(root->_left); size_t rightDepth = _Depth(root->_right); return leftDepth > rightDepth ? leftDepth+1 : rightDepth+1; } void _PrevOrder(Node* root) { if(root == NULL) { return; } cout<<root->_data<<","; _PrevOrder(root->_left); _PrevOrder(root->_right); } private: Node* _root;
網(wǎng)頁名稱:二叉樹的實現(xiàn)
瀏覽路徑:http://bm7419.com/article32/jdeopc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、動態(tài)網(wǎng)站、關鍵詞優(yōu)化、網(wǎng)站導航、網(wǎng)站維護、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)