二叉排序树/二叉查找树 (binary sort tree/ binary search tree)的C语言实现
2014-02-20 20:41
302 查看
二叉排序树(Binary Sort Tree)又称二叉查找树,亦称二叉搜索树。 它或者是一棵空树;或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树;
我们假设用二叉链表进行存储;二叉链表结点。有三个域:一个数据域,两个分别指向左右子结点的指针域。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 9
typedef int ElemType;
typedef struct BSTNode
{
ElemType data ;
struct BSTNode *Lchild, *Rchild;
}BSTNode;
//查找关键字key,迭代版本
BSTNode* SearchBST(BSTNode* &T, ElemType key)
{
BSTNode* p=T;
while( p )
{
if( key == p->data) return p; //查找成功,返回p
if( key < p->data)
p=p->Lchild; //在左子树中继续查找
else
p=p->Rchild; //在右子树中继续查找
}
return NULL; //T为空
}
//查找关键字key,递归版本
BSTNode* Recur_SearchBST(BSTNode* &T, ElemType key)
{
if (NULL==T || key==T->data)
return T;
if ( key< T->data)
return Recur_SearchBST(T->Lchild,key);
else
return Recur_SearchBST(T->Rchild,key);
}
//插入关键字key
void InsertBST(BSTNode* &T,ElemType key)
{
if(T==NULL)
{
T=(BSTNode*)malloc(sizeof(BSTNode));
T->Lchild=T->Rchild=NULL;
T->data=key;
return;
}
if(key < T->data )
InsertBST(T->Lchild,key);
else
InsertBST (T->Rchild, key );
}
//n个数据在数组d中,tree为二叉排序树根
void CreateBiTree(BSTNode* &T,ElemType d[ ] ,int n)
{
T=NULL;
for(int i=0;i<n;i++)
InsertBST(T,d[i]);
}
BSTNode* DeleteNode(BSTNode* &T)
{
BSTNode* p=T;
if (p->Lchild)
{
BSTNode* L = p->Lchild; //L指向其左子树;
BSTNode* prer = p->Lchild; //prer指向其左子树;
while(L->Rchild != NULL)//搜索左子树的最右边的叶子结点L
{
prer = L;
L = L->Rchild;
}
p->data = L->data;
if(prer != L)//若L不是p的左孩子,把L的左孩子作为L的父亲的右孩子
prer->Rchild = L->Lchild;
else
p->Lchild = L->Lchild; //否则结点p的左子树指向L的左子树
free(L);
return p;
}
else
{
BSTNode *q = p->Rchild; //q指向其右子树;
free(p);
return q;
}
}
int DeleteBST(BSTNode* &T,ElemType key)
{
//若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素,并返回TRUE;否则返回FALSE
if(!T) return -1; //不存在关键字等于key的数据元素
else
{
if(key == T->data) // 找到关键字等于key的数据元素
{
T = DeleteNode(T);
return 1;
}
else if(key < T->data)
return DeleteBST(T->Lchild, key);
else
return DeleteBST(T->Rchild, key);
}
}
void InorderTraverse(BSTNode* &T) //中序遍历
{
if (T!=NULL)
{
InorderTraverse(T->Lchild) ;
printf("%d ",T->data);
InorderTraverse(T->Rchild) ;
}
}
int main()
{
int arr
= {75, 95, 15, 78, 84, 51, 24, 120,24};
BSTNode* head=NULL;
CreateBiTree(head,arr,N);
printf("%d\n",head->data);
InorderTraverse(head); printf("\n");
InsertBST(head,63);
InsertBST(head,54);
InorderTraverse(head); printf("\n");
printf("%d\n",SearchBST(head,78));
printf("%d\n",SearchBST(head,55));
DeleteBST(head,63);
InorderTraverse(head);
printf("\n");
DeleteBST(head,95);
InorderTraverse(head);
printf("\n");
DeleteBST(head,75);
InorderTraverse(head);
printf("\n");
free(head);
return 0;
}
用三叉链表存储:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 10 typedef int ElemType; typedef struct BSTNode { ElemType data ; struct BSTNode *Lchild, *Rchild, *Parent; }BSTNode; void InorderTraverse(BSTNode* &T) //中序遍历 { if (T!=NULL) { InorderTraverse(T->Lchild) ; printf("%d ",T->data); InorderTraverse(T->Rchild) ; } } //查找关键字key,迭代版本 BSTNode* SearchBST(BSTNode* &T, ElemType key) { BSTNode* p=T; while( p ) { if( key == p->data) return p; //查找成功,返回p if( key < p->data) p=p->Lchild; //在左子树中继续查找 else p=p->Rchild; //在右子树中继续查找 } return NULL; //T为空 } //查找关键字key,递归版本 BSTNode* Recur_SearchBST(BSTNode* &T, ElemType key) { if (NULL==T || key==T->data) return T; if ( key< T->data) return Recur_SearchBST(T->Lchild,key); else return Recur_SearchBST(T->Rchild,key); } //插入关键字key void InsertBST(BSTNode* &T,ElemType key) { BSTNode* p= T; BSTNode* father=NULL; while( p!=NULL ) { if (p->data==key) return; //防止插入重复键值 father=p; p=(key < p->data)? p->Lchild :p->Rchild; } p=(BSTNode*)malloc(sizeof(BSTNode)); p->Lchild=p->Rchild=NULL; p->Parent=father; p->data=key; if (T==NULL) T=p; else { if (key < father->data) father->Lchild=p; else father->Rchild=p; } } //n个数据在数组d中,tree为二叉排序树根 void CreateBiTree(BSTNode* &T,ElemType d[] ,int n) { T=NULL; for(int i=0;i<n;i++) { InsertBST(T,d[i]); } } BSTNode* DeleteNode(BSTNode* &T) { BSTNode* p=T; if (p->Lchild) { BSTNode* L = p->Lchild; //L指向其左子树; BSTNode* prer = p->Lchild; //prer指向其左子树; while(L->Rchild != NULL)//搜索左子树的最右边的叶子结点L { prer = L; L = L->Rchild; } p->data = L->data; if(prer != L)//若L不是p的左孩子,把L的左孩子作为L的父亲的右孩子 prer->Rchild = L->Lchild; else p->Lchild = L->Lchild; //否则结点p的左子树指向L的左子树 free(L); return p; } else { BSTNode *q = p->Rchild; //q指向其右子树; free(p); return q; } } int DeleteBST(BSTNode* &T,ElemType key) { //若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素,并返回TRUE;否则返回FALSE if(!T) return -1; //不存在关键字等于key的数据元素 else { if(key == T->data) // 找到关键字等于key的数据元素 { T = DeleteNode(T); return 1; } else if(key < T->data) return DeleteBST(T->Lchild, key); else return DeleteBST(T->Rchild, key); } } void PrintRouth(BSTNode* &T, ElemType key) { BSTNode* q=SearchBST(T,key); while(q) { printf("%d ",q->data); q=q->Parent; } printf("\n"); } int main() { int arr = {75, 95, 15, 78, 84, 51, 24, 120,25,24}; BSTNode* head=NULL; CreateBiTree(head,arr,N); //printf("%d\n",head->data); InorderTraverse(head); printf("\n"); PrintRouth(head,24); PrintRouth(head,84); InsertBST(head,63); InsertBST(head,54); InorderTraverse(head); printf("\n"); printf("%d\n",SearchBST(head,78)); printf("%d\n",SearchBST(head,55)); DeleteBST(head,63); InorderTraverse(head); printf("\n"); DeleteBST(head,95); InorderTraverse(head); printf("\n"); DeleteBST(head,75); InorderTraverse(head); printf("\n"); free(head); return 0; }
相关文章推荐
- 数据结构之---C语言实现二叉排序树(BinarySortTree)
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- 【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】
- Binary Search Tree(BST)二叉搜索树的实现-java语言
- LeetCode 99 Recover Binary Search Tree(Python详解及实现)
- 基于数组的二叉查找树 Binary Search Tree (Java实现)
- BinarySearchTree(1)二叉查找树链式递归实现
- 二分查找树的C++实现 Binary Search Tree(BST)
- 二叉查找树 BinarySearchTree 的实现
- LeetCode 98 Validate Binary Search Tree(Python详解及实现)
- PAT04-树6 Complete Binary Search Tree 【JAVA实现】
- BinarySearchTree Implements(Java 实现的二叉搜索树算法)
- 二叉排序树(Binary Sort Tree)查找、插入、删除 Java实现
- LeetCode 96 Unique Binary Search Tree(Python详解及实现)
- PAT1064:Complete Binary Search Tree (Java实现)
- BinarySearchTree二叉搜索树的实现
- BinarySearchTree(2)二叉查找树链式非递归实现
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
- 折半插入排序(Binary Insertion Sort)的C语言实现
- [LeetCode]--315. Count of Smaller Numbers After Self(Binary Search Tree && Merge Sort)