C++二叉树排序代码 最好是从大到小排列的 谢谢了

C++二叉树排序代码 有的给一个 谢谢 各位大神了
2025-05-09 01:54:52
推荐回答(2个)
回答1:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "malloc.h"

typedef struct _student {
char number[20];
char name[20];
int score;
}Student;

typedef struct _node {
Student student;
_node *lchild, *rchild;
}Node, *PNode, BSTree, *PBSTree;

void BSTreeInsert(PBSTree *root, Student s) {
if(*root == NULL) {
*root = (PBSTree) malloc (sizeof(BSTree));
strcpy((*root)->student.number, s.number);
strcpy((*root)->student.name, s.name);
(*root)->student.score = s.score;
(*root)->lchild = NULL;
(*root)->rchild = NULL;
return;
}
else if(s.score < (*root)->student.score)
BSTreeInsert(&((*root)->lchild), s);
else
BSTreeInsert(&((*root)->rchild), s);
}

void InOrder(PBSTree root) {
if(!root) return;
InOrder(root->lchild);
printf("%s\t%s\t%d\n", root->student.number, root->student.name, root->student.score);
InOrder(root->rchild);
}

void main( ) {
int i, j, n, _class;
PBSTree bstree[20];
Student s;
for(i = 0; i < 20; i++) bstree[i] = NULL;
printf("请输入班级数!\n");
scanf("%d", &_class);
for(j = 0; j < _class; j++) {
printf("请输入%d班的学生人数!\n", j+1);
scanf("%d", &n);
printf("请输入学生信息:学号 姓名 成绩\n");
for(i = 0; i < n; i++) {
scanf("%s %s %d", s.number, s.name, &s.score);
BSTreeInsert(&bstree[i], s);
}

printf("\n从低分到高分排序后的结果为:\n");
InOrder(bstree[j]);
}
getch( );
}

回答2:

#include "stdio.h" #include "stdlib.h" #include "string.h" #include "conio.h" #include "malloc.h" typedef struct _student Student; typedef struct _node { Student student; _node *lch...3912