源程序:

#include <stdio.h>

#include <stdlib.h>

 

//二叉排序树节点描述

typedef int DataType;

typedef struct Node

{

  DataType key;

  struct Node *lchild, *rchild;

  struct Node *parent;  //指向父节点指针

}Node, *pNode;

 

//采用插入法创建一棵二叉树

void insert(pNode *root, DataType key)

{

  pNode p = (pNode)malloc(sizeof(Node));

  p->key = key;

  p->lchild = NULL;

  p->rchild = NULL;

  p->parent = NULL;

  if ((*root) == NULL)  //空树时,直接作为根节点

  {

    *root = p;

    return;

  }

 

  if ((*root)->lchild == NULL && (*root)->key > key)         //插入到当前节点(*root)的左孩子

  {

    p->parent = (*root);

    (*root)->lchild = p;

  return;

  }

  if ((*root)->rchild == NULL && (*root)->key < key)            //插入到当前节点(*root)的右孩子

  {

    p->parent = (*root);

    (*root)->rchild = p;

    return;

  }

  if ((*root)->key > key)

    insert(&(*root)->lchild, key);

  else if ((*root)->key < key)

    insert(&(*root)->rchild, key);

  else

    return;

}

 

void create(pNode *root, DataType *keyArray, int length)

{

  int i;

  for (i = 0; i<length; i++)

    insert(root, keyArray[i]);        //逐个节点插入二叉树

}

 

//查找元素

pNode search(pNode root, DataType key)

{

if (root == NULL)

  return NULL;

else if (key > root->key)

  return search(root->rchild, key);

else if (key < root->key)

  return search(root->lchild, key);

else

  return root;

}

 

void inordertraverse(pNode root)

{

  if (root)

  {

    inordertraverse(root->lchild);

    printf("%4d", root->key);

    inordertraverse(root->rchild);

  }

}

 

/////////////////////////////////////////////////////////////

//链队列的定义

typedef struct LinkQueueNode

{

  int data;

  struct LinkQueueNode *next;

}LkQueNode;

typedef struct LkQueue

{

  LkQueNode *front, *rear;

}LkQue;

//LkQue LQ;

 

//初始化链队列

void InitQueue(LkQue *LQ)

{

  LkQueNode *temp;

  temp = (LkQueNode *)malloc(sizeof(LkQueNode));

  LQ->front = temp;

  LQ->rear = temp;

  (LQ->front)->next = NULL;

}

 

//判断链队列空

int EmptyQueue(LkQue *LQ)

{

  return LQ->front == LQ->rear;

}

 

//入链队列

void EnQueue(LkQue *LQ, int x)

{

  LkQueNode *temp;

  temp = (LkQueNode *)malloc(sizeof(LkQueNode));

  temp->data = x;

  temp->next = NULL;

  (LQ->rear)->next = temp;

  LQ->rear = temp;

}

 

//出链队列

int outQueue(LkQue *LQ)

{

  LkQueNode *temp;

  if (EmptyQueue(LQ))

  {

    printf("空队列!");

    return 0;

  }

  else

  {

    temp = (LQ->front)->next;

    (LQ->front)->next = temp->next;

    if (temp->next == NULL)

    LQ->rear = LQ->front;

    free(temp);

    return 1;

  }

}

 

//取队列首元素

int Gethead(LkQue *LQ)

{

  LkQueNode *temp;

  if (EmptyQueue(LQ))

    return 0;

  else

  {

    temp = (LQ->front)->next;

    return temp->data;

  }

}

 

//按层次遍历二叉排序树,借助数组实现

void levelorder(pNode bt)

{

  pNode q[100];

  int front = 0, rear = 0;

  pNode p;

  if (bt == NULL)

    return;

  q[rear] = bt;

  rear = (rear + 1) % 100;

  while (front != rear)

  {

    p = q[front];

    front = (front + 1) % 100;

    printf("%4d", p->key);

    if (p->lchild)

    {

      q[rear] = p->lchild;

      rear = (rear + 1) % 100;

     }    

    if (p->rchild)

    {

      q[rear] = p->rchild;

      rear = (rear + 1) % 100;

    }

  }

}

/////////////////////////////////////////////////////////////

 

 

 

void main()

{

  pNode root = NULL;

  DataType nodeArray[11] = { 15,6,18,3,7,17,20,2,4,13,9 };

  int i;

  printf("待查找数据为:\n");

  for (i = 0; i<11; i++)

    printf("%4d", nodeArray[i]);

  create(&root, nodeArray, 11);

  printf("\n\n");

  printf("中序遍历输出二叉树结点的值\n");

  inordertraverse(root);

  printf("\n\n");

  //按层次遍历二叉排序树,需借助队列实现

  printf("按层次遍历二叉排序树\n");

  levelorder(root);

  printf("\n请输入要查找的元素的值:");

  DataType locate;

  scanf("%d", &locate);

  if (search(root, locate) != NULL)

    printf("这些数中有你要查找的数%d\n", search(root, locate)->key);

  else

    printf("这些数据中没有你要查找的数。\n");

  system("pause");

}

 运行结果:

二叉排序树,按层次遍历二叉树(用数组作缓冲)_链队列