-
大小: 6KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-01-01
- 語言: C/C++
- 標簽: 二叉樹??C++??數(shù)據(jù)結(jié)構(gòu)??
資源簡介
根據(jù)括號表達式構(gòu)造二叉樹,對二叉樹進行前序,中序,后序,層序遍歷,并用樹形方式打印輸出,有詳細注釋,供C++數(shù)據(jù)結(jié)構(gòu)課程學習與交流使用。
代碼片段和文件信息
#include?“stdafx.h“
#include?“stdlib.h“
#include?
#include?
#include?
#include?
using?namespace?std;
struct?BTNode?//二叉樹節(jié)點結(jié)構(gòu)
{
char?data;
BTNode?*lChild?*rChild;
BTNode(const?char?&data)
{
this->data?=?data;
this->lChild?=?NULL;
this->rChild?=?NULL;
}
BTNode()
{
this->lChild?=?NULL;
this->rChild?=?NULL;
}
};
class?BTree?//二叉樹類
{
private:
BTNode?head;?//?僅左子樹有效
int?GetHeight(BTNode?*ptr)const
{
if?(ptr?==?NULL)
return?0;
else
return?GetHeight(ptr->lChild)?>?GetHeight(ptr->rChild)???GetHeight(ptr->lChild)?+?1?:?GetHeight(ptr->rChild)?+?1;
}
public:
BTree(const?string?&s)?//根據(jù)字符串(括號表達式)初始化二叉樹的構(gòu)造函數(shù),只支持小寫字母
{
stacknodeStack;?//節(jié)點棧
stackstatusStack;?//狀態(tài)棧
nodeS
評論
共有 條評論