重庆大学数据结构英文课件searching_02
合集下载
数据结构与程序设计(王丽苹)18 searching
1/10/2019
数据结构与程序设计
11
Implementation of Key Class
class Key { int key; public: Key (int x = 0); Key (const Record &r); int the_key( ) const; }; bool operator == (const Key &x, const Key &y);
1/10/2019 数据结构与程序设计 6
Implementation of Record Class
class Record{ public: operator Key( ); // implicit conversion from Record to Key . Record(int x=0, int y=0); private: int key; int other; };
1/10/2019 8
数据结构与程序设计
Test Main
void main(){ Key target(5); Record myrecord(5,9); if (target==myrecord) cout<<"yes"<<endl; else cout<<"no"<<endl; } 调用operator Key( )构造临时Key tmp,采用void operator == (const Key &x, const Key &y)操作符比较。 Output: yes
重庆大学算法导论英文课件Chapter_1_The_Role_of_Algorithms_in_Computing
Algorithm description
• Specification:
– Natural language, computer program, hardware design
• An algorithm can be specified in English | Chinese, as a computer program, or even as a hardware design.
• Know the strengths and limitations of data structures • Hard problems: NP-complete, efficient algorithm, good | best
1.3 Algorithms as a technology
• Algorithms Exercise & Mark
• See 《算法分析与设计》课程介绍与要求.ppt
Exercises for Chapter 1
• Exercises: 1.2‐3: Find the smallest value of n… • Problems 1‐1: Comparison of running times…
Exercises for Chapter 1
• 1. 1‐3. Select a data structure that you have seen previously, and discuss its strengths and limitations. • 1.1‐4 How are the shortest‐path and traveling‐ salesman problems given above similar? How are • they different? • 1.1‐5 Come up with a real‐world problem in which only the best solution will do. Then come up with one in which a solution that is "approximately" the best is good enough.
重庆大学数据结构英文课件Trees_04
11
Building Human Trees
Create a collection of n initial Huffman trees, each of which is a single leaf node containing one of the letters. Put the n partial trees onto a priority queue organized by weight (frequency). Next, remove the first two trees (the ones with lowest weight) from the priority queue. Join these two trees together to create a new tree whose root has the two trees as children, and whose weight is the sum of the weights of the two trees. Put this new tree back into the priority queue. This process is repeated until all of the partial Huffman trees have been combined into one.
Data Structure
Tree & Binary Trees (4)
College of Computer Science, CQU
Outline
Introduction Weighted Path Length Huffman tree Huffman Codes
计算机算法设计与分析-Chapter4数据集合上的搜索(Searching)算法
优先队列(Priority Queue):(动态数据集S,Insert, Deletemin)。
4.2 二叉搜索树
二叉搜索树又称为二元字典树,是一种最常用的动态数据集 的数据结构,可以用于实现字典和优先队列等ADT。
4.2.1 二叉搜索树(Binary Search Trees)
BST的一个结点与一个数据项相对应,除了数据项Object或数
数据集合上的操作(operation)可以分为两类: .查询(queries)操作:对数据集不做任何变动,仅仅返回有
关集合的某些信息; .修改(modifying operations)操作:要对数据集合的某些域进
行改动。 一些典型的操作: .Search(S,k):搜索,一个查询操作。对于给定的数据集合S
.Predecessor(S,x):求前导数据元,一个查询操作。返回x所 指向的数据元素的前一个数据元素的指针。若x指向第一个 数据元素,则返回NULL。
搜索算法(及相关操作算法)的设计实际上是实现适合各种 不同应用需要的ADT,例如:
字典(Dictionary)作为抽象数据类型,可以分为两类: ·静态字典:(静态数据集S,Search); ·动态字典:(动态数据集S,Search,Insert,Delete)。
计算机算法设计与分析
Chapter 4. 数据集合上的搜索 (Searching)算法
➢ 4.1 动态数据集(Dynamic Set)与抽象数据类 型(ADT)
➢ 4.2 二叉搜索树(Binary Search Trees) ➢ 4.3 随机二叉搜索树(Randomly Built Binary
Search Tree) ➢ 4.4 红黑树(Red-Black Tree) ➢ 4.5 2-3-4树 ➢ 4.6 Hashing技术
4.2 二叉搜索树
二叉搜索树又称为二元字典树,是一种最常用的动态数据集 的数据结构,可以用于实现字典和优先队列等ADT。
4.2.1 二叉搜索树(Binary Search Trees)
BST的一个结点与一个数据项相对应,除了数据项Object或数
数据集合上的操作(operation)可以分为两类: .查询(queries)操作:对数据集不做任何变动,仅仅返回有
关集合的某些信息; .修改(modifying operations)操作:要对数据集合的某些域进
行改动。 一些典型的操作: .Search(S,k):搜索,一个查询操作。对于给定的数据集合S
.Predecessor(S,x):求前导数据元,一个查询操作。返回x所 指向的数据元素的前一个数据元素的指针。若x指向第一个 数据元素,则返回NULL。
搜索算法(及相关操作算法)的设计实际上是实现适合各种 不同应用需要的ADT,例如:
字典(Dictionary)作为抽象数据类型,可以分为两类: ·静态字典:(静态数据集S,Search); ·动态字典:(动态数据集S,Search,Insert,Delete)。
计算机算法设计与分析
Chapter 4. 数据集合上的搜索 (Searching)算法
➢ 4.1 动态数据集(Dynamic Set)与抽象数据类 型(ADT)
➢ 4.2 二叉搜索树(Binary Search Trees) ➢ 4.3 随机二叉搜索树(Randomly Built Binary
Search Tree) ➢ 4.4 红黑树(Red-Black Tree) ➢ 4.5 2-3-4树 ➢ 4.6 Hashing技术
重庆大学数据结构ppt06_linear+list_04
Data Structure
Linear Lists_04
Mechanism for extracting keys
One approach: to require all record types to support some particular method that returns the key value. Problem: this approach does not work when the same record type is meant to be stored in multiple dictionaries, each keyed by a different field of the record.
Data Structure
Unit 6 Lists Application Dictionaries
College of Computer Science, CQU
Outline
The ADT for a simple dictionary Example:A payroll record implementation A dictionary search example Implementation for a class representing a key-value pair A dictionary implemented with an unsorted array-based list Dictionary implementation using a sorted array-based list
Here, payroll records are stored in two dictionaries, one organized by ID and the other organized by name. Both dictionaries are implemented with an unsorted array-based list.
第02章基本数据结构StacksQueuesListsTrees
The total cost of the series of operations is no more than the total amount charged.
(amortized time) (total $ charged) / (# operations)
Elementary Data Structures
Elementary Data Structures
5
Comparison of the Strategies
We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations
object front(): returns the element at the front without removing it
integer size(): returns the number of elements stored
boolean isEmpty(): indicates whether no elements are stored
Elementary Data Structures
6
Analysis of the Incremental Strategy
We replace the array k = n/c times The total time T(n) of a series of n push operations is proportional to
We set up a scheme for charging operations. This is known as an amortization scheme.
(amortized time) (total $ charged) / (# operations)
Elementary Data Structures
Elementary Data Structures
5
Comparison of the Strategies
We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations
object front(): returns the element at the front without removing it
integer size(): returns the number of elements stored
boolean isEmpty(): indicates whether no elements are stored
Elementary Data Structures
6
Analysis of the Incremental Strategy
We replace the array k = n/c times The total time T(n) of a series of n push operations is proportional to
We set up a scheme for charging operations. This is known as an amortization scheme.
重庆大学数据结构ppt10_Queue
‘c’ ‘b’ ‘b’ ‘a’
Dequeue
‘c’ ‘b’
‘a’
‘a’
Data Structure
Queue
Queue ADT
// Abstract queue class template <typename E> class Queue { private: void operator =(const Queue&) {} // Protect assignment Queue(const Queue&) {} // Protect copy constructor public: Queue() {} // Default virtual ˜Queue() {} // Base destructor
‘c’
3
General Equations: front = (front+1)%N; rear = (rear+1)%N;
‘d’
4
Data Structure
Queue Implementation (2)
How can we recognize when the circular queue is empty or full?
Data Structure
Queue
Queue
FIFO: First in, First Out It means: the first element inserted is the first one to be removed Example: The first one in line is the first one to be served
Data Structure
数据结构课件CHAPTER2
a1
a2
a3
a4
a5
a6
直接前驱和直接后继描述了结点之间的逻辑关系 (即邻接关系)
Department of Computer Science & Technology, Nanjing university fall
线性表的抽象基类 template <class T, class E> class LinearList { public: LinearList(); //构造函数 //析构函数 〜~LinearList(); virtual int Size() const = 0; //求表最大大体积 virtual int Length() const = 0; //求表⻓长度 virtual int Search(T x) const = 0; //搜索 virtual int Locate(int i) const = 0; //定位 virtual E* getData(int i) const = 0; //取值 virtual void setData(int i, E x) = 0; //赋值
Nanjing university
fall
删除的主要思想: (1) 在顺序表中查找x,如果x在表中不存在,则 不能删除;
Data Structures
(2)如果x在表中存在,设x在顺序表中的位置 为i; (3) 将顺序表的最后位置减1; (4)把顺序表中原来第i+1至第n-1个表项依次向 前移动一个表项位置
Department of Computer Science & Technology, Nanjing university fall
Data Structures
【重庆大学本科四门专业课PPT】数据结构linear list_02
next->element; // Remember value
Link<E>* ltemp = curr->next; // Remember link node
if (tail == curr->next) tail = curr; // Reset tail
curr->next = curr->next->next; // Remove from list
template <typename E> class LList: public List<E> {
private:
Link<E>* head; // Pointer to list header
Link<E>* tail; // Pointer to last element
Link<E>* curr; // Access to current element
Link(const E& elemval, Link* nextval =NULL)
{ element = elemval; next = nextval; }
Link(Link* nextval =NULL) { next = nextval; }
};
node
Element next
Data Structure
Insertion
Inserting a new element is a three-step process: First, the new list node is created and the new
element is stored into it. Second, the next field of the new list node is
Link<E>* ltemp = curr->next; // Remember link node
if (tail == curr->next) tail = curr; // Reset tail
curr->next = curr->next->next; // Remove from list
template <typename E> class LList: public List<E> {
private:
Link<E>* head; // Pointer to list header
Link<E>* tail; // Pointer to last element
Link<E>* curr; // Access to current element
Link(const E& elemval, Link* nextval =NULL)
{ element = elemval; next = nextval; }
Link(Link* nextval =NULL) { next = nextval; }
};
node
Element next
Data Structure
Insertion
Inserting a new element is a three-step process: First, the new list node is created and the new
element is stored into it. Second, the next field of the new list node is
UNIT2_重庆大学_商务英语_课件
新时代商务英语专业系列教材
An Integrated Course of Business English
商务英语综合教程 2
主 编 副主编 李海峰 唐 梅
UNIT TWO
Oil and Economy
Pre-reading Questions Notes to Text A Key to Exercises Notes to TEXT B
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. horizontally deceive equivalently prove prior representative accessed independently Renewable emit
IV.
Give the synonyms and antonyms of the following.
Vocabulary
I. Match the words and expressions on the left with the explanations on the right. 1. f 2.g 3.i 4.j 5.a 6.b 7.d 8.c 9.e 10.h
II. Determine the part of speech of each of the underlined words and translate each sentence into Chinese. 1. n. 很快他们就成了地平线上的几个小点了。 2. n. 他们在 9 点钟准时吃早饭。 3. n. 很久以前你就一直想成为一名辩护律师了。 4. v. 这一地区滨海小镇星罗棋布。 5. n. 一名男子向他们射出了一排子弹。 6. n. 他视力不好,算不上是一个出色的射手。 7. n. 他把球射到球门外的右边。 8. v. 一名警察在一次伏击中中弹身亡。 9. n. 干那件事不容易,但我想试试看。
An Integrated Course of Business English
商务英语综合教程 2
主 编 副主编 李海峰 唐 梅
UNIT TWO
Oil and Economy
Pre-reading Questions Notes to Text A Key to Exercises Notes to TEXT B
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. horizontally deceive equivalently prove prior representative accessed independently Renewable emit
IV.
Give the synonyms and antonyms of the following.
Vocabulary
I. Match the words and expressions on the left with the explanations on the right. 1. f 2.g 3.i 4.j 5.a 6.b 7.d 8.c 9.e 10.h
II. Determine the part of speech of each of the underlined words and translate each sentence into Chinese. 1. n. 很快他们就成了地平线上的几个小点了。 2. n. 他们在 9 点钟准时吃早饭。 3. n. 很久以前你就一直想成为一名辩护律师了。 4. v. 这一地区滨海小镇星罗棋布。 5. n. 一名男子向他们射出了一排子弹。 6. n. 他视力不好,算不上是一个出色的射手。 7. n. 他把球射到球门外的右边。 8. v. 一名警察在一次伏击中中弹身亡。 9. n. 干那件事不容易,但我想试试看。
高一英语 Unit3 Getting ready and comprehending课件 重大必修2
Lead-in
resolute
brave
Give words and expressions that can be used to describe an unconfident person.
unhappy
unsatisfied
helpless
sad
unsuccessful
unoptimistic
Fast reading
3) The passage suggests that _____. A. one of the two pots cracked and the water leaked out. B. if we make good use of our weakness, they will turn into strength. C. one of the two pots helped the water bearer water flowers.
crack
Read the passage quickly and choose the best answer for each blank.
1) The reading passage is a ________. A. short story B. fable C. novel 2) We can find the moral of the passage __. A. in the first paragraph B. in the middle C. in the last paragraph
pot two
crack
perfect
half full
full of water
ashamed
proud
resolute
brave
Give words and expressions that can be used to describe an unconfident person.
unhappy
unsatisfied
helpless
sad
unsuccessful
unoptimistic
Fast reading
3) The passage suggests that _____. A. one of the two pots cracked and the water leaked out. B. if we make good use of our weakness, they will turn into strength. C. one of the two pots helped the water bearer water flowers.
crack
Read the passage quickly and choose the best answer for each blank.
1) The reading passage is a ________. A. short story B. fable C. novel 2) We can find the moral of the passage __. A. in the first paragraph B. in the middle C. in the last paragraph
pot two
crack
perfect
half full
full of water
ashamed
proud
重庆大学数据结构第一章绪论
数据结构是带“结构”的数据元素的集合。“结构”即指 数 据元素之间存在的关系。
• 数据结构的形式定义为: 数据结构是一个二元组 Data_Structures = ( D,S )
其中:D是数据元素的有限集, S是D上关系的有限集。
例 复数的数据结构定义如下: Complex=(C,R)
其中:C是含两个实数的集合﹛C1,C2﹜,分别表示复数的 实部和虚部。R={P},P是定义在集合上的一种关系{〈C1, C2〉}。
• 数据的逻辑结构可归结为以下四类:
一、集合 结构中的数据元素除了同属于一种 类型外,别无其它关系。
二、线性结构 结构中的数据元素之间存在一 对一的关系。
三、树型结构 结构中的数据元素之间存在一 对多的关系。
四、图状结构或网状结构 结构中的数据元素 之间存在多对多的关系。
●数据的物理结构可分为(关系的表示方法): 一. “顺序映象”。以 “y 相对于 x 的存储位置” 表 示 “y 是x的后继”,由此得到的数据存储结构为 “顺序存储结构”。
} ADT Complex
• 1.3 算法和算法分析 • 1.3.1 算法及其设计原则
算法是对问题求解过程的一种描述,是为解决一个 或一类问题给出的一个确定的、有限长的操作序列。
一个算法必须满足以下五个重要特性: (1) 有穷性:一个算法必须总是在执行有穷步之后结束,且每一步
都在有穷时间内完成。
(2) 确定性:算法中每一条指令必须有确切的含义。不存在二义性。
S 是 D 上的关系集, P 是 D 的基本操作集。
• 抽象数据类型的形式定义为: ADT 抽象数据类型名 { 数据对象: 数据对象的定义
数据关系: 数据关系的定义
基本操作: 基本操作的定义
• 数据结构的形式定义为: 数据结构是一个二元组 Data_Structures = ( D,S )
其中:D是数据元素的有限集, S是D上关系的有限集。
例 复数的数据结构定义如下: Complex=(C,R)
其中:C是含两个实数的集合﹛C1,C2﹜,分别表示复数的 实部和虚部。R={P},P是定义在集合上的一种关系{〈C1, C2〉}。
• 数据的逻辑结构可归结为以下四类:
一、集合 结构中的数据元素除了同属于一种 类型外,别无其它关系。
二、线性结构 结构中的数据元素之间存在一 对一的关系。
三、树型结构 结构中的数据元素之间存在一 对多的关系。
四、图状结构或网状结构 结构中的数据元素 之间存在多对多的关系。
●数据的物理结构可分为(关系的表示方法): 一. “顺序映象”。以 “y 相对于 x 的存储位置” 表 示 “y 是x的后继”,由此得到的数据存储结构为 “顺序存储结构”。
} ADT Complex
• 1.3 算法和算法分析 • 1.3.1 算法及其设计原则
算法是对问题求解过程的一种描述,是为解决一个 或一类问题给出的一个确定的、有限长的操作序列。
一个算法必须满足以下五个重要特性: (1) 有穷性:一个算法必须总是在执行有穷步之后结束,且每一步
都在有穷时间内完成。
(2) 确定性:算法中每一条指令必须有确切的含义。不存在二义性。
S 是 D 上的关系集, P 是 D 的基本操作集。
• 抽象数据类型的形式定义为: ADT 抽象数据类型名 { 数据对象: 数据对象的定义
数据关系: 数据关系的定义
基本操作: 基本操作的定义
【重庆大学本科四门专业课PPT】组成原理3.Arithmetic+for+Computers
sign extend – addi, addiu, slti, sltiu zero extend – andi, ori, xori overflow detection – add, addi, sub
3.1 Introduction
Computer words are composed of bits; thus words can be represented as binary numbers.
Binary, Decimal, and Hexadecimal Equivalents
Converting Binary to Hexadecimal
❖ Each hexadecimal digit corresponds to 4 binary bits ❖ Example:
Convert the 32-bit binary number to hexadecimal 1110 1011 0001 0110 1010 0111 1001 0100 ❖ Solution:
Unsigned and signed integersMost
Characters
Significant Bit
Least Significant Bit
Floating-point numbers Images, sound, etc.
76 543 210 10011101
27 26 25 24 23 22 21 20
Each bit represents a power of 2 Every binary number is a sum of powers of 2 Decimal Value = (dn-1 2n-1) + ... + (d1 21) + (d0 20) Binary (10011101)2 = 27 + 24 + 23 + 22 + 1 = 157
3.1 Introduction
Computer words are composed of bits; thus words can be represented as binary numbers.
Binary, Decimal, and Hexadecimal Equivalents
Converting Binary to Hexadecimal
❖ Each hexadecimal digit corresponds to 4 binary bits ❖ Example:
Convert the 32-bit binary number to hexadecimal 1110 1011 0001 0110 1010 0111 1001 0100 ❖ Solution:
Unsigned and signed integersMost
Characters
Significant Bit
Least Significant Bit
Floating-point numbers Images, sound, etc.
76 543 210 10011101
27 26 25 24 23 22 21 20
Each bit represents a power of 2 Every binary number is a sum of powers of 2 Decimal Value = (dn-1 2n-1) + ... + (d1 21) + (d0 20) Binary (10011101)2 = 27 + 24 + 23 + 22 + 1 = 157
数据结构与程序设计19 search
Repeat the process in the half of the data that should be examined next.
Stop when item is found, or when there is nowhere else to look and item has not been found.
appropriate half of the list. Loop terminates when top <= bottom, if it has not terminated earlier by
finding the target. Make progress toward termination by ensuring that the number of items
{
Record data;
if (bottom <= top) {
int mid = (bottom + top)/2;
the_list.retrieve(mid, data);
if (data == target) {
position = mid;
return success;
}
item = 45
15 26
38 57
62 78
data[0] [1]
[2]
first
[3]
[4]
[5]
middle
item < data [ middle ]
84 91 108 119
[6]
[7]
[8]
[9]
last
last = middle - 1
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Data Structure
Searching
Removal in BST: Example
Case 1: removing a node with 2 EMPTY SUBTREES parent cursor 7 5 4 Removing 4 replace the link in the parent with null 6 7 5 6 Data Structure 8 9 10 Searching 8 9 10
method insert(key)
ðn places
a new item near the frontier of the BST while retaining its organization of data: ðn starting at the root it probes down the tree till it finds a node whose left or right pointer is empty and is a logical place for the new value ðn uses a binary search to locate the insertion point ðn based on comparisons of the new item and is values of nodes in the BST ðn Elements in nodes must be comparable!
34 0 41 1 56 2 63 3 72 4 89 5 95 6
34 0
41 1
56 2
72 4
89 5
95 6
34 0 Data Structure
56 2
72 4
95 6 Searching
Organization Rule for BST
" the values in all nodes in the left subtree of a node are less than the node value " the values in all nodes in the right subtree of a node are greater than the node values 63
Data Structure
Searching(2)
College of Computer Science, CQU
outline
ðp Binary ðp B-tree ðp B
+
Search Trees (BST)
-tree
Data Structure
Searching
A Taxonomy of Trees
Data Structure Searching
Removal in BST - Pseudocode
method remove (key) I if the tree is empty return false II Attempt to locate the node containing the target using the binary search algorithm if the target is not found return false else the target is found, so remove its node: Case 1: if the node has 2 empty subtrees replace the link in the parent with null Case 2: if the node has a left and a right subtree - replace the node s value with the max value in the left subtree - delete the max node in the left subtree
Data Structure Searching
BST Operations: Search
Searching in the BST method search(key) " implements the binary search based on comparison of the items in the tree " the items in the BST must be comparable (e.g integers, string, etc.) The search starts at the root. It probes down, comparing the values in each node with the target, till it finds the first item equal to the target. Returns this item or null if there is none.
ð§removes a specified item from the BST and adjusts the tree ð§uses a binary search to locate the target item: ð§starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree) ð§ removal of a node must not leave a gap in the tree,
Data Structure Searching
Search in BST - Pseudocode
if the tree is empty return NULL else if the item in the node equals the target return the node value else if the item in the node is greater than the target return the result of searching the left subtree else if the item in the node is smaller than the target return the result of searching the right subtree
Data Structure Searching
Removal in BST - Pseudocode
Case 3: if the node has no left child - link the parent of the node to the right (non-empty) subtree Case 4: if the node has no right child - link the parent of the target to the left (non-empty) subtree
Binary Search Trees
ðp
Binary search tree (BST) ðn Every element has a unique key. ðn The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree. ðn The left and right subtrees are also binary search trees.
10 7 10 > 9 5 4 Data Structure 6 8 9 10 Searching
Insertion in BST - Pseudocode
if tree is empty create a root node with the new key else compare key with the top node if key = node key replace the node with the new value else if key > node key compare key with the right subtree: if subtree is empty create a leaf node else add key in right subtree else key < node key compare key with the left subtree: if the subtree is empty create a leaf node else add key to the left subtree
41
89
34 Data StrucSearching
BST Example
63 55 42 10 45 BST
Data Structure
63 90 55 82 83 10 45 un-BST
Searching
60 58 67 70 83
58 67
70
BST Operations: Insertion
ðp
General Trees any number of children / node Binary Trees max 2 children / node
ðn Heaps ðn Binary
ðp
parent < (>) children Search Trees
Data Structure
Searching
ðn O(h)
ðp This
in general
Data Structure
Searching
Binary Search Algorithm