#include"iostream"
#include"string"
using namespace std;
typedef struct Node2
{
int date;
struct Node2 *lchild, *rchild;
}BTNode;
void Displeaf(BTNode *b)//输出所有结点
{
if (b != NULL)
{
if (b->lchild == NULL&&b->rchild == NULL)
{
cout << b->date;
}
else
{
Displeaf(b->lchild);
Displeaf(b->rchild);
}
}
}
BTNode*InsertleftNode(BTNode *p, int x)//插入左结点
{
BTNode *s, *t;
if (p == NULL)
return NULL;
t = p->lchild;
s = new BTNode;
s->date = x;
s->lchild = t;
s->rchild = NULL;
p->lchild = s;
return p->lchild;
}
BTNode*nsertrightNode(BTNode*p, int x)
{
BTNode *s, *t;
if (p == NULL)
{
return NULL;
}
t = p->rchild;
s = new BTNode;
s->date = x;
s->rchild = t;
s->lchild = NULL;
p->rchild = s;
return p->rchild;
}
BTNode *DeleteleftTree(BTNode *p)
{
BTNode *q;
q = new BTNode;
if (p == NULL || p->lchild == NULL)
return NULL;
q = p->lchild;
p->lchild = NULL;
delete q;
return p;
}
BTNode *DeleterightTree(BTNode *p)
{
BTNode *q;
q = new BTNode;
if (p == NULL || p->rchild == NULL)
return NULL;
q = p->rchild;
p->rchild = NULL;
delete q;
return p;
}
BTNode*SearchNode(BTNode*b, int x)
{
BTNode *p;
if (b == NULL)return b;
else if (b->date == x)return b;
else
{
p = SearchNode(b->lchild, x);
if (p != NULL)return p;
else
return SearchNode(b->rchild, x);
}
}
BTNode *LchildNode(BTNode*p)
{
return p->lchild;
}
BTNode *RchildNode(BTNode*p)
{
return p->rchild;
}
int BiTreeDepth(BTNode *b)
{
int lchilddep, rchilddep;
if (b == NULL)return 0;
else
{
lchilddep = BiTreeDepth(b->lchild);
rchilddep = BiTreeDepth(b->rchild);
return(lchilddep > rchilddep ? (lchilddep + 1) : (rchilddep + 1));
}
}
void DispBiTree(BTNode*b)
{
if (b != NULL)
{
cout << b->date;
if (b->lchild != NULL || b->rchild != NULL)
{
cout << "(";
DispBiTree(b->lchild);
if (b->rchild != NULL)
{
cout << ",";
DispBiTree(b->rchild);
}
cout << ")";
}
}
}
void preOrder(BTNode *p)
{
if (p != NULL)
{
cout << p->date;
preOrder(p->lchild);
preOrder(p->rchild);
}
}
void InOrder(BTNode *p)
{
if (p != NULL)
{
preOrder(p->lchild);
cout << p->date;
preOrder(p->rchild);
}
}
void postOrder(BTNode *p)
{
if (p != NULL)
{
preOrder(p->lchild);
preOrder(p->rchild);
cout << p->date;
}
}
void InTongji(BTNode*t, int &m, int&n)
{
if (t != NULL)
{
InTongji(t->lchild, m, n);
cout << t->date;
m++;
if ((t->lchild == NULL) && (t->rchild == NULL))
n++;
InTongji(t->rchild, m, n);
}
}
int CountLeaf(BTNode*T)
{
int m, n;
if (!T)return 0;
if (!T->lchild&&!T->rchild)return 1;
else
{
m = CountLeaf(T->lchild);
n = CountLeaf(T->rchild);
return(m + n);
}
}
int Count(BTNode *T)
{
int m, n;
if (!T)return 0;
if (!T->lchild&&!T->rchild)return 1;
else
{
m = Count(T->lchild);
n = Count(T->rchild);
return(m + n+1);
}
}
void createbitree(BTNode *&T)
{
int ch;
cin >> ch;
if (ch == 0)T = NULL;
else
{
T = new BTNode;
T->date = ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
}
BTNode *GetTreeNode(int item, BTNode *lptr, BTNode *rptr)
{
BTNode *T;
T = new BTNode;
T->date = item;
T->lchild = lptr;
T->rchild = rptr;
return T;
}
BTNode*CopyTree(BTNode *T)
{
BTNode*newlptr, *newrptr, *newT;
if (!T)return NULL;
if (T->lchild)
newlptr = CopyTree(T->lchild);
else
newlptr = NULL;
if (T->rchild)
newrptr = CopyTree(T->rchild);
else
newrptr = NULL;
newT = GetTreeNode(T->date, newlptr, newrptr);
return newT;
}
void output()
{
int i;
for (i = 0; i < 10; i++)
cout << " ";
for (i = 0; i < 32; i++)
cout << "*";
cout << endl;
}
void mainapp()
{
int i;
output();
for (i = 0; i < 10; i++)
cout << " ";
cout << "* ";
cout << "1.构造二叉树";
for (i = 0; i < 14; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "* ";
cout << "2.先序输出二叉树";
for (i = 0; i < 10; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10;i++)cout << " "; cout << "* ";
cout << "3.中序输出二叉树";
for (i = 0; i < 10; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "* ";
cout << "3.后序输出二叉树";
for (i = 0; i < 10; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "* ";
cout << "5.求二叉树的高度";
for (i = 0; i < 10; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "* ";
cout << "6.输出二叉树结点";
for (i = 0; i < 10; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "* ";
cout << "7.输出所有结点";
for (i = 0; i < 12; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "* ";
cout << "0.退出";
for (i = 0; i < 20; i++)cout << " "; cout << "*"; cout << endl;
for (i = 0; i < 10; i++)cout << " "; cout << "*";
cout << "*******************************" << endl;
}
void main()
{
int m = 0, n = 0, k = 1, t;
BTNode *root;
root = new BTNode;
mainapp();
t = 0;
while (k)
{
cout << "请输入0~7:" << endl;
cin >> m;
switch (m)
{
case 0:return;
case 1:cout << "构造二叉树:" << endl; createbitree(root); break;
case 2:cout << "先序输出二叉树各结点:"; preOrder(root); cout << endl; break;
case 3:cout << "中序输出二叉树各节点:"; InOrder(root); cout << endl; break;
case 4:cout << "后序输出二叉树各结点:"; postOrder(root); cout << endl; break;
case 5:t = BiTreeDepth(root); cout << "二叉树的高度是:" << t << endl; break;
case 6:cout << "输出所有叶子结点"; Displeaf(root); n = CountLeaf(root); cout << "叶子结点总数:" << n << endl; break;
case 7:cout << "输出所有结点" << endl; DispBiTree(root); cout << endl; break;
default:return;
}
cout << "继续?1or0:"; cin >> k;
if (!k)return;
}
}
二叉树的基本操作
最新推荐文章于 2019-08-30 19:44:30 发布