数据结构——查找(线性表的查找与树表的查找)

avatar
作者
筋斗云
阅读量:3

目录

1.查找

1.查找的基本概念

1.在哪里找?

 2.什么查找?

3.查找成功与否? 

4.查找的目的是什么? 

5.查找表怎么分类? 

6.如何评价查找算法? 

7.查找的过程中我们要研究什么? 

2.线性表的查找 

1.顺序查找 

代码示例:

 1.顺序查找的改进

代码示例:

2.顺序查找的性能分析与特点 

2.折半查找 

代码示例:

1.折半查找的性能分析与特点 

3.分块查找(索引顺序查找) 

1.分块查找性能分析与优缺点 

3.树表的查找 

1.二叉排序树 

1.二叉排序树的存储结构

代码示例:

2.二叉排序树的递归查找 

代码示例: 

3.二叉排序树的查找分析 

平衡二叉树 

4.二叉排序数的操作-插入 

5.二叉排序树的操作-删除 

4.总的代码


1.查找

533296e771294cbaa2746ef6e04969b0.png

1.查找的基本概念

1.在哪里找?

a9def868af234913868a73aae44271f7.png

 2.什么查找?

f9f7e196a1744e16bfe7259cd3e8c05e.png

3.查找成功与否? 

bfe61aa8ddd943f7acf5cc5b20b917a7.png

4.查找的目的是什么? 

a51e5f6f514049b991cb5068bd975fde.png

5.查找表怎么分类? 

6c003c29f02446ea94a41cce70496b37.png

6.如何评价查找算法? 

51ec30689b1f45b6b1413fef0b5f2888.png

7.查找的过程中我们要研究什么? 

d7bc73aacf224fc7a140dd5171df0964.png

2.线性表的查找 

eef9498bb7954bdd8a6d5fab5cac7054.png

1.顺序查找 

47ba2e82f5e04db3bd81d1b8f4407669.png

c91ee042172a4e11b7a1c9f35e6d9a32.png

代码示例:

typedef struct { 	int key; }elem;  typedef struct { 	elem *r; 	int len; }sstable;  sstable st;  int search_seq(sstable st,int key) { 	for(int i = st.len; i >= 1; --i) { 		if(st.r[i].key == key) return i; 		return 0; 	} }
 

b9a9a808e3694b318a4415c8563b5685.png

 1.顺序查找的改进

e4ba4f4e86fd4875abb501bf6c6abfea.png

ba7ce0590e3e4a94b7f1cc1a6e7bb43c.png

fcdd734399b84c1cad36eeb8d064db79.png

a47727fce9b74be7b834080cb1c2db40.png

代码示例:
int Search_seq(sstable st,int key) { 	st.r[0].key = key;     int i; 	for(i = st.len; st.r[i].key != key; --i); 	return i; }
 

c79ce80272e54bff8dfcc054034861dc.png

2.顺序查找的性能分析与特点 

3dea09fcb3a349a8bb8957d18fd9bf2a.png

c578c6704db24293ab255d1b1b1fe36d.png

738d66ea5188408d8b47fe87040f93b9.png

d7acc23868234f1a9ba04c3bbd719f9d.png

 

2.折半查找 

4b547c5f5cf844c09c194ed0e01e1698.png

f77d8dfedaa941be8029ef0e9a4aa3d8.png

aaf870f135af41ddb0bdf6950c7bccf8.png

1419712a749c4e4a97b246f02518aa40.png

代码示例:
 
int search_bin(sstable st,int key) { 	int low = 1; 	int high = st.len; 	while(low <= high) { 		int mid = (low + high) / 2; 		if(st.r[mid].key == key) return mid; 		else if(key < st.r[mid].key) 			high = mid - 1; 		else low = mid + 1; 	} 	return 0; }

83cf0dd929b94b1587d1ec744b58f50b.png

4ed481b24d864da691dcd5500e05c251.png

a63cdf5df0bb4facb2a11e98e88346ba.png

1.折半查找的性能分析与特点 

f6a1c389a54f40b7b76c8e1983094a05.png

16fa0295167a40d891a517b5e66c2fee.png

2e7e8a16ea9f47ad89828ae56c814124.png

3.分块查找(索引顺序查找) 

546a37c1fa694f78ac83c8344cfb3279.png

8f13666cb30f45ce81a8a832ee7bc846.png

1.分块查找性能分析与优缺点 

381952aa31e54e21b68f3bec81b5a8a4.png

039a56248a6f4b1cb82b685de02a965d.png

f191ed21adb24296b963ee5043be129d.png

3.树表的查找 

1d3471901908473d9c27c3c7d90acf64.png

1.二叉排序树 

a409770b951e44f3b0082d5abb81bfd2.png

df037eab22ab464980ead17d156476f6.png

e0b5b59f2ed145dd83f6e6c40ef3e71d.png

3ffc4da738ab4f1383f8dbb8941c8de2.png

1.二叉排序树的存储结构

5a0a7984c2a847ceb8a7b23356db16e8.png

代码示例:
 
typedef struct { 	int key; }elemtype;  typedef struct bstnode { 	elemtype data; 	struct bstnode *lchild, *rchild; }bstnode,*bstree;  bstree t;

2.二叉排序树的递归查找 

a22ea3fa087f464685fbe2d5e476f3a5.png

a5c7483e9c994a1ea1c519a84c1843b3.png

代码示例: 
bstree searchbst(bstree t,int key) { 	if((!t) || key == t -> data.key) return t; 	else if(key < t -> data.key) 		return searchbst(t -> lchild,key); 	else return searchbst(t -> rchild,key); }

3.二叉排序树的查找分析 

0b4430660853460fa87667c2dd72c5f7.png

27aff5d23c4545bcab11c992207ba7a6.png

a21afdf37dfe4ffd872afbe23b4d77b2.png

平衡二叉树 

574e568b8bd94e9fa72b3728e6bbaa79.png

4.二叉排序数的操作-插入 

30a0cf9a947d40f4940ea5f89651e63f.png

 

a2db92b6d828477baf544fa4c7e1a1bb.png

216451cdf1c249cd9482e99e2761091b.png

d9347d625dc94bfdb53d8f9936dea63d.png

6cbef40ed9724e9b8bf7a72cbb3e3b7f.png

5.二叉排序树的操作-删除 

dfaa609e58f4470b9b3b5ee6366d65da.png

14f50aa057a949548f29cec25738b611.png

f850985719e44fdfb0092222dfba753e.png

f1b92c16c6e347379eb21b7dae3699b2.png

ce8f4521e19c45f19313a869c3f58587.png

2b992b9215b143238ea6b84cf3f1f804.png

a93635bc67a54ca2bab3cb3a3652eb59.png

4.总的代码

#include<bits/stdc++.h> using namespace std;  typedef struct { 	int key; }elem;  typedef struct { 	elem *r; 	int len; }sstable;  sstable st;  int search_seq(sstable st,int key) { 	for(int i = st.len; i >= 1; --i) { 		if(st.r[i].key == key) return i; 		return 0; 	} }  int Search_seq(sstable st,int key) { 	st.r[0].key = key; 	int i; 	for(i = st.len; st.r[i].key != key; --i); 	return i; }  int search_bin(sstable st,int key) { 	int low = 1; 	int high = st.len; 	while(low <= high) { 		int mid = (low + high) / 2; 		if(st.r[mid].key == key) return mid; 		else if(key < st.r[mid].key) 			high = mid - 1; 		else low = mid + 1; 	} 	return 0; }  typedef struct { 	int key; }elemtype;  typedef struct bstnode { 	elemtype data; 	struct bstnode *lchild, *rchild; }bstnode,*bstree;  bstree t;  bstree searchbst(bstree t,int key) { 	if((!t) || key == t -> data.key) return t; 	else if(key < t -> data.key) 		return searchbst(t -> lchild,key); 	else return searchbst(t -> rchild,key); }  int main() { 	 	return 0; }

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!