新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
// 本次练习的是 二叉树的 递归和非递归 遍历 以及二叉树的 节点数 高度 叶子节点数 和查找功能
//如果要是变量在函数栈回归时不回归原值,则可以用引用
//
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
using namespace std;
template
struct BinaryTreeNode
{
BinaryTreeNode(const T& date)
:_date(date)
, _left(NULL)
, _right(NULL)
{}
T _date;
BinaryTreeNode
BinaryTreeNode
};
template
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)<
void Hight()
{
cout << _Hight(_root) << endl;
}
void LeafOfNumber()
{
cout<< _LeafOfNumber(_root)<
BinaryTreeNode
{
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
{
if (root != NULL)
{
Destory(root->_left);
Destory(root->_right);
delete root;
}
}
protected:
BinaryTreeNode
{
if (index >= size || date[index] == '#')
{
return NULL;
}
BinaryTreeNode
root->_left = _CreateBinaryTree(date, ++index, size); //要用前置加加而不能用后置加加 因为后置加加会产生一个临时变量
root->_right = _CreateBinaryTree(date, ++index, size);
return root;
}
void _PreOrder(BinaryTreeNode
{
if (root == NULL)
{
return;
}
cout << root->_date << " ";
_PreOrder(root->_left);
_PreOrder(root->_right);
}
void _MiddleOrder(BinaryTreeNode
{
if (root == NULL)
{
return;
}
_MiddleOrder(root->_left);
cout << root->_date<<" ";
_MiddleOrder(root->_right);
}
void _LastOrder(BinaryTreeNode
{
if (root == NULL)
{
return;
}
_LastOrder(root->_left);
_LastOrder(root->_right);
cout << root->_date << " ";
}
int _Size(BinaryTreeNode
{
if (root == NULL)
{
return 0;
}
return _Size(root->_left) + _Size(root->_right) + 1;
}
int _Hight(BinaryTreeNode
{
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
{
if (root == NULL)
{
return 0;
}
if (root->_left == NULL && root->_right == NULL)
{
return 1;
}
return _LeafOfNumber(root->_left) + _LeafOfNumber(root->_right);
}
BinaryTreeNode
{
if (root == NULL)
{
return NULL;
}
if (root->_date == data)
{
return root;
}
BinaryTreeNode
if (left != NULL)
{
return left;
}
BinaryTreeNode
if (right != NULL)
{
return right;
}
}
/*
void _PrevOrder_Non_R(BinaryTreeNode
{
stack
BinaryTreeNode
while (!s.empty() || cur != NULL)
{
s.push(cur);
BinaryTreeNode
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
{
stack
BinaryTreeNode
if (cur != NULL)
{
s.push(cur);
}
while (!s.empty())
{
BinaryTreeNode
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
{
stack
BinaryTreeNode
while (!s.empty() || cur != NULL)
{
while (cur != NULL)
{
s.push(cur);
cur = cur->_left;
}
if (!s.empty())
{
BinaryTreeNode
s.pop();
cout << top->_date << " ";
cur = top->_right;
}
}
}
void _LastOrder_Non_R(BinaryTreeNode
{
stack
BinaryTreeNode
BinaryTreeNode
while (cur != NULL || !s.empty())
{
while (cur != NULL)
{
s.push(cur);
cur = cur->_left; //此时左已遍历完
}
BinaryTreeNode
if (top->_right == NULL || top->_right == prev) //这两种情况都表示 1. 右 已遍历完
{
cout << top->_date << " ";
prev = top;
s.pop();
}
else // 2.右还没有遍历完
{
cur = top->_right;
}
}
cout << endl;
}
protected:
BinaryTreeNode
};
void Test1()
{
int arr[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6};
BinaryTree
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
cout << find->_date << endl;
}
int main()
{
Test1();
return 0;
}
创新互联专业网站制作、成都做网站,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文营销等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。