您好,登錄后才能下訂單哦!
// 本次練習的是 二叉樹的 遞歸和非遞歸 遍歷 以及二叉樹的 節點數 高度 葉子節點數 和查找功能
//如果要是變量在函數棧回歸時不回歸原值,則可以用引用
//
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
template<class T>
struct BinaryTreeNode
{
BinaryTreeNode(const T& date)
:_date(date)
, _left(NULL)
, _right(NULL)
{}
T _date;
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
};
template<class T>
class BinaryTree
{
public:
BinaryTree(const T* date,size_t size)
{
size_t index = 0;
_root = _CreateBinaryTree(date, index, size);
}
~BinaryTree()
{
Destory(_root);
}
void PreOrder()
{
_PreOrder(_root);
cout << endl;
}
void MiddleOrder()
{
_MiddleOrder(_root);
cout << endl;
}
void LastOrder()
{
_LastOrder(_root);
cout << endl;
}
void Size()
{
cout<<_Size(_root)<<endl;
}
void Hight()
{
cout << _Hight(_root) << endl;
}
void LeafOfNumber()
{
cout<< _LeafOfNumber(_root)<<endl;
}
BinaryTreeNode<T>* Find(const T& data)
{
return _Find(_root,data);
}
void PrevOrder_Non_R()
{
_PrevOrder_Non_R(_root);
cout << endl;
}
void MiddleOrder_Non_R()
{
_MiddleOrder_Non_R(_root);
cout << endl;
}
void LastOrder_Non_R()
{
_LastOrder_Non_R(_root);
cout << endl;
}
void Destory(BinaryTreeNode<T>* root)
{
if (root != NULL)
{
Destory(root->_left);
Destory(root->_right);
delete root;
}
}
protected:
BinaryTreeNode<T>* _CreateBinaryTree(const T* date, size_t& index, size_t size)//注意 index 要用 引用
{
if (index >= size || date[index] == '#')
{
return NULL;
}
BinaryTreeNode<T>* root = new BinaryTreeNode<T>(date[index]);
root->_left = _CreateBinaryTree(date, ++index, size); //要用前置加加而不能用后置加加 因為后置加加會產生一個臨時變量
root->_right = _CreateBinaryTree(date, ++index, size);
return root;
}
void _PreOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
cout << root->_date << " ";
_PreOrder(root->_left);
_PreOrder(root->_right);
}
void _MiddleOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
_MiddleOrder(root->_left);
cout << root->_date<<" ";
_MiddleOrder(root->_right);
}
void _LastOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
_LastOrder(root->_left);
_LastOrder(root->_right);
cout << root->_date << " ";
}
int _Size(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return 0;
}
return _Size(root->_left) + _Size(root->_right) + 1;
}
int _Hight(BinaryTreeNode<T>* root) //求二叉樹的高度
{
if (root == NULL)
{
return 0;
}
int left = _Hight(root->_left) ;
int right = _Hight(root->_right);
return (left > right ? (left + 1) :( right + 1));
}
int _LeafOfNumber(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return 0;
}
if (root->_left == NULL && root->_right == NULL)
{
return 1;
}
return _LeafOfNumber(root->_left) + _LeafOfNumber(root->_right);
}
BinaryTreeNode<T>* _Find(BinaryTreeNode<T>* root,const T& data)
{
if (root == NULL)
{
return NULL;
}
if (root->_date == data)
{
return root;
}
BinaryTreeNode<T>* left = _Find(root->_left,data);
if (left != NULL)
{
return left;
}
BinaryTreeNode<T>* right = _Find(root->_right,data);
if (right != NULL)
{
return right;
}
}
/*
void _PrevOrder_Non_R(BinaryTreeNode<T>* root)
{
stack<BinaryTreeNode<T>*> s;
BinaryTreeNode<T>* cur = root;
while (!s.empty() || cur != NULL)
{
s.push(cur);
BinaryTreeNode<T>* top = s.top();
cout << top->_date << " ";
cur = top->_left;
if (cur == NULL)
{
s.pop();
if (s.top()->_right != NULL)
{
cur = s.top()->_right;
}
else
{
s.pop();
}
}
}
}
*/
void _PrevOrder_Non_R(BinaryTreeNode<T>* root)
{
stack<BinaryTreeNode<T>* > s; //棧中每個節點都會存放一次 下面只需把一個節點的 根 左 右 訪問一次就行
BinaryTreeNode<T>* cur = root;
if (cur != NULL)
{
s.push(cur);
}
while (!s.empty())
{
BinaryTreeNode<T>* top = s.top();
cout << top->_date << " ";
s.pop();
if (top->_right != NULL)
{
s.push(top->_right);
}
if (top->_left != NULL)
{
s.push(top->_left);
}
}
}
void _MiddleOrder_Non_R(BinaryTreeNode<T>* root)
{
stack<BinaryTreeNode<T>* > s;
BinaryTreeNode<T>* cur = root;
while (!s.empty() || cur != NULL)
{
while (cur != NULL)
{
s.push(cur);
cur = cur->_left;
}
if (!s.empty())
{
BinaryTreeNode<T>* top = s.top();
s.pop();
cout << top->_date << " ";
cur = top->_right;
}
}
}
void _LastOrder_Non_R(BinaryTreeNode<T>* root)
{
stack<BinaryTreeNode<T>*> s;
BinaryTreeNode<T>* cur = root;
BinaryTreeNode<T>* prev = NULL;
while (cur != NULL || !s.empty())
{
while (cur != NULL)
{
s.push(cur);
cur = cur->_left; //此時左已遍歷完
}
BinaryTreeNode<T>* top = s.top();
if (top->_right == NULL || top->_right == prev) //這兩種情況都表示 1. 右 已遍歷完
{
cout << top->_date << " ";
prev = top;
s.pop();
}
else // 2.右還沒有遍歷完
{
cur = top->_right;
}
}
cout << endl;
}
protected:
BinaryTreeNode<T>* _root;
};
void Test1()
{
int arr[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6};
BinaryTree<int> t2(arr, 10);
cout << "前序:" << endl;
t2.PreOrder();
t2.PrevOrder_Non_R();
cout << "中序:" << endl;
t2.MiddleOrder();
t2.MiddleOrder_Non_R();
cout << "后序:" << endl;
t2.LastOrder();
t2.LastOrder_Non_R();
cout << "節點數:" << endl;
t2.Size();
cout << "高度:" << endl;
t2.Hight();
cout << "葉子節點數:" << endl;
t2.LeafOfNumber();
BinaryTreeNode<int>* find = t2.Find(3);
cout << find->_date << endl;
}
int main()
{
Test1();
return 0;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。