#include<iostream>
using namespace std;
#define InitSize 20
typedef char TElemType;
typedef struct node {
struct node*lchild=NULL, *rchild=NULL;
TElemType data;
}TNode;
typedef struct {
TNode tnode[InitSize];
int curSize = 0, maxSize = InitSize;
}Tree;
//根据广义表产生树
void CreateTree(Tree&BT, char ch[], int n)
{
TNode*S[InitSize];
int top = -1;int bottom = 0;
BT.tnode[bottom].data = ch[0];
TNode*ptr = &BT.tnode[bottom++];
int dir = 0;
for (int i = 1;i < n;i++)
{
switch (ch[i]) {
case';':return;
case'(':S[++top] = ptr;dir = 0;break;
case')':top--;break;
case',':dir = 1;break;
default:
BT.tnode[bottom].data = ch[i];
if (!dir)S[top]->lchild = &BT.tnode[bottom];
else S[top]->rchild = &BT.tnode[bottom];
ptr = &BT.tnode[bottom++];
}
}
}
void GetNodeNumB(TNode*BT, int&n0, int&n1, int&n2)
{
if (BT->lchild != NULL && BT->rchild != NULL)
{
n2++;
GetNodeNumB(BT->lchild, n0, n1, n2);
GetNodeNumB(BT->rchild, n0, n1, n2);
}
else if (BT->lchild == NULL && BT->rchild != NULL)
{
n1++;
GetNodeNumB(BT->rchild, n0, n1, n2);
}
else if (BT->lchild != NULL && BT->rchild == NULL)
{
n1++;
GetNodeNumB(BT->lchild, n0, n1, n2);
}
else
n0++;
}
void GetNodeNum(Tree BT, int&n0, int&n1, int&n2)
{
GetNodeNumB(&BT.tnode[0], n0, n1, n2);
}
int GetTreeHeightSub(TNode*tree)
{
if (tree == NULL)return 0;
else {
int i = GetTreeHeightSub(tree->lchild);
int j = GetTreeHeightSub(tree->rchild);
return i >= j ? i + 1 : j + 1;
}
}
//得到树高度
void GetTreeHeight(Tree BT, int&h)
{
h=GetTreeHeightSub(&BT.tnode[0]);
}
void GetWidthSub(TNode*BT, int(&ch)[10], int n)
{
if (BT != NULL)
{
ch[n]++;
GetWidthSub(BT->lchild, ch, n + 1);
GetWidthSub(BT->rchild, ch, n + 1);
}
}
//获得最大宽度层
void GetWidth(Tree BT, int(&ch)[10], int n)
{
GetWidthSub(&BT.tnode[0], ch, n);
}
void DelNodeSub(TNode*BT,TNode*Parent,int dir)
{
if (BT != NULL)
{
if (BT->lchild == NULL && BT->rchild == NULL)
{
if (Parent != NULL)
{
if (!dir)Parent->lchild = NULL;
else Parent->rchild = NULL;
}
return;
}
DelNodeSub(BT->lchild, BT, 0);
DelNodeSub(BT->rchild, BT, 1);
}
}
//删除叶节点
void DelNode(Tree&BT)
{
TNode*parent = NULL;
TNode*ptr = &BT.tnode[0];
DelNodeSub(ptr,parent,0);
}
void DispTreeSub(TNode*root)
{
if (root != NULL)
{
cout << root->data;
if (root->lchild != NULL || root->rchild != NULL)
cout << '(';
DispTreeSub(root->lchild);
if (root->lchild != NULL && root->rchild != NULL)
cout << ',';
DispTreeSub(root->rchild);
if (root->lchild != NULL || root->rchild != NULL)
cout << ')';
}
}
void DispTree(Tree BT)
{
DispTreeSub(&BT.tnode[0]);
}
int FindInTree(TNode*root, TNode*ptr,int n)
{
if (root != NULL)
{
int i = -1,j=-1;
if (root == ptr)
return n;
else
{
i = FindInTree(root->lchild, ptr, n + 1);
j = FindInTree(root->rchild, ptr, n + 1);
return i >= j ? i : j;
}
}
else return -1;
}
//寻找目标节点
int FindInTree(Tree&BT,TNode*ptr)
{
int n = 1;
return FindInTree(&BT.tnode[0], ptr,n);
}
char GetMaxValueSub(TNode*BT)
{
TNode*S[InitSize],*ptr=NULL,*maxptr=NULL;
int front = 0, rear = 0;
S[++rear] = BT;
char max = 'A';
while (front != rear)
{
front = (front + 1) % InitSize;
ptr = S[front];
if (ptr->lchild != NULL)
{
rear = (rear + 1) % InitSize;
S[rear] = ptr;
}
if (ptr->rchild != NULL)
{
rear = (rear + 1) % InitSize;
S[rear] = ptr->rchild;
}
if (ptr->data > max)
{
max = ptr->data;
maxptr = ptr;
}
}
return max;
}
//获得节点最大值
char GetMaxValue(Tree BT)
{
return GetMaxValueSub(&BT.tnode[0]);
}
void DispValueSub(TNode*BT,int n)
{
if (BT != NULL)
{
cout << BT->data << ' ' << n << endl;
DispValueSub(BT->lchild, n + 1);
DispValueSub(BT->rchild, n + 1);
}
}
//显示节点数值以及节点层数
void DispValue(Tree BT)
{
int n = 1;
DispValueSub(&BT.tnode[0], n);
}
void ChangeChildSub(TNode*BT)
{
if (BT != NULL)
{
TNode*temp = NULL;
temp = BT->lchild;
BT->lchild = BT->rchild;
BT->rchild = temp;
ChangeChildSub(BT->lchild);
ChangeChildSub(BT->rchild);
}
}
//交换节点
void ChangeChild(Tree&BT)
{
ChangeChildSub(&BT.tnode[0]);
}
int main()
{
Tree BT;
int n0 = 0, n1 = 0, n2 = 0;
char ch[] = "A(B(C,D),E(F,G));";
CreateTree(BT, ch, sizeof(ch));
DispValue(BT);
cout << endl;
ChangeChild(BT);
DispTree(BT);
/*DispTree(BT);
cout << endl;
GetNodeNum(BT, n0, n1, n2);
cout << n0 << ' ' << n1 << ' ' << n2;cout << endl;
int height;
GetTreeHeight(BT, height);
cout <<height<<endl;
int width = 0, num[10] = {};
GetWidth(BT, num, 0);
for (int i = 0;i < sizeof(num)/sizeof(int);i++)
if (width < num[i])width = num[i];
cout << width<<endl;
DelNode(BT);
DispTree(BT);
cout << endl;
int m=FindInTree(BT,&BT.tnode[0]);
cout << m<<endl;
cout << GetMaxValue(BT);*/
system("pause");
}
C++树的各种操作
最新推荐文章于 2024-06-29 10:04:06 发布