資源簡介
根據括號表達式構造二叉樹,對二叉樹進行前序,中序,后序,層序遍歷,并用樹形方式打印輸出,有詳細注釋,供C++數據結構課程學習與交流使用。
代碼片段和文件信息
#include?“stdafx.h“
#include?“stdlib.h“
#include?
#include?
#include?
#include?
using?namespace?std;
struct?BTNode?//二叉樹節點結構
{
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)?//根據字符串(括號表達式)初始化二叉樹的構造函數,只支持小寫字母
{
stacknodeStack;?//節點棧
stackstatusStack;?//狀態棧
nodeS
評論
共有 條評論