C資料結構-二叉搜尋樹BSTree
阿新 • • 發佈:2018-12-21
二叉搜尋樹BSTree
#ifndef BSTREE_H #define BSTREE_H #ifndef NULL #define NULL 0 #endif /* 元素型別 */ typedef int elem_t; /* 節點結構體 */ typedef struct tag_bstree_node { elem_t data; /* 資料域 */ struct tag_tree_node *lchild; /* 左孩子指標 */ struct tag_tree_node *rchild; /* 右孩子指標 */ }bstree_node,bstree; /** * 建立bstree * @return 返回NULL */ bstree *bstree_create(); /** * 建立新節點 * @param pe * @return */ bstree_node *bstree_create_node(elem_t *pe); /** * 插入新節點 * @param root * @param pnode * @return */ bstree *bstree_insert(bstree *root, bstree_node *pnode); /** * 刪除節點 * @param root * @param pe * @return */ bstree_node *bstree_remove(bstree_node *root,elem_t *pe); /** * 查詢節點 * @param ptree * @param pe * @return */ bstree_node *bstree_find(bstree *ptree, elem_t *pe); /** * 銷燬bstree * @param ptree */ void bstree_destroy(bstree *ptree); /** * 先序遍歷 * @param root * @param handler 處理函式指標 */ void bstree_pre_travel(bstree_node *root,void (*handler)(bstree_node *)); /** * 中序遍歷 * @param root * @param handler 處理函式指標 */ void bstree_mid_travel(bstree_node *root,void (*handler)(bstree_node *)); /** * 後序遍歷 * @param root * @param handler 處理函式指標 */ void bstree_post_travel(bstree_node *root,void (*handler)(bstree_node *)); #endif // BSTREE_H
#include "BSTree.h" #include <malloc.h> /** * 建立bstree * @return 返回NULL */ bstree *bstree_create() { return NULL; } bstree_node *bstree_create_node(elem_t *pe) { bstree_node *pnode = (bstree_node *)malloc(sizeof(bstree_node)); if(pnode) { pnode->data = *pe; pnode->lchild = NULL; pnode->rchild = NULL; } return pnode; } bstree *bstree_insert(bstree_node *root, bstree_node *pnode) { if(pnode == NULL) return root; else if(root == NULL) return pnode; if(pnode->data > root->data) { root->rchild = bstree_insert(root->rchild,pnode); } else if(pnode->data < root->data) { root->lchild = bstree_insert(root->lchild,pnode); } else { free(pnode); pnode = NULL; } return root; } bstree_node *bstree_remove(bstree_node *root,elem_t *pe) { /* 遞迴所有都沒有找到,則返回NULL */ if(root == NULL) return NULL; /* 遞迴找到待刪除的節點 */ if(*pe > root->data) { root->rchild = bstree_remove(root->rchild,pe); } else if(*pe < root->data) { root->lchild = bstree_remove(root->lchild,pe); } /* 如果不大不小就是找到了 */ else { bstree_node *tmp; /* 判斷是否有左子樹 */ if(root->lchild != NULL) { for(tmp = root->lchild;tmp->rchild != NULL;tmp = tmp->rchild); /* 將左子樹中最大值替換到被刪除的節點位置 */ root->data = tmp->data; /* 刪除原tmp位置的元素 */ root->lchild = bstree_remove(root->lchild,pe); } /* 判讀是否有右子樹 */ else if(root->lchild != NULL) { for(tmp = root->rchild;tmp->lchild != NULL;tmp = tmp->lchild); root->data = tmp->data; root->rchild = bstree_remove(root->rchild,pe); } /* 沒有孩子節點,直接釋放該節點 */ else { free(root); root = NULL; return NULL; } } return root; } bstree_node *bstree_find(bstree_node *root, elem_t *pe) { if(root == NULL) return NULL; if(*pe > root->data) return bstree_find(root->rchild,pe); else if(*pe < root->data) return bstree_find(root->lchild,pe); else return root; } void bstree_destroy(bstree *ptree) { if (ptree == NULL) return; if(ptree->lchild != NULL) { bstree_destroy(ptree->lchild); } if(ptree->rchild != NULL) { bstree_destroy(ptree->rchild); } if(ptree->lchild == NULL && ptree->rchild == NULL) { free(ptree); } } void bstree_pre_travel(bstree_node *root,void (*handler)(bstree_node *)) { if(root == NULL) return; handler(root); bstree_pre_travel(root->lchild,handler); bstree_pre_travel(root->rchild,handler); } void bstree_mid_travel(bstree_node *root,void (*handler)(bstree_node *)) { if(root == NULL) return; bstree_mid_travel(root->lchild,handler); handler(root); bstree_mid_travel(root->rchild,handler); } void bstree_post_travel(bstree_node *root,void (*handler)(bstree_node *)) { if(root == NULL) return; bstree_post_travel(root->lchild,handler); bstree_post_travel(root->rchild,handler); handler(root); }