二叉排序樹,按層次遍歷二叉樹(用數組作緩沖)


 源程序:

#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");

}

 運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM