Search(查找) 折半二叉树
Posted 一身千寻瀑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Search(查找) 折半二叉树相关的知识,希望对你有一定的参考价值。
下午去机房做实验,最近事情太多,貌似也没有什么心情一点点敲代码。
贴完代码继续刷Arrow吧。
晚上还有课,但愿能看进去一点概率论,周末就考了。
直接贴代码
折半查找 Search_Bin
#pragma once
typedef int SSTable;
typedef int KeyType;
int Search_Bin(SSTable ST[], KeyType key,int STLength)
int low = 0, mid;
int high = STLength - 1;
while (low <= high)
mid = (low + high) / 2;
if (key == ST[mid])
return mid;
else if (key < ST[mid])
high = mid - 1;
else
low = mid + 1;
return -1;
MainTest.cpp
#include <stdio.h>
#include "Search_Bin.h"
#define SIZE 5
int main()
int arr[SIZE] = 1,2,3,4,5 ;
//Test 1 can find at place0
if (Search_Bin(arr, 1, SIZE) == -1)
printf("Search Failed.\\n");
else
printf("%d\\n", Search_Bin(arr, 1, SIZE));
//Test 6 can't find in arr
if (Search_Bin(arr, 6, SIZE) == -1)
printf("Search Failed.\\n");
else
printf("%d\\n",Search_Bin(arr, 6, SIZE));
return 0;
--------------------------------------------------------------------------
以下是二叉查找树(Binary Search Tree)
SearchBST.h
#pragma once
#include "BiTree.h"
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree &p)
if (!T)
p = f;
return false;
else if (key == T->data)
p = T;
return true;
else if (key < T->data)
return SearchBST(T->lchild, key, T, p);
else
return SearchBST(T->rchild, key, T, p);
InsertBST
#pragma once
#include <stdlib.h>
#include "BiTree.h"
#include "SearchBST.h"
Status InsertBST(BiTree &T, ElemType e, KeyType key, BiTree &p)
BiTree s;
if (!SearchBST(T, e, nullptr, p))
s = (BiTree)malloc(sizeof(BiNode));
s->data = e;
s->lchild = s->rchild = nullptr;
if (!p)
T = s;
else if (key < p->data)
p->lchild = s;
else
p->rchild = s;
return true;
else
return false;
DeleteBST.h
#pragma once
#include <stdlib.h>
#include "BiTree.h"
Status DeleteBST(BiTree &T, KeyType key)
if (!T)
return false;
else
if (key == T->data)
return Delete(T);
else if (key < T->data)
return DeleteBST(T->lchild, key);
else
return DeleteBST(T->rchild, key);
Status Delete(BiTree &p)
BiTree q;
BiNode *s;
if (!p->rchild)
q = p;
p = p->lchild;
free(q);
else if (!p->data)
q = p;
p = p->rchild;
free(q);
else
q = p;
s = p->lchild;
while (s->rchild)
q = s;
s = s->rchild;
p->data = s->data;
if (q != p)
q->rchild = s->lchild;
delete s;
return true;
普通的二叉树结构化定义
#pragma once
typedef int ElemType;
typedef bool Status;
typedef int KeyType;
typedef struct BiNode
ElemType data;
struct BiNode * lchild, * rchild;
BiNode, *BiTree;
加上平衡因子bf之后的二叉树结构化定义
#pragma once
typedef int ElemType;
typedef struct BSNode
ElemType data;
int bf;
struct BSNode *lchild, *rchild;
BSNode, *BSTree;
左旋转算法
L_Rotate.h
#pragma once
#include "BSTree.h"
void L_Rotate(BSTree &p)
BSNode *lc;
lc = p->rchild;
p->rchild = lc->lchild;
lc->lchild = p;
p = lc;
右旋转算法
R_Rotate.h
#pragma once
#include "BSTree.h"
void R_Rotate(BSTree &p)
BSNode *lc;
lc = p->lchild;
p->lchild = lc->rchild;
lc->rchild = p;
p = lc;
---------------------------------------分割线---------------------------------------
考试月,今天是12月10号,1月9号结束。
概率论 、Java 、DB 、DataStructure 、Academic English 1&3、中马 、CET4+CET6刷分、IELTS、选修课以及各种大作业,事情是不少。
感冒,状态不佳。
以上是关于Search(查找) 折半二叉树的主要内容,如果未能解决你的问题,请参考以下文章