C++ 數(shù)據(jù)結(jié)構(gòu)二叉樹(前序/中序/后序遞歸、非遞歸遍歷)
C++ 數(shù)據(jù)結(jié)構(gòu)二叉樹(前序/中序/后序遞歸、非遞歸遍歷)
二叉樹的性質(zhì):
二叉樹是一棵特殊的樹,二叉樹每個(gè)節(jié)點(diǎn)最多有兩個(gè)孩子結(jié)點(diǎn),分別稱為左孩子和右孩子。
例:


實(shí)例代碼:
#include <iostream>
#include <Windows.h>
#include <stack>
using namespace std;
template <class T>
struct BinaryTreeNode
{
int _data;
BinaryTreeNode<T>* _left; //左孩子
BinaryTreeNode<T>* _right; //右孩子
BinaryTreeNode(const T& data)
:_data(data)
, _left(NULL)
, _right(NULL)
{}
};
template <class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(T* arr, size_t n, const T& invalid=T())
{
size_t index = 0;
_root = _CreatTree(arr, n, invalid, index);
}
void PreOrderR() //前序遍歷 遞歸
{
_PreOrderR(_root);
}
void PreOrder() //前序遍歷 非遞歸
{
_PreOrder(_root);
}
void InOrderR() //中序遍歷 遞歸
{
_InOrderR(_root);
}
void InOrder() //中序遍歷 非遞歸
{
_InOrder(_root);
}
void PostOrderR() //后序遍歷 左 右 根 遞歸
{
_PostOrderR(_root);
}
void PostOrder() //后序遍歷 左 右 根 非遞歸
{
_PostOrder(_root);
}
~BinaryTree()
{}
protected:
//建樹 arr:建樹使用的數(shù)組 n:數(shù)組大小 invalid:非法值 index:當(dāng)前下標(biāo)
Node* _CreatTree(T* arr, size_t n, const T& invalid, size_t& index)
{
Node* root = NULL;
if (index < n && arr[index] != invalid)
{
root = new Node(arr[index]); //根節(jié)點(diǎn)
root->_left = _CreatTree(arr, n, invalid, ++index);
root->_right = _CreatTree(arr, n, invalid, ++index);
}
return root;
}
void _PreOrderR(Node* root) //前序遍歷 遞歸
{
if (root == NULL)
{
return;
}
cout << root->_data << " ";
_PreOrderR(root->_left);
_PreOrderR(root->_right);
}
void _PreOrder(Node* root) //前序遍歷 非遞歸
{
stack<Node*> tty;
while (root != NULL || !tty.empty())
{
if (root)
{
cout << root->_data << " ";
tty.push(root);
root = root->_left;
}
else
{
Node* temp = tty.top();
tty.pop();
root = temp->_right;
}
}
}
void _InOrderR(Node* root) //中序遍歷 遞歸
{
if (root == NULL)
{
return;
}
_InOrderR(root->_left);
cout << root->_data << " ";
_InOrderR(root->_right);
}
void _InOrder(Node* root) //中序遍歷 非遞歸
{
if (root == NULL)
{
return;
}
stack<Node*> tty;
while (root != NULL || !tty.empty())
{
while (root)
{
tty.push(root);
root = root->_left;
}
//此時(shí)出了循環(huán)走到了最左葉子節(jié)點(diǎn)
Node* temp = tty.top();
tty.pop();
cout << temp->_data << " ";
root = temp->_right;
}
}
void _PostOrderR(Node* root) //后序遍歷 左 右 根 遞歸
{
if (root == NULL)
{
return;
}
_PostOrderR(root->_left);
_PostOrderR(root->_right);
cout << root->_data << " ";
}
void _PostOrder(Node* root) //后序遍歷 左 右 根 非遞歸
{
if (root == NULL)
{
return;
}
stack<Node*> tty;
Node* PreNode = NULL; //上一個(gè)訪問的結(jié)點(diǎn)
tty.push(root);
while (!tty.empty())
{
Node* cur = tty.top();
//訪問的當(dāng)前節(jié)點(diǎn)左右孩子均為空或者當(dāng)前節(jié)點(diǎn)左右子樹均已經(jīng)訪問過
if ((cur->_left == NULL && cur->_right == NULL) || ((PreNode != NULL) && (PreNode == cur->_left || PreNode == cur->_right)))
{
cout << cur->_data << " ";
tty.pop();
PreNode = cur;
}
else
{
if (cur->_right != NULL)
{
tty.push(cur->_right);
}
if (cur->_left != NULL)
{
tty.push(cur->_left);
}
}
}
}
protected:
Node* _root;
};
#include "源.h"
void Test()
{
int array[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
BinaryTree<int> p(array, sizeof(array) / sizeof(array[0]), '#');
cout << "前序遞歸遍歷: " << "";
p.PreOrderR();
cout << endl;
cout << "前序非遞歸遍歷: " << "";
p.PreOrder();
cout << endl;
cout << "中序遞歸遍歷: " << "";
p.InOrderR();
cout << endl;
cout << "中序非遞歸遍歷: " << "";
p.InOrder();
cout << endl;
cout << "后序遞歸遍歷: " << "";
p.PostOrderR();
cout << endl;
cout << "后序非遞歸遍歷: " << "";
p.PostOrder();
cout << endl;
}
int main()
{
Test();
system("pause");
return 0;
}
實(shí)現(xiàn)效果:

以上就是數(shù)據(jù)結(jié)構(gòu)二叉樹的詳解,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C++超詳細(xì)講解RTTI和cast運(yùn)算符的使用
RTTI(Runtime Type Identification)是“運(yùn)行時(shí)類型識(shí)別”的意思。C++引入這個(gè)機(jī)制是為了讓程序在運(yùn)行時(shí)能根據(jù)基類的指針或引用來獲得該指針或引用所指的對(duì)象的實(shí)際類型,cast強(qiáng)制轉(zhuǎn)換運(yùn)算符是一種特殊的運(yùn)算符,它把一種數(shù)據(jù)類型轉(zhuǎn)換為另一種數(shù)據(jù)類型2022-08-08
C語言中變參函數(shù)傳參的實(shí)現(xiàn)示例
本文主要介紹了C語言中變參函數(shù)傳參,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C/C++ 多線程的學(xué)習(xí)心得總結(jié)
本篇文章是對(duì)C/C++中多線程的學(xué)習(xí)心得總結(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
c++ STL set_difference set_intersection set_union 操作
這篇文章主要介紹了c++ STL set_difference set_intersection set_union 操作,需要的朋友可以參考下2017-03-03

