91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

數據結構之二叉樹 (構造 拷貝構造 以及前序中序后續三種遍歷方法)

發布時間:2020-07-25 20:57:35 來源:網絡 閱讀:1818 作者:性感的玉米 欄目:編程語言
首先二叉樹的節點定義如下:
struct BinaryNode
{

                 BinaryNode *_left;
                 BinaryNode *_right;
                 T _data;
                BinaryNode( T data ) :_data(data), _left( NULL), _right(NULL )
                {};
};

二叉樹的結構以及接口如下
template<class T>
class BinaryTree
{
                 typedef BinaryNode <T> Node;
public:
                BinaryTree() :_head( NULL)
                {};
                BinaryTree( const T * a, size_t size, const T & valiue)
                ~BinaryTree()
                BinaryTree( BinaryTree &b )
                void PrevOder()
                 void InOder()
                 void PostOder()
private:
public:
                 void LevalOder()
                 size_t depth()
                 size_t size()
                 size_t learsize()
                 void _LevalOder(BinaryNode <T> *root);
                 size_t _depth(BinaryNode <T> *root);
                 void _size(BinaryNode <T> *root, int *p);
                 void _leafsize(BinaryNode <T> *root, size_t *p);
                 int Leafsize(BinaryNode <T> *root);
                 void PrevOder_Nor();
                 void InOder_Nor();
                 void PostOder_Nor();
                 void Distory(Node *_root)
                 Node* _Copy(Node *cur);
private:
                 BinaryNode<T > *_root;
};


二叉樹的建立(構造函數)
                BinaryTree( const T * a, size_t size, const T & valiue)
                {
                                 size_t index = 0;
                                _root = _CreatTree( a, size , index, valiue);
                }
                 BinaryNode<T >* _CreatTree(const T* a, size_t size, size_t &index, const T &valiue)
                {
                                 BinaryNode<T > *root = NULL;
                                 if (index < size&& a[index ] != valiue)
                                {
                                                root = new BinaryNode <T>(a[index]);
                                                root->_left = _CreatTree(a, size , ++index, valiue);
                                                root->_right = _CreatTree(a, size , ++index, valiue);
                                }
                                 return root;
                }
二叉樹的銷毀(析構函數)
                ~BinaryTree()
                {
                                Distory(_root);
                                cout << " ~BinaryTree()" << endl;
                }
                 void Distory(Node *_root)
                {
                                 if (_root == NULL)
                                                 return;
                                Distory( _root->_left);
                                Distory( _root->_right);
                                 if (_root )
                                                 delete _root ;
                                 _root == NULL ;

                }
二叉樹的拷貝(拷貝構造)
                BinaryTree( BinaryTree &b )
                {
                                _root = _Copy( b._root);
                }
BinaryNode<T >* BinaryTree< T>::_Copy(Node *cur)
{
                 if (cur == NULL)
                                 return NULL ;
                 Node *tmp = new Node(cur->_data);
                tmp->_left=_Copy( cur->_left);
                tmp->_right=_Copy( cur->_right);
                 return tmp;

}
求葉子節點個數(兩種方法)
一:
int BinaryTree <T>::Leafsize( BinaryNode<T > *root)
{
                 int count;
                 if (root == NULL)
                                count = 0;
                 else if (root->_left == NULL&&root ->_right == NULL)
                {
                                count = 1;
                }
                 else  count = Leafsize(root ->_left) + Leafsize(root->_right);
                 return count;
}
二:
void BinaryTree <T>::_leafsize( BinaryNode<T > *root, size_t * p)
{
                 if (root != NULL)
                {
                                _leafsize( root->_left,p );
                                _leafsize( root->_right,p );
                                 if (root ->_left == NULL&& root->_right == NULL )
                                {
                                                ++ *p;
                                }

                }
}

二叉樹前序遍歷遞歸
                 void _PrevOder(BinaryNode <T> * root)
                {
                                 if (root == NULL)
                                                 return;
                                cout << root->_data << " " ;
                                _PrevOder( root->_left);
                                _PrevOder( root->_right);
                }
二叉樹前序遍歷非遞歸
void BinaryTree <T>::PrevOder_Nor()
{
                 BinaryNode<T > *cur = _root;
                 stack<BinaryNode <T>*> s;
                 if (cur == NULL )
                                 return;
                s.push(cur);
                 while (!s.empty())
                {
                                 BinaryNode<T > *tmp = s.top();
                                cout << tmp->_data << " ";
                                s.pop();
                                 if (tmp->_right)
                                {
                                                s.push(tmp->_right);
                                }
                                 if (tmp->_left)
                                {
                                                s.push(tmp->_left);
                                }
                }
                cout << endl;
                
}
二叉樹層序遍歷(隊列實現)
void BinaryTree <T>::_LevalOder( BinaryNode<T > *root)
{
                 deque<BinaryNode <T>*> q;
                q.push_back( root);
                 while (q.size())
                {
                                 BinaryNode<T > *pNode = q.front();
                                q.pop_front();
                                cout << pNode->_data << " ";
                                 if (pNode->_left)
                                {
                                                q.push_back(pNode->_left);
                                }
                                 if (pNode->_right)
                                {
                                                q.push_back(pNode->_right);
                                }
                }
                

}
二叉樹中序遍歷遞歸
                 void _InOder(BinaryNode <T> * root)
                {
                                 if (root == NULL)
                                                 return;
                                _InOder( root->_left);
                                cout << root->_data << " " ;
                                _InOder( root->_right);
                }
二叉樹中序遍歷非遞歸
void BinaryTree <T>::InOder_Nor()
{
                 if (_root == NULL )
                                 return;
                 Node *cur = _root;
                 stack<Node *> s;
                 while (cur||!s.empty())
                {
                                 while (cur)
                                {
                                                s.push(cur);
                                                cur = cur->_left;
                                }
                                 Node *tmp = s.top();
                                cout << tmp->_data << " ";
                                s.pop();
                                cur = tmp->_right;
                }
                cout << endl;

}
二叉樹后序遍歷遞歸
                 void  _PostOder(BinaryNode <T> * root)
                {
                                 if (root == NULL)
                                                 return;
                                _PostOder( root->_left);
                                _PostOder( root->_right);
                                cout << root->_data << " " ;
                }
二叉樹后序遍歷非遞歸
void BinaryTree <T>::PostOder_Nor()
{
                 if (_root == NULL )
                                 return;
                 Node *cur = _root;
                 stack<Node *> s;
                 Node *prev = NULL ;
                 while (cur||!s.empty())
                {
                                 while (cur)
                                {
                                                s.push(cur);
                                                cur = cur->_left;
                                }
                                 Node *tmp = s.top();
                                 if (tmp->_right == NULL || tmp->_right == prev)
                                {
                                                cout << tmp->_data << " " ;
                                                s.pop();
                                                prev = tmp;
                                }
                                 else
                                {
                                                cur = tmp->_right;
                                }
                }
                cout << endl;
                
}
二叉樹的深度
size_t BinaryTree <T>::_depth( BinaryNode<T > *root)
{
                 int hleft;
                 int hright;
                 int max;
                 if (root )
                {
                                hleft = _depth( root->_left);
                                hright = _depth( root->_right);
                                max = hleft > hright ? hleft : hright;
                                 return max + 1;

                }
                 else
                {
                                 return 0;
                }
}
二叉樹的大小
                 size_t size()
                {
                                 int count = 0;
                                _size(_root,&count);
                                 return count;
                }
void BinaryTree <T>::_size( BinaryNode<T > *root,int *p)
{
                 if (root )
                {
                                ++(* p);
                                _size( root->_left, p );
                                _size( root->_right, p );
                }
                 return ;
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

兴山县| 凤凰县| 眉山市| 滁州市| 金昌市| 微山县| 仁布县| 台山市| 通化县| 香格里拉县| 文山县| 福鼎市| 高雄市| 罗山县| 克山县| 安龙县| 武川县| 罗田县| 新龙县| 新闻| 云和县| 松滋市| 察隅县| 蒲城县| 外汇| 天津市| 秀山| 青铜峡市| 鄂托克前旗| 固阳县| 托里县| 健康| 开阳县| 隆昌县| 桃源县| 长乐市| 宁陵县| 晋城| 唐海县| 威远县| 正镶白旗|