BJFUOJ

BJFUOJ

#include<bits/stdc++.h>
using namespace std;
bool flag = true;

typedef struct BiTnode {
	char data;
	struct BiTnode *lchild, *rchild;
} BiTnode, *BiTree;

void CreatTree(BiTree &l, char c) {
	if (l == NULL) {
		l = new BiTnode;
		l->data = c;
		l->lchild = NULL;
		l->rchild = NULL;
	} else if (l->data > c) {
		CreatTree(l->lchild, c);
	} else {
		CreatTree(l->rchild, c);
	}
}

int Hight(BiTree &l) {
	if (l == NULL) return 0;
	else if (l->lchild == NULL && l->rchild == NULL) {
		return 1;
	} else {
		return max(Hight(l->lchild), Hight(l->rchild)) + 1;
	}
}

bool  Judge(BiTree &l) {
	if (l == NULL) return true;
	queue <BiTree> q;
	q.push(l);
	bool leaf = false;
	while (!q.empty()) {
		BiTree p = q.front();
		q.pop();
		if ((p->rchild != NULL && p->lchild == NULL) || (leaf && (l->lchild != NULL || l->rchild != NULL))){
			return false;
		}
		if(l->lchild != NULL){
			q.push(l->lchild);
		}
		if(l->rchild){
			q.push(l->rchild);
		}
		if((p->lchild != NULL && p->rchild == NULL)||(p->rchild == NULL && p->lchild == NULL)){
			leaf = true;
		}
			
	}
	return true;

}

void OutPut(BiTree &l) {
	if (l) {
		OutPut(l->lchild);
		cout << l->data << " ";
		OutPut(l->rchild);
	}
}

void BracketsOut(BiTree &l){ //   未解决
	cout<<"(";
	if(l){
		cout<<l->data;
		if(l->lchild){
			BracketsOut(l->lchild);
		}
		if(l->rchild){
			cout<<",";
			BracketsOut(l->rchild);
		}
	}
	cout<<")";
}

void CreatLinklist(BiTree &l,BiTree &head,BiTree &r){
	if(l){
		CreatLinklist(l->lchild,head,r);
		CreatLinklist(l->rchild,head,r);
		if(l->lchild == NULL && l->rchild == NULL){
			if(flag){
				head = new BiTnode;
				r = l; 
				head->lchild = r;
				flag = false;
			}else{
				r->lchild = l;
				l->rchild = r;
				r = l; 
			}
		}
		r->lchild = NULL;
	}
}

void OutLeaf(BiTree &head){
	
	BiTree r = head->lchild;
	while(r){
		
		cout<<r->data<<" ";
		r = r->lchild;
	}
}

int main() {
	char s[100];
	cin >> s;
	BiTree l = NULL;
	for (int i = 0; i < strlen(s); i++) {
		CreatTree(l, s[i]);
	}
	OutPut(l);
	cout << endl;
	cout << Hight(l) << endl;
	if (Judge(l)) {
		cout << "是" << endl;
	} else {
		cout << "否" << endl;
	}
	BiTree head,r;
	CreatLinklist(l,head,r);
	OutLeaf(head);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值