Using the binary tree traversal ordering module is designed to achieve student achievement (Binary Sort Tree)

Source:

#include <stdio.h>
#include <stdlib.h>

typedef struct tnode
{
  int id;
  int score;
  struct tnode *lchild,*rchild;
}stu;

void ins_student(stu **p,long id,int score)
{
  stu *s;
  if(*p==NULL)
  {
    s=(stu *)malloc(sizeof(stu)); //插入学生信息
    s->id=id;
    s->score=score;
    s->lchild=NULL;
    s->rchild=NULL;
    *p=s;
  }
  else if(score<(*p)->score)
    ins_student(&((*p)->lchild),id,score);
  else
    ins_student(&((*p)->rchild),id,score);
}

// create a binary sort tree

* create_student STU ()
{
  int the above mentioned id, Score;
  STU * root;
  root = NULL;
  printf ( "Please enter the student number and scores (with separated, 0 end)!");
  printf ( "\ the n----- -------------------------- \ the n-");
  printf (" student number, the results: ");
  scanf ("% LD,% d ", & ID, & score);
  the while (score = 0)!
  {
    ins_student (& the root, ID, score);
    the printf (" student number, grade: ");
    Scanf ("% LD,% D ", & ID, & score);  
  }
  the printf ( "\ n------------------------------- \ n-");
  return the root;
}

In order void in_order (stu * bt) // recursive binary tree traversal
{
  IF (BT = NULL!)
  {
    in_order (BT-> lchild);
    the printf ( "% LD,% D \ n-", BT-> ID, BT -> Score);
    in_order (BT-> rchild);
  }
}

main void ()
{
  STU * the root;
  the root create_student = ();
  the printf ( "sorted results: \ n-");
  the printf ( "student number, grade: \ n-");
  in_order (the root);
}

operation result:

 

Guess you like

Origin www.cnblogs.com/duanqibo/p/11770060.html