#include<iostream>
using namespace std;
typedef int Elementype ;
typedef char TElemType;
typedef struct BiNode {
TElemType data;
struct BiNode* lchild;
struct BiNode *rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree& T)
{
char ch;
cin >> ch;
if (ch == '*')
T = NULL;
else {
T = new BiTNode;
T->data = ch;
cout << "finished" << endl;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//先序便利
void PreOrder(BiTree T)
{
if (T != NULL)
{
cout << T->data;
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void Inorder(BiTree T)
{
if (T != NULL)
{
Inorder(T->lchild);
cout << T->data ;
Inorder(T->rchild);
}
}
void lasorder(BiTree T)
{
if (T != NULL)
{
lasorder(T->lchild);
lasorder(T->rchild);
cout << T->data ;
}
}
//统计叶子节点个数
void countleaf(BiTree T, int &num)
{
if (T)
{
if (!T->lchild && !T->rchild)
{
num++;
}
countleaf(T->lchild,num);
countleaf(T->rchild,num);
}
}
//求出元素e的左孩子
void ChildX(BiTree T, TElemType e)
{
if (T)
{
if (T->data == e)
{
if (T->lchild)
cout << "左孩子为" <<T->lchild->data<< endl;
else
cout << "无" << endl;
return;
}
ChildX(T->lchild, e);
ChildX(T->rchild, e);
}
}
//求二叉树的高度
int Getheight(BiTree T)
{
int leftheight = 0;
int rightheight = 0;
int height = 0;
if (T)
{
leftheight=Getheight(T->lchild);
rightheight=Getheight(T->rchild);
return max(leftheight, rightheight) + 1;
}
else {
return 0;
}
}
//求e的双亲
void ParentX(BiTree T, Elementype e)
{
if (T)
{
if ((T->lchild && T->lchild->data == e) ||
(T->rchild && T->rchild->data == e))
{
cout << "e的双亲为"<<T->data << endl;
}
ParentX(T->lchild, e);
ParentX(T->rchild, e);
}
}
//求某个节点的兄弟
void GetSibiling(BiTree T,Elementype e)
{
if (T)
{
if (T->lchild && T->lchild->data == e )
{
cout << "该节点的兄弟为" << T->rchild->data << endl;
}
if (T->rchild && T->rchild->data == e)
{
cout<< "该节点的兄弟为" << T->lchild->data << endl;
}
GetSibiling(T->lchild, e);
GetSibiling(T->rchild, e);
}
return;
}
//求某个节点的孩子
void Getchild(BiTree T,Elementype e)
{
if (T)
{
if (T->data == e)
{
cout << "该节点的左孩子为" << T->lchild->data << endl;
cout << "该节点的右孩子为" << T->rchild->data << endl;
return;
}
Getchild(T->lchild, e);
Getchild(T->rchild, e);
}
return;
}
int main()
{
BiTree bt = NULL;//创建一个空树
//cout<<"输入先序二叉树结点(子树为空输入*)"<<endl;
// CreateBiTree(bt);
// cout<<"输出先序遍历:"<<endl;
// PreOrder(bt);
// cout<<("输出中序遍历:")<<endl;
// Inorder(bt);
// cout<<"输出后序遍历:"<<endl;
// lasorder(bt);
while (true) {
cout << "1.输入先序遍历顺序创建二叉树" << endl;
cout << "2.前序遍历已有二叉树" << endl;
cout << "3.中序遍历已有二叉树" << endl;
cout << "4.后中序遍历已有二叉树" << endl;
cout << "5.求二叉树高度" << endl;
cout << "6.求叶子节点个数" << endl;
cout << "7.求某一节点的孩子" << endl;
cout << "8.求某个节点的双亲" << endl;
cout << "9.求某个节点的兄弟" << endl;
int choice = 0;
cin >> choice;
switch (choice)
{
case 1:
{
CreateBiTree(bt);
cout << endl;
break;
}
case 2:
{
PreOrder(bt);
cout << endl;
break;
}
case 3:
{
Inorder(bt);
cout << endl;
break;
}
case 4:
{
lasorder(bt);
cout << endl;
break;
}
case 5:
{
int height=Getheight(bt);
cout << "高度为" << height << endl;
break;
}
case 6:
{
int num = 0;
countleaf(bt, num);
cout << "叶节点数为" << num << endl;
break;
}
case 7:
{
char e;
cin >> e;
Getchild(bt, e); break;
}
case 8:
{
char e;
cin >> e;
ParentX(bt, e); break;
}
case 9: {
char e;
cin >> e;
GetSibiling(bt, e);
break;
}
default:
break;
}
}
return 0;
}
以上为完整代码。
本人还是小白,有不足的地方希望大佬在评论区指正。