📄 tree.h
字号:
#ifndef TREE_H
#define TREE_H
#include <iostream>
#include "LinkedQueen.h"
#include "LinkedStack.h"
using namespace std;
TreeNode* CreateTree(LinkedQueen<TreeNode*> q)
{
TreeNode* Gen = NULL;
char fa,ch;
TreeNode* r = new TreeNode;
cout<<"\n请 输 入 一 个 结 点 :";
for (cin>>fa>>ch;ch!='#';cin>>fa>>ch)
{
TreeNode* p = new TreeNode;
p->data = ch;
p->firstchild = p->nextsibling = NULL;
q.Add(p);
if (fa == '#')
{
Gen = p;
r = p;
}
else
{
TreeNode* s = q.First();
while (s->data!=fa)
{
q.Delete();
s = q.First();
r = s;
}
if (!(s->firstchild))
{
s->firstchild = p;
r = p;
}
else
{
r->nextsibling = p;
r = p;
}
}
cout<<"请 输 入 一 个 结 点 :";
}
cout<<"\n结 点 输 入 完 毕!"<<endl;
return Gen;
}
int count = 0;
void PrintTree(LinkedStack<char>& s,TreeNode* Gen)
{
while (Gen)
{
s.Push(Gen->data);
if (!Gen->firstchild)
{
cout<<"路 径 "<<++count<<" :";
s.Print();
}
else
{
PrintTree(s,Gen->firstchild);
}
s.Pop();
Gen = Gen->nextsibling;
}
}
void Visit(TreeNode* Gen)
{
if (Gen->data!='+' && Gen->data!='-' && Gen->data!='*' && Gen->data!='/')
{
//cout<<float(Gen->data);
int asc = Gen->data;
cout<<asc;
}
else
{
cout<<Gen->data;
}
}
void PreOrder(TreeNode* Gen)
{
if (Gen)
{
//cout<<Gen->data<<" ";
Visit(Gen);cout<<" ";
PreOrder(Gen->firstchild);
PreOrder(Gen->nextsibling);
}
}
void InOrder(TreeNode* Gen)
{
if (Gen)
{
InOrder(Gen->firstchild);
//cout<<Gen->data<<" ";
Visit(Gen);cout<<" ";
InOrder(Gen->nextsibling);
}
}
void PostOrder(TreeNode* Gen)
{
if (Gen)
{
PostOrder(Gen->firstchild);
PostOrder(Gen->nextsibling);
//cout<<Gen->data<<" ";
Visit(Gen);cout<<" ";
}
}
void LevelOrder(TreeNode* Gen)
{
LinkedQueen<TreeNode*> Q;
TreeNode* t = Gen;
Q.Add(t);
while (t)
{
Q.Delete();
//cout<<t->data<<" ";
Visit(t);cout<<" ";
if (t->firstchild)
{
Q.Add(t->firstchild);
}
if (t->nextsibling)
{
Q.Add(t->nextsibling);
}
t = Q.First();
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -