#include
#include<malloc.h>
#define maxsize 10
#define ElemType int
using namespace std;
typedef struct node
{
char date;
struct node* lchild;
struct node* rchild;
}btnode;
int main() {
btnode* b ;
char str[20];
int m;
char g;
cout << "请输入二叉树的字符数" << endl;
cin >> m;
cout << "shuru" << endl;
for (int i = 0; i < m; i++) {
cin >> str[i];
}
void fea(btnode*&, char str);
fea(b,str);
cout << "shuchu" << endl;
void shuchu(btnode*);
shuchu(b);
cout << "请输入需要查找的节点值" << endl;
cin >> g;
btnode find(btnode*, char);
btnode* h=find(b, g);
cout << "查找节点的左孩子"<<endl;
btnode* lnode(btnode );
cout<<lnode(h)->date;
cout << "右孩子" << endl;
btnode* rnode(btnode*);
cout<<rnode(h)->date;
cout << "高度" << endl;
int btheight(btnode);
cout<<btheight(b)<<endl;
}
void fea(btnode*& b, char str) {
b = (btnode)malloc(sizeof(btnode));
if (b != NULL) {
btnode* st[maxsize], * p = NULL;
int top = -1, k = 0, j = 0;
char ch;
b = NULL;
ch = str[j];
while (ch != '\0') {
switch (ch) {
case'(':top++; st[top] = p; k = 1; break;
case')':top--; break;
case',':k = 2; break;
default:p = (btnode*)malloc(sizeof(btnode));
if (p != NULL) {
p->date = ch;
p->lchild = p->rchild = NULL;
if (b == NULL)b = p;
else
{
switch (k)
{
case 1:st[top]->lchild = p; break;
case 2:st[top]->rchild = p; break;
}
}
}
j++;
ch = str[j];
}
}
}
}
btnode* find(btnodeb ,char x) {
btnode p;
if (b == NULL)return NULL;
else if (b->date == x)return b;
else
{
p = find(b->lchild, x);
if (p != NULL)return p;
else
return find(b->rchild, x);
}
}
btnode* lnode(btnode* p) {
return p->lchild;
}
btnode* rnode(btnode* p) {
return p->rchild;
}
int btheight(btnode* b) {
int lchildl, rchildl;
if (b == NULL)return(0);
else
{
lchildl = btheight(b->lchild);
rchildl = btheight(b->rchild);
return (lchildl > rchildl) ? (lchildl + 1) : (rchildl + 1);
}
}
void shuchu(btnode* b) {
if (b != NULL) {
cout << b->date;
if (b->lchild != NULL || b->rchild != NULL) {
cout << '(';
shuchu(b->lchild);
if (b->rchild != NULL)cout << ',';
shuchu(b->rchild);
cout << ')';
}
}
}