1. 程式人生 > >中英互譯詞典(二叉搜尋樹)

中英互譯詞典(二叉搜尋樹)

一、BSTree.h

#ifndef __BSTREE_H__
#define __BSTREE_H__

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

typedef char* KeyType;
typedef char* ValueType;

typedef struct BSTreeNode
{
    struct BSTreeNode* _left;
    struct BSTreeNode* _right;
    KeyType _key;
    ValueType _value;
}BSTreeNode;

int
BSTreeInsertR(BSTreeNode** pptree, KeyType key, ValueType value); int BSTreeRemoveR(BSTreeNode** pptree, KeyType key); BSTreeNode* BSTreeFindR(BSTreeNode* ptree, KeyType key); void BSTreeInOrder(BSTreeNode* ptree); void BSTreeDestory(BSTreeNode* ptree); #endif __BSTREE_H__

二、BSTree.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "BSTree.h" //建立節點 BSTreeNode* BuyBSTreeNode(KeyType key, ValueType value) { BSTreeNode* cur = (BSTreeNode*)malloc(sizeof(BSTreeNode)); assert(cur); cur->_key = key; cur->_value = value; cur->_left = NULL; cur->_right = NULL; return cur; } //遞迴插入 int BSTreeInsertR(BSTreeNode** pptree, KeyType key, ValueType value) { if
(*pptree == NULL) { *pptree = BuyBSTreeNode(key, value); return 1; } if (strcmp((*pptree)->_key, key) > 0) { return BSTreeInsertR(&(*pptree)->_left, key, value); } else if (strcmp((*pptree)->_key, key) < 0) { return BSTreeInsertR(&(*pptree)->_right, key, value); } else { return 0; } } //遞迴刪除 int BSTreeRemoveR(BSTreeNode** pptree, KeyType key) { if (*pptree == NULL) { return 0; } if (strcmp((*pptree)->_key, key) > 0) { return BSTreeRemoveR(&(*pptree)->_left, key); } else if (strcmp((*pptree)->_key, key) < 0) { return BSTreeRemoveR(&(*pptree)->_right, key); } else { //1.左為空 //2.右為空 //3.左右都不為空 if ((*pptree)->_left == NULL) { *pptree = (*pptree)->_right; return 1; } else if ((*pptree)->_right == NULL) { *pptree = (*pptree)->_left; return 1; } else { BSTreeNode* subleft = (*pptree)->_right; while (subleft->_left) { subleft = subleft->_left; } strcpy((*pptree)->_key, subleft->_key); return BSTreeRemoveR(&(*pptree)->_right, subleft->_key); } } } //遞迴查詢 BSTreeNode* BSTreeFindR(BSTreeNode* ptree, KeyType key) { if (ptree == NULL) { return NULL; } if (strcmp(ptree->_key, key) > 0) { return BSTreeFindR(ptree->_left, key); } else if (strcmp(ptree->_key, key) < 0) { return BSTreeFindR(ptree->_right, key); } else { return ptree; } } //中序遍歷 void BSTreeInOrder(BSTreeNode* ptree) { if (ptree == NULL) { return; } BSTreeInOrder(ptree->_left); printf("[%s %s] ", ptree->_key, ptree->_value); BSTreeInOrder(ptree->_right); } //銷燬 void BSTreeDestory(BSTreeNode* ptree) { if (ptree == NULL) { return; } BSTreeDestory(ptree->_left); BSTreeDestory(ptree->_right); free(ptree); }

三、Test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "BSTree.h"

void Menu()
{
    printf("****************\n");
    printf("**            **\n");
    printf("**   1.查詢   **\n");
    printf("**   0.退出   **\n");
    printf("**            **\n");
    printf("****************\n");
}

int main()
{
    BSTreeNode* tree = NULL;

    BSTreeInsertR(&tree, "tree", "樹");
    BSTreeInsertR(&tree, "sort", "排序");
    BSTreeInsertR(&tree, "binary", "二分");
    BSTreeInsertR(&tree, "return", "返回");
    BSTreeInsertR(&tree, "hash", "雜湊");
    BSTreeInsertR(&tree, "list", "連結串列");

    int op = 0;
    do{
        Menu();
        printf("請選擇:");
        scanf("%d", &op);
        switch (op)
        {
        case 1:
        {
                  fflush(stdin); //清除記憶體緩衝區
                  char* str = (char*)malloc(10 * sizeof(char));
                  printf("請輸入查詢單詞:");
                  gets(str);
                  BSTreeNode* cur = BSTreeFindR(tree, str);
                  if (cur)
                  {
                      printf("翻譯結果為:%s\n", cur->_value);
                  }
                  else
                  {
                      printf("未找到該詞\n");
                  }
                  free(str);
                  str = NULL;
        }
            break;
        case 0:
        {
                  BSTreeDestory(tree);
                  system("pause");
                  return 0;
        }
            break;
        }
    } while (op);
}