APS 重点复习课程之数据结构
Overview
Linear List
- Sequence Table (Array)
- Linked List
- Stack
- Queue
Tree
- Binary Tree
- Pre-order, In-order, Post-order, on the level Traversal
- Binary Sort Tree
- B Tree
- B+ Tree
Graph
- Adjacent Matrix and Adjacent List
- DFS
- BFS
Search Algorithm
- Binary Search
- Heap Search
- B Tree
Sort Algorithm
Bubble Sort
Selective Sort
Quick Sort
Merge Sort
Linear list
Sequence Table
1 | template<typename T> |
insert an element at i-th position: every element behind i-th position move to the next one respectively
O(n) for each insert-element operation
remove an element at i-th position: every element beind i-th position move to the last one respectively
O(n) for each remove-element operation
Linked List
Single Linked List
1 | template<typename T> |
Double Linked List
1 | template<typename T> |
Stack
- A special linear list: insert and delete operations are only allowed to occur at only side of the list.
- Top: the side used to insert or delete element
- Bottom: the other side
- Insert an element:
stk.push(element)
- Delete an element:
stk.pop(element)
Queue
A special linear list: insert only allowed at the back, delete only allowed at the front.
back(tail) front(head). FIFO (first in first out)
Tree
non-linear structure
root: node on the top, has no precursor (ancestor)
leaf node: has no successors (offspring)
depth: distance between current node and root
height: distance between current node and the farthest leaf node of sub-tree
Binary Tree
1 | template<typename T> |
max number of nodes on the i-th level of Binary Tree: (two to the power of i minus one)
a binary tree of depth k has at most: nodes
number of leaf nodes: a, number of nodes of two out-degree: b, a=b+1 (叶子节点数=出度为 2 的节点数+1)
the depth of a complete binary tree with n nodes:
Complete Binary Tree
Except the nodes in second last layer and the last layer, every nodes have both right and left subtree.
(就是除了最后两层节点,其它节点都必有左右子节点)
Traversal
Pre-order Traversal
For each node being visited: current node→left successor→right successor
1 | void pre_order(Node* cur){//recursion |
In-order Traversal
For each node being visited: left successor→current node→right successor
1 | void in_order(Node* cur){//recursion |
Post-order Traversal
For each node being visited: left successor→right successor→current node
1 | void post_order(Node* cur){//recursion |
Traversal on the level (BFS)
apply with queue.
- push current node into queue
- when current queue is not empty, pop a element A and visit it.
- push left child and right child of A into queue.
1 | void on_the_level(Node* cur){ |
Binary Sort Tree
the value of every nodes in the left subtree is smaller than the subtree’s root’s value.
(所有左子树节点的值都小于当前节点的值)
the value of every nodes in the right subtree is bigger than the subtree’s root’s value.
(所有右子树节点的值都大于当前节点的值)
in-order traversal of BST brings an increasing sequence.
AVL Tree (Balanced BST)
- a BST.
- absolute value of depth difference between left and right subtrees is no more than 1
- every subtree of AVL tree is also AVL tree.
B Tree
- similar to the BST, it focus on effective search
- it differently record not only the key-value, but also a in-order table of the key-values.
- don’t need to be binary
B+ Tree
each node of the B+ tree except the leaf nodes can be seen as a index, the B+ tree only store the key values in the leaf nodes, and the other nodes store the most(high/low)key-value of its subtree.
characters are like this:
once we have find the equal value of search in the nodes, we don’t stop the search process until we arrive the leaf nodes. which means both the successful or the failure situation we have to traverse a complete path in the tree.
widely used in the storage method of database to save time of searching
Huffman Tree
related to Huffman coding. (in order to compress)
For each char and corresponding probability/frequency:
- find the current 2 least frequency, merge them.
- replace these 2 least frequency with a sum of them.
- loop 1.
Graph
How to store a graph?
Adjacent Matrix
- advantages: simple to insert and delete edges
- disadvantages: hard to insert or delete vertex
- space complexity: with a 2-dimensional matrix
Adjacent List
each vertex correspond to a list.
the list stores vertex which directly linked with current vertex
DFS & BFS on a graph
……
Search Algorithm
Binary Search
1 | template<typename T> |
B Tree
B+ Tree
Hash Map
Sort Algorithm
Bubble Sort
time complexity:
- compare adjacent 2 data, if left > right, swap.
- we assure in every loop, the last one is max value.
- loop 1. and 2.
Selective Sort
time complexity:
- for each position, loop:
- find the min value, swap with data at current position.
Quick Sort
- average time complexity:
- time complexity in worst condition:
- find a random element as basis.
- place the element smaller than basis to left, bigger than basis to right.
- recursion to quick sort subarray.
Merge Sort
- time complexity:
- 2 times space complexity than quick sort, but time complexity is more stable.
- divide n element into two n/2-subsequences
- conquer: using merge sort to merge 2 subsequences.
- merge 2 sorted subsequence.