数据结构:对计算机内存中数据的一种安排。包括:数组、链表、栈、二叉树、哈希表等。
算法:对数据结构中的数据进行各种处理,例如查找特定项,排序等。
解决问题:现实世界中的数据存储,以前是用索引卡片(一叠卡片)来存储数据的,现在如果想用计算机来代替索引卡片(凡是用索引卡片的都可以用计算机代替),将会出现一些问题:
• How would you store the data in your computer’s memory? --建模
• Would your method work for a hundred file cards? A thousand? A million? --扩容
• Would your method permit quick insertion of new cards and deletion of old ones?
--插入、删除
• Would it allow for fast searching for a specified card? --查找
• Suppose you wanted to arrange the cards in alphabetical order. How would you sort them? --排序
程序员的工具
有些数据结构只会在编程语言中用到,而不会被用户直接使用。
现实世界的建模
数据结构的特性(围绕查找、增加、删除的性能)
Data Structure |
Data Structure |
Disadvantages |
Array |
Quick insertion, very fast access if index known. |
Slow search, slow deletion, fixed size. |
Ordered array |
Quicker search than unsorted array. |
Slow insertion and deletion, fixed size |
Stack
|
Provides last-in, first-out access. |
Slow access to other items. |
Queue |
Provides first-in, first-out access |
Slow access to other items |
Linked list |
Quick insertion, quick deletion. |
Slow search.
|
Binary tree |
Quick search, insertion, deletion (if tree remains balanced). |
Deletion algorithm is complex.
|
Red-black tree |
Quick search, insertion, deletion. Tree always balanced. |
Complex. |
2-3-4 tree |
Quick search, insertion, deletion. Tree always balanced. Similar trees good for disk storage. |
Complex. |
Hash table |
Very fast access if key known. Fast insertion. |
Slow deletion, access slow if key not known, inefficient memory usage. |
Heap |
Fast insertion, deletion, access to largest item. |
Slow access to other items. |
Graph |
Models real-world situations. |
Some algorithms are slow and complex. |
抽象数据结构:除了数组之外的数据结构。
算法的概述:插入删除查找,迭代查询、排序
Summary
• A data structure is the organization of data in a computer’s memory or in a disk file.
• The correct choice of data structure allows major improvements in program efficiency.
• Examples of data structures are arrays, stacks, and linked lists.
• An algorithm is a procedure for carrying out a particular task.
• In Java, an algorithm is usually implemented by a class method.
• Many of the data structures and algorithms described in this book are most often used to build databases.(数据库的原理看来不简单啊)
• Some data structures are used as programmer’s tools: They help execute an algorithm.
• Other data structures model real-world situations, such as telephone lines running between cities.(电话线网和数据结构有什么关系?也涉及到查询)
• A database is a unit of data storage composed of many similar records.
• A record often represents a real-world object, such as an employee or a car part.
• A record is divided into fields. Each field stores one characteristic of the object described by the record.
• A key is a field in a record that’s used to carry out some operation on the data.
For example, personnel records might be sorted by a LastName field.
• A database can be searched for all records whose key field has a certain value.
This value is called a search key.索引
关键字:当查询一条记录时,所使用的那个字段被称为(索引)关键字。