编辑代码

#include<iostream>
using namespace std;
#define MAX 100

typedef struct {
    void *items[MAX];
    int front;
    int rear;
} Queue;

void InitQueue(Queue *q) {
    q->front = -1;
    q->rear = -1;
}

int IsEmpty(Queue *q) {
    return q->front == -1;
}

int IsFull(Queue *q) {
    return q->rear == MAX - 1;
}

void enqueue(Queue *q, void *value) {
    if (IsFull(q)) {
        printf("Queue is full\n");
        return;
    }
    if (IsEmpty(q)) {
        q->front = 0;
    }
    q->items[++q->rear] = value;
}

void *dequeue(Queue *q) {
    if (IsEmpty(q)) {
        printf("Queue is empty\n");
        return NULL; // 返回一个特殊值表示队列为空
    }
    void *item = q->items[q->front++];
    if (q->front > q->rear) {
        q->front = q->rear = -1;
    }
    return item;
}
//队列

//二叉树
typedef struct node
{
	char data;
	struct node * lchild;
	struct node * rchild;
}BiTree;

BiTree* newNode(char v) { 
	BiTree*Node =(BiTree*)malloc(sizeof(BiTree)); //申请一个node类型变量的地址空间
	Node->data = v; //结点权值为v
	Node->lchild = Node->rchild = NULL; //初始状态下无左右孩子
	return Node; //返回新节点的地址
}
//构造二叉排序树
BiTree* insert(BiTree *root, int x) {
	if (root == NULL) { //空树,即查找失败,插入结点(递归边界)
		root = newNode(x);
		return root;
	}
	else if (root->data > x) { //往左子树搜索
		root->lchild=insert(root->lchild, x);
	}
	else {
        root->rchild=insert(root->rchild, x); //往右子树搜索
    }
    return root;
}
//访问根节点
void visit(BiTree *root){
    printf("%c",root->data);
}
//前序遍历
void PreOrder(BiTree *root){
	if(root!=NULL) {
	visit(root); //访问根节点数据域
	PreOrder(root->lchild); //访问左子树
	PreOrder(root->rchild); //访问右子树
}
}
//中序遍历
void InOrder(BiTree *root){
    if(root!=NULL){
        InOrder(root->lchild);
        visit(root);
        InOrder(root->rchild);
    }
}
//后序遍历
void PostOrder(BiTree *root){
    if(root!=NULL){
        PostOrder(root->lchild);
        PostOrder(root->rchild);
        visit(root);
    }
}
//层序遍历
void LevelOrder(BiTree *root) {
    if (root == NULL) {
        printf("Tree is empty\n");
        return;
    }
    Queue q;
    InitQueue(&q);
    enqueue(&q, root);//根节点入队
    while (!IsEmpty(&q)) {
        BiTree *root = (BiTree *)dequeue(&q);//队头节点出队
        visit(root);
        //左孩子不为空,则左孩子入队
        if (root->lchild != NULL) {
            enqueue(&q, root->lchild);
        }
        //右孩子不为空。则右孩子入队
        if (root->rchild != NULL) {
            enqueue(&q, root->rchild);
        }
    }
}
int wplPreOrder(BiTree *root,int deep){
   static int wpl=0;
    if(root->lchild==NULL && root->rchild==NULL){
        wpl+=(root->data)*deep;//若为叶结点,则累积WPL
    }
    if(root->lchild!=NULL){
        wplPreOrder(root->lchild,deep+1);//如果左子树不为空,则对左子树进行递归遍历
    }
    if(root->rchild!=NULL){
        wplPreOrder(root->rchild,deep+1);//如果右子树不为空,则对右子树进行递归遍历
    }
    return wpl;//返回WPL值
}
void express(BiTree *root,int deep){
    if(root!=NULL){
        if(root->lchild==NULL && root->rchild==NULL){
        printf("%c",root->data);
        }
        else{
        if(deep>0){
            printf("(");
        }
        express(root->lchild,deep+1);
        visit(root);
        express(root->rchild,deep+1);
        if(deep>0){
            printf(")");
        }
        }
    }
}
typedef struct {
    int SqBitnode[MAX];
    int num;
}Sqbitree;
void increase(Sqbitree &t){
    t.SqBitnode[0]=
}
int main() {
    Sqbitree &t;
    BiTree *root=NULL;
    root=newNode('*');
    BiTree *s1=newNode('+');
    BiTree *s2=newNode('*');
    BiTree *s3=newNode('a');
    BiTree *s4=newNode('b');
    BiTree *s5=newNode('c');
    BiTree *s6=newNode('-');
    BiTree *s7=newNode('d');
    root->lchild=s1;
    root->rchild=s2;
    s1->lchild=s3;
    s1->rchild=s4;
    s2->lchild=s5;
    s2->rchild=s6;
    s6->rchild=s7;
    // root = insert(root, 50);
    // insert(root, 30);
    // insert(root, 20);
    // insert(root, 40);
    // insert(root, 70);
    // insert(root, 60);
    // insert(root, 80);
    //2017数据结构express(root,0);
    //2014数据结构 int weight=wplPreOrder(root,0);
    // printf("\n");
    // printf("%d",weight);
    return 0;
}