二叉查找树(binary search tree)上的基本操作
2010-05-04 11:28
323 查看
二叉查找树:是这样一种树,它满足对对任意一个节点,其左儿子的值<该节点的值<其右儿子的值,因此按中序(inorder)遍历一个二叉查找树时,元素正好按升序排列。下面是关于二叉查找树的插入,删除,中序遍历等基本操作的实现。
1. BST.h
2 .BST.cpp
3.main.cpp
4.结果
附注
1.程序来源于http://oz.nthu.edu.tw/~d947207/chap9_tree.pdf
1. BST.h
#ifndef BINARY_SEARCH_TREE #define BINARY_SEARCH_TREE typedef int treeEle;//integer binary tree typedef struct BinNode { treeEle data; BinNode *left; BinNode *right; } BinNode; typedef BinNode* BinNodePtr; typedef struct { BinNodePtr root; } BST; //allocate a new BinNode with data-val BinNodePtr newBinNode(treeEle val); //construct an empty BST BST* BST_init(void); //return 1 if BST is empty // 0 otherwise int empty(BST* ); //return 1 if item is in the BST // 0 other wise int search(BST *tree,treeEle item); //insert a new item in the BST and maintain BST property void insert(BST *tree,treeEle item); //traverse BST: inorder LNR void traverse_inorder(BST *tree); void traverse_inorder_aux(BinNodePtr node); //remove node in BST void remove(BST* tree,treeEle item); //locates node containing an item and its parent int search2(BST* tree,treeEle item,BinNodePtr *locPtr,BinNodePtr *parent); #endif //BINARY_SEARCH_TREE
2 .BST.cpp
//allocate a new BinNode with data = val BinNodePtr newBinNode(treeEle val) { BinNodePtr node = (BinNodePtr)malloc(sizeof(BinNode)); assert(node); node->data = val; node->left = NULL; node->right = NULL; return node; } //construct an empty BST BST* BST_init(void) { BST *tree =(BST *)malloc(sizeof(BST)); assert(tree); tree->root = NULL; return tree; } //return 1 if BST is empty,0 otherwise int empty(BST* tree) { assert(tree); return(NULL == tree->root); } //return 1 if value is in the BST, 0 otherwise int search(BST *tree,treeEle item) { assert(tree); BinNodePtr locPtr = tree->root; int found = 0; while(1) { if(found || (NULL == locPtr)) break; if(item < locPtr->data) locPtr = locPtr->left; else if(item > locPtr->data) locPtr = locPtr->right; else found = 1; } return found; } //insert a new item in the BST and maintain BST property void insert(BST *tree,treeEle item) { assert(tree); BinNodePtr locPtr = tree->root; BinNodePtr parent = NULL; int found = 0; while(1) { if(found || (NULL == locPtr)) break; parent = locPtr; if(item < locPtr->data) locPtr = locPtr->left; else if(item > locPtr->data) locPtr = locPtr->right; else found = 1; } if (found) printf("Item already in the tree/n"); else { locPtr = newBinNode((item)); if (NULL == parent) tree->root = locPtr; else if (item < parent->data) parent->left = locPtr; else parent->right = locPtr; } } //traverse BST inorder: LNR void traverse_inorder(BST *tree) { assert(tree); traverse_inorder_aux(tree->root); printf("/n"); } void traverse_inorder_aux(BinNodePtr node) { if(NULL == node) return; traverse_inorder_aux(node->left); printf("%d ",node->data); traverse_inorder_aux(node->right); } //remove node in BST void remove(BST *tree,treeEle item) { int found; BinNodePtr x,parent; found = search2(tree,item,&x,&parent); if(!found) { printf("Item is not in the BST/n"); return; } if ((NULL != x->left) && (NULL != x->right)) { //x has two children //find x's inorder successor and its parent BinNodePtr xsucc = x->right; parent = x; while( NULL != xsucc->left) { parent = xsucc; xsucc = xsucc->left; } //move content of xsucc to x and change x to //point to successor which will be deleted x->data = xsucc->data; x = xsucc; } //if x has two children //proceed with case where node x has 0/1 child BinNodePtr subtree = x->left; if(NULL == subtree) subtree = x->right; if (NULL == parent) tree->root = subtree; else if(x == parent->left) parent->left = subtree; else parent->right = subtree; free(x); } //locates node containing an item and its parent int search2(BST* tree,treeEle item,BinNodePtr *locPtr,BinNodePtr *parent) { assert(tree); *locPtr = tree->root; *parent = NULL; int found = 0; while(1) { if(found || (NULL == locPtr)) break; if(item < (*locPtr)->data ) { *parent = *locPtr; *locPtr = (*locPtr)->left; } else if(item > (*locPtr)->data ) { *parent = *locPtr; *locPtr = (*locPtr)->right; } else found = 1; } return found; }
3.main.cpp
#include <stdio.h> #include <tchar.h> #include <stdlib.h> #include <assert.h> #include "BST.h" int _tmain(int argc, _TCHAR* argv[]) { BST* tree = BST_init(); if (empty(tree)) { printf("Tree is empty/n"); } insert(tree,49); insert(tree,28); insert(tree,13); insert(tree,35); insert(tree,66); insert(tree,62); insert(tree,80); traverse_inorder(tree); if(search(tree,80)) printf("80 is in the tree/n"); if(!search(tree,12)) printf("12 is NOT in the tree/n"); remove(tree,66); traverse_inorder(tree); return 0; }
4.结果
附注
1.程序来源于http://oz.nthu.edu.tw/~d947207/chap9_tree.pdf
相关文章推荐
- 二叉查找树(Binary Search Tree)相关操作
- 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】
- 基于数组的二叉查找树 Binary Search Tree (Java实现)
- 基本数据结构之Binary Search Tree
- 二叉查找树(binary search tree (BST))--算法导论示例
- 根据有序数组构造二叉查找树 Convert Sorted Array to Binary Search Tree
- Binary Search Tree(二叉搜索树、二叉查找树、二叉排序树)
- 二叉搜索树;二叉查找树;二叉排序树;binary search tree(BST)
- 【二叉查找树】05根据升序的链表构造二叉查找树【Convert Sorted List to Binary Search Tree】
- ※数据结构※→☆非线性结构(tree)☆============二叉搜索树(二叉查找树) 顺序存储结构(tree Binary Search sequence)(二十四)
- leetcode:Recover Binary Search Tree (修正二叉查找树错误节点,不改变结构)【面试算法题】
- Leetcode 108 Convert Sorted Array to Binary Search Tree 有序数组构造平衡二叉查找树
- Convert Sorted Array to Binary Search Tree (递增数组建高度平衡的二叉查找树)【leetcode】
- 根据单链表构造二叉查找树 Convert Sorted List to Binary Search Tree
- 【LeetCode】Validate Binary Search Tree 验证二叉查找树 - Medium+
- 【LeetCode】Validate Binary Search Tree 二叉查找树的判断
- BinarySearchTree(1)二叉查找树链式递归实现
- 自己写GoBinarySearchTree 二叉查找树
- 二叉搜索树的查询、插入与删除操作(Binary Search Tree, Search, Insert, Delete)(C++)
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点