二叉排序树算法

1900阅读 0评论2013-01-19 梦醒潇湘love
分类:C/C++

二叉排序树的创建,查找与删除,以下代码是其所有左子树的结点均小于根的值,其右子树的值均小于其根的值。

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. typedef int datatype;
  4. #define ENDKEY 0
  5. typedef struct BSTree
  6. {
  7.     datatype data;
  8.     struct BSTree *lchild;
  9.     struct BSTree *rchild;
  10. }BSTreenode,*BSTree;
  11. typedef struct node
  12. {
  13.     BSTree BST;
  14.     
  15.     struct node *next;
  16. }QueueNode;
  17. typedef struct Queue
  18. {
  19.     QueueNode *front;
  20.     QueueNode *tail;
  21. }Queue;
  22. void InitQueue(Queue *Q)
  23. {
  24.     Q->front=(QueueNode *)malloc(sizeof(QueueNode));
  25.     if(Q->front)/*初始化为一个空的队列*/
  26.     {
  27.         Q->tail=Q->front;
  28.         Q->front->next=NULL;

  29.     }
  30. }
  31. void EnterQueue(Queue *Q,BSTree p)
  32. {
  33.     QueueNode *newnode;
  34.     newnode=(QueueNode *)malloc(sizeof(QueueNode));
  35.     if(newnode)
  36.     {
  37.         newnode->BST=p;
  38.         newnode->next=NULL;
  39.         Q->tail->next=newnode;
  40.         Q->tail=newnode;

  41.     }
  42. }
  43. void DeQueue(Queue *Q,BSTree *p)
  44. {
  45.     QueueNode *q;
  46.     if(Q->front==Q->tail)
  47.         return ;
  48.     q=Q->front->next;
  49.     Q->front->next=q->next;
  50.     if(Q->tail==q)
  51.         Q->tail=Q->front;
  52.     *p=q->BST;
  53.     free(q);
  54. }
  55. int Empty(Queue Q)
  56. {
  57.     if(Q.front==Q.tail)
  58.         return 1;
  59.     return 0;
  60. }
  61. void Output(BSTree BT)//利用队列层析显示二叉排序树的初始状态元素
  62. {
  63.     Queue Q;
  64.     
  65.     BSTree p,q;
  66.     InitQueue(&Q);
  67.     q=p=BT;
  68.     if(p)
  69.         EnterQueue(&Q,p);
  70.     while(!Empty(Q))
  71.     {
  72.         DeQueue(&Q,&q);
  73.         printf("%3d",q->data);
  74.         if(q->lchild)
  75.             EnterQueue(&Q,q->lchild);
  76.         if(q->rchild)
  77.             EnterQueue(&Q,q->rchild);
  78.     }
  79.     
  80. }
  81. void InsertBT(BSTree *BT,datatype key)//插入方式建立二叉树
  82. {
  83.     BSTreenode *bst;
  84.     if((*BT)==NULL)
  85.     {
  86.         bst=(BSTreenode *)malloc(sizeof(BSTreenode));
  87.         bst->data=key;
  88.         bst->lchild=NULL;
  89.         bst->rchild=NULL;
  90.         *BT=bst;
  91.     }
  92.     else if(key<(*BT)->data)//小于根的元素放到左子树
  93.     {
  94.         InsertBT(&((*BT)->lchild),key);
  95.     }
  96.     else if(key>(*BT)->data)//大于根的元素放到右子树
  97.     {
  98.         InsertBT(&((*BT)->rchild),key);
  99.     }

  100.     
  101. }
  102. void Create_BSTree(BSTree *BT)
  103. {
  104.     datatype key;
  105.     (*BT)=NULL;
  106.     printf("Input the key (input 0 to end the input ):");
  107.     scanf("%d",&key);
  108.     while(key!=ENDKEY)
  109.     {
  110.         InsertBT(BT,key);
  111.         scanf("%d",&key);
  112.     }
  113. }
  114. void InorderOutput(BSTree *BT)//利用中序遍历输出二叉树的元素
  115. {
  116.     if(*BT)
  117.     {
  118.         InorderOutput(&((*BT)->lchild));
  119.         printf("%3d",(*BT)->data);
  120.         InorderOutput(&((*BT)->rchild));
  121.     }
  122. }
  123. BSTree FindBT(BSTree BT,int key)
  124. {
  125.     BSTree p;
  126.     p=BT;
  127.     while(p)
  128.     {
  129.         if(p->data==key) return p;
  130.         else if(key<p->data) p=p->lchild;
  131.         else p=p->rchild;
  132.     }
  133.     return NULL;
  134. }
  135. BSTree DeleteKey(BSTree BT,int key)
  136. {
  137.     BSTree f,p,s,q;
  138.     p=BT;
  139.     f=NULL;
  140.     while(p)
  141.     {
  142.         if(p->data==key) break;
  143.     
  144.         f=p;
  145.         if(p->data<key) p=p->rchild;
  146.         else
  147.             p=p->lchild;
  148.     }
  149.     if(p==NULL)
  150.         printf("Cannot find the data\n");
  151.     if(p->lchild==NULL&&p->rchild==NULL)//如果p是叶子节点
  152.     {
  153.         if(f)
  154.         {
  155.             if(f->lchild==p)
  156.                 f->lchild=NULL;
  157.             else
  158.                 f->rchild=NULL;
  159.         }
  160.         free(p);
  161.         printf("the data has been deleted\n");
  162.         return BT;
  163.     }
  164.     if(p->lchild==NULL)//要找的结点没有左子树
  165.     {
  166.         if(f==NULL)
  167.             BT=p->rchild;
  168.         else if(f->lchild==p)
  169.             f->lchild=p->lchild;
  170.         else
  171.             f->rchild=p->rchild;
  172.         free(p);
  173.     }
  174.     else//要找的结点有左子树
  175.     {
  176.         q=p;
  177.         s=p->lchild;
  178.         while(s->rchild)
  179.         {
  180.             q=s;
  181.             s=s->rchild;
  182.         }
  183.         if(p==q) q->lchild=s->lchild;
  184.         else
  185.             q->rchild=s->lchild;
  186.         p->data=s->data;
  187.         free(s);
  188.         
  189.     }
  190.     return BT;


  191. }
  192. void main()
  193. {
  194.     BSTreenode *BT,*find,*BST;
  195.     int key;
  196.     int Delete_key;
  197.     Create_BSTree(&BT);//创建一颗二叉树
  198.     Output(BT);
  199.     printf("\nafter Inorder the tree as follow :\n");
  200.     InorderOutput(&BT);//中序遍历可以把二叉树按递增的顺序输出
  201.     printf("\nInput the data you wan to find :");
  202.     scanf("%d",&key);
  203.     find=FindBT(BT,key);
  204.     if(find)

  205.         printf("find the data %d\n",find->data);
  206.     else
  207.         printf("cannot find the data\n");
  208.     printf("Input the data you want to delete: ");
  209.     scanf("%d",&Delete_key);
  210.     BST=DeleteKey(BT,Delete_key);
  211.     InorderOutput(&BST);//删除后再次显示二叉树序列
  212.     printf("\n");
  213. }


熟练掌握二叉排序树的算法。
上一篇:数据结构之线索二叉树
下一篇:数据结构之红黑树的实现与剖析