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、选修课以及各种大作业,事情是不少。

感冒,状态不佳。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值