二叉树前序中序后续线索树_二叉树的先序,中序,后序遍历以及线索二叉树的遍历...
二叉樹的先序,中序,后序遍歷以及線索二叉樹的遍歷
(2008-05-04 17:52:49)
標簽:
雜談
C++
二叉樹的先序,中序,后序遍歷以及線索二叉樹的遍歷
頭文件
//*******************************************************************************
//二叉樹中的數據類型為ElemType
//*******************************************************************************
// BinaryTree.h: interface for the CBinaryTree class.
//
//
#include //C++語言
#define OK 1
#define ERROR 0
class CBinaryTree
{
public:
CBinaryTree();
virtual ~CBinaryTree();
protected:
typedef int ElemType;
struct Node
{ //數據
ElemType data; //數據域信息
int lt; //前繼指針標志
int rt; //后繼指針標志
Node* lchild; //指向左孩子
Node* rchild; //指向右孩子
}*root,*head;
public:
int Create();
int PreOrderTraverse();
int MidOrderTraverse();
int LastOrderTraverse();
void LevelOrderTraverse();
int GetLeafCount();
int GetDepth();
int GetNodeCount();
int MidOrderTraverseThr();
void ChangeLeftRight();
void Destroy();
private: //重載以上函數,供以上函數調用
int Create(Node* &node);
int PreOrderTraverse(Node*node);
int MidOrderTraverse(Node*node);
int LastOrderTraverse(Node *node);
int GetLeafCount(Node*node);
void GetNodeCount(Node*node,int &count);
int GetDepth(Node*node);
void ChangeLeftRight(Node*node);
void Visit(Node*node);
void Destroy(Node*node);
};
// 實現文件
// BinaryTree.cpp: implementation of the CBinaryTree class.
#include "BinaryTree.h"
#include "Stack.h"
#include "Queue.h"
//
// Construction/Destruction
//
CBinaryTree::CBinaryTree()
{
root=NULL;
head=NULL;
}
CBinaryTree::~CBinaryTree()
{
Destroy();
}
int CBinaryTree::Create()
{ //創建一個二叉樹
return Create(root);
//用戶調用此函數,創建根結點,由此函數調用其內的
//CreateNode()創建除根結點外的其它所有結點
}
int CBinaryTree::PreOrderTraverse()
{ //先序遍歷二叉樹
return PreOrderTraverse(root);
}
int CBinaryTree::MidOrderTraverse()
{ //中序遍歷二叉樹
return MidOrderTraverse(root);
}
int CBinaryTree::LastOrderTraverse()
{ //后序遍歷二叉樹
return LastOrderTraverse(root);
}
void CBinaryTree::LevelOrderTraverse()
{ //借助于堆棧,完成二叉樹的層次遍歷
CQueue queue;
if(!root) return;
queue.InitQueue();
Node*p=root; //初始化
Visit(p);
queue.EnQueue((long)p);
//訪問根結點,并將根結點地址入隊
while(!queue.QueueEmpty())
{ //當隊非空時重復執行下列操作
p=(Node*)queue.DeQueue(); //地址出隊
if(p->lchild)
{
Visit(p->lchild);
queue.EnQueue((long)p->lchild); //處理左孩子
}
if (p->rchild)
{
Visit(p->rchild);
queue.EnQueue((long)p->rchild); //處理右孩子
}
}
}
int CBinaryTree::GetLeafCount()
{ //返回葉子結點數
return GetLeafCount(root);
}
int CBinaryTree::GetNodeCount()
{ //返回總結點數
int count=0;
GetNodeCount(root,count);
return count;
}
int CBinaryTree::GetDepth()
{ //返回二叉樹的深度
return GetDepth(root);
}
void CBinaryTree::ChangeLeftRight()
{ //交換左右兩孩子結點
if(root!=NULL)
ChangeLeftRight(root);
}
int CBinaryTree::Create(Node *&node)
{
int i;
cout<
不創建本結點):";
cin>>i;
if(i==0)
node=NULL;
else
{
if(!(node=new Node))
return ERROR;
node->data=i;
node->lt=0;
node->rt=0;
Create(node->lchild); //創建左子樹
Create(node->rchild); //創建右子樹
}
return OK;
}
int CBinaryTree::PreOrderTraverse(Node *node)
{
if(node!=NULL)
{
cout<data<
if(PreOrderTraverse(node->lchild)) //遍歷左子樹
if(PreOrderTraverse(node->rchild)) //遍歷右子樹
return 1;
return 0;
}
else
return 1;
}
int CBinaryTree::MidOrderTraverse(Node *node)
{
if(node!=NULL)
{
if(MidOrderTraverse(node->lchild))//遍歷左子樹
cout<data<
if(MidOrderTraverse(node->rchild))//遍歷右子樹
return 1;
return 0;
}
else
return 1;
}
int CBinaryTree::LastOrderTraverse(Node *node)
{
if(node!=NULL)
{
if(LastOrderTraverse(node->lchild)) //遍歷左子樹
if(LastOrderTraverse(node->rchild)) //遍歷右子樹
{
cout<data<
return 1;
}
return 0;
}
else
return 1;
}
int CBinaryTree::MidOrderTraverseThr()
{ //中序線索化二叉樹
if(head!=NULL)
delete head;
head=new Node;
if(head==NULL)
return 0;
int i=0;
Node*pre=NULL,*cur=NULL;
long *temp=new long[GetNodeCount()];
head->lt=0; //無前驅,有左孩子
head->rt=1; //有后繼,無右孩子
head->rchild=head;
if(root==NULL)
head->lchild=head;
else
{
head->lchild=root;
pre=head;
cur=root;
for(;i>0 || cur!=NULL;)
{
for(;cur!=NULL;temp[i]=(long)cur,i++,cur=cur->lchild);
if(i>0)
{
cur=(Node*)temp[--i];
cout<data<
if(cur->lchild==NULL)
{
cur->lt=1;
cur->lchild=pre;
}
if(pre->rchild==NULL)
{
pre->rt=1;
pre->rchild=cur;
}
pre=cur;
cur=cur->rchild;
}
}
pre->rchild=head;
pre->rt=1;
head->rchild=pre;
}
delete temp;
return 1;
}
void CBinaryTree::GetNodeCount(Node *node,int &count)
{
if(node!=NULL)
count++;
else
return;
GetNodeCount(node->lchild,count);//統計左子樹個數
GetNodeCount(node->rchild,count);//統計右子樹個數
}
int CBinaryTree::GetDepth(Node *node)
{
int d=0,depthr=0,depthl=0;
if (node==NULL)
return 0; // 如果是空樹,則其深度為 0
else
{
depthl=GetDepth(node->lchild); //求node
的左子樹的深度
depthr=GetDepth(node->rchild); //求node
的右子樹的深度
return depthl>=depthr?1+depthl:1+depthr;
}
}
int CBinaryTree::GetLeafCount(Node *node)
{
int n,m;
if(node==NULL)
return 0; // 如果二叉樹是空樹,則返回 0
else if((node->lchild==NULL) &&
(node->rchild==NULL))
return 1; //
如果二叉樹的左孩子和右孩子均為空,則返回
1
else // 如果二叉樹的左孩子或右孩子不為空
{
n=GetLeafCount(node->lchild); //
求左子樹的葉子結點數目
m=GetLeafCount(node->rchild); //
求右子樹的葉子結點數目
return n+m; // 返回總的葉子結點數目
}
}
void CBinaryTree::ChangeLeftRight(Node *node)
{
if(node!=NULL)
{
ChangeLeftRight(node->lchild);
ChangeLeftRight(node->rchild);
Node*temp=node->lchild;
node->lchild=node->rchild;
node->rchild=temp;
}
}
void CBinaryTree::Visit(Node *node)
{ //訪問結點
cout<data<
}
void CBinaryTree::Destroy()
{ //銷毀二叉樹
Destroy(root);
root=NULL;
if(head)
delete head;
head=NULL;
}
void CBinaryTree::Destroy(Node *node)
{
if(node!=NULL)
{
Destroy(node->lchild);
Destroy(node->rchild);
delete node;
node=NULL;
}
}
分享:
喜歡
0
贈金筆
加載中,請稍候......
評論加載中,請稍候...
發評論
登錄名: 密碼: 找回密碼 注冊記住登錄狀態
昵???稱:
評論并轉載此博文
發評論
以上網友發言只代表其個人觀點,不代表新浪網的觀點或立場。
后一篇?>游戲名稱
總結
以上是生活随笔為你收集整理的二叉树前序中序后续线索树_二叉树的先序,中序,后序遍历以及线索二叉树的遍历...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何自定义cad线型_百度经验.html
- 下一篇: cad lisp程序大集_AUTO CA