需求二叉树:
代码实现:
"tree.hpp"文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdbool.h>
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <stack>
#include<queue>
using namespace std;
class Treenode
{
friend class Bitree;
public:
Treenode();
Treenode(char a);
//Treenode(char c);
Treenode& setnode(char a)
{
this->val = a;
return *this;
}
void setfirstchild(Treenode* a);
void setnextchild(Treenode* a);
void setrtag(bool a);
void setltag(bool a);
char getval();
Treenode& getfirstchild();
Treenode& getnextchild();
private:
char val;//数据部分
Treenode* firstchild;
Treenode *nextchild;//同一化的树的存储
bool ltag , rtag ;//0表示有左或右孩子,1表示该结点没有左或右孩子,且此链域是线索
};
Treenode& Treenode::getfirstchild()
{
return *this->firstchild;
}
Treenode& Treenode::getnextchild()
{
return *this->nextchild;
}
char Treenode::getval()
{
return this->val;
}
void Treenode::setfirstchild(Treenode* a)
{
this->firstchild = a;
}
void Treenode::setnextchild(Treenode *a)
{
this->nextchild = a;
}
void Treenode::setltag(bool a)
{
this->ltag = a;
}
void Treenode::setrtag(bool a)
{
this->rtag = a;
}
Treenode::Treenode(char a)
{
this->val = a;
if (this->firstchild != NULL)
{
delete this->firstchild;
}
this->firstchild = NULL;
if (this->nextchild != NULL)
{
delete this->nextchild;
}
this->nextchild = NULL;
this->ltag = false;
this->rtag = false;
}
Treenode::Treenode()
{
this->val = '#';
if (this->firstchild != NULL)
{
delete this->firstchild;
}
this->firstchild = NULL;
if (this->nextchild != NULL)
{
delete this->nextchild;
}
this->nextchild = NULL;
this->ltag = false;
this->rtag = false;
}
//Treenode::Treenode(char c)
//{
// this->val = (char)('A' + rand()%);//输出大写的
// this->firstchild = NULL;
// this->nextchild = NULL;
// this->ltag = false;
// this->rtag = false;
//}
class Bitree
{
friend class Treenode;
public:
Bitree()
{
this->root = new(class Treenode);
}
Treenode& setroot(Treenode* a);
//void createBitree();
void visit(Treenode* a);
Treenode* getroot()
{
return this->root;
}
void inorder_traversal(Treenode* t);//中序遍历递归算法
void inorder_nonrecursion_traversal();//中序遍历非递归算法
void preorder_traversal(Treenode* t);
void preorder_nonrecursion_traversal();
int getcount()
{
return this->count;
}
void sequence_traversal();//层序遍历
private:
Treenode* root;
int count = 0;
};
void Bitree::sequence_traversal()//层序遍历
{
queue<Treenode*>Q;
Treenode* p=this->root;
Q.push(p);
while (!Q.empty())
{
this->visit(Q.front());
p = Q.front();
Q.pop();
if (p->firstchild != NULL)
Q.push(p->firstchild);
if (p->nextchild != NULL)
Q.push(p->nextchild);
}
}
void Bitree::inorder_nonrecursion_traversal()//中序遍历非递归算法
{
stack<Treenode*> S;
Treenode*p=this->root;
//Treenode* pre = NULL;
while (p || !S.empty())
{
if (p)
{
S.push(p);
//if(p->firstchild!=NULL)
//pre = p;
p = p->firstchild;
}
else
{
this->visit(S.top());
p = S.top()->nextchild;
S.pop();
}
}
}
void Bitree::preorder_traversal(Treenode* t)//先序遍历递归算法
{
if (t == NULL)
return;
visit(t);
preorder_traversal(t->firstchild);
preorder_traversal(t->nextchild);
}
void Bitree::preorder_nonrecursion_traversal()//先序遍历非递归算法
{
stack<Treenode*> S;
Treenode* p = this->root;
//Treenode* pre = NULL;
while (p || !S.empty())
{
if (p)
{
this->visit(p);
S.push(p);
//if(p->firstchild!=NULL)
//pre = p;
p = p->firstchild;
}
else
{
p = S.top()->nextchild;
S.pop();
}
}
}
Treenode& Bitree::setroot(Treenode* a)
{
this->root = a;
return *this->root;
}
void Bitree::visit(Treenode*a)
{
if (a->val != '#')
{
cout << a->val;
this->count++;
}
}
void Bitree::inorder_traversal(Treenode* t)//中序遍历递归算法
{
if (t == NULL)
return;
inorder_traversal(t->firstchild);
visit(t);
inorder_traversal(t->nextchild);
}
"源.cpp":
#define _CRT_SECURE_NO_WARNINGS 1
#include "tree.hpp"
int main()
{
//srand((unsigned)time(NULL));
//输出字母的测试代码
//cout <<(char) ('A' + rand() % 26);
//int count = 0;//避免重复的字母
//先创建空树再遍历赋值
Bitree T;
Treenode m1('A');
Treenode m2('B');
Treenode m3('C');
Treenode m4('D');
Treenode m5('E');
Treenode m6('F');
Treenode m7('G');
T.setroot(&m1);
m1.setfirstchild(&m2);
m1.setnextchild(&m3);
m2.setfirstchild(&m4);
m2.setnextchild(&m5);
m3.setfirstchild(&m6);
m3.setrtag(true) ;
m4.setnextchild(&m7);
//cout << m4.getnextchild().getval();
m4.setrtag(true);
m5.setrtag(true);
m5.setrtag(true);
m6.setrtag(true);
m6.setrtag(true);
m7.setrtag(true);
m7.setrtag(true) ;
//T.createBitree();
T.inorder_traversal(T.getroot());//中序递归遍历
cout << endl;
T.inorder_nonrecursion_traversal();//中序非递归遍历
cout << endl;
T.preorder_traversal(T.getroot());//先序递归遍历
cout << endl;
T.preorder_nonrecursion_traversal();//先序非递归遍历
cout << endl;
T.sequence_traversal();//层序遍历
cout<<endl;
cout << T.getcount();
system("pause");
return 0;
}
中序遍历结果:
先序遍历结果:
层序遍历结果: