MindMap Gallery Outline of Data Structure
This is a mind map about the outline of "Data Structures", the main contents include: Chapter 1 Introduction, Chapter 2 Tables, Stacks and Queues, Chapter 3 Trees, Chapter 4 Hashing, Chapter 5 Priority Queues, Chapter 6 Sort, Chapter 7 Non-intersection collection class, Chapter 8 Figure.
Edited at 2025-09-15 22:38:03This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
This is a mind map about Deep Analysis of Character Relationships in Zootopia 2, Main content: 1、 Multi-layer network of relationships: interweaving of main lines, branch lines, and hidden interactions, 2、 Motivation for Character Behavior: Active Promoter and Hidden Intendant, 3、 Key points of interaction: logic of conflict, collaboration, and covert support, 4、 Fun Easter eggs: metaphorical details hidden in interactions.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
This is a mind map about Deep Analysis of Character Relationships in Zootopia 2, Main content: 1、 Multi-layer network of relationships: interweaving of main lines, branch lines, and hidden interactions, 2、 Motivation for Character Behavior: Active Promoter and Hidden Intendant, 3、 Key points of interaction: logic of conflict, collaboration, and covert support, 4、 Fun Easter eggs: metaphorical details hidden in interactions.
Outline of Data Structure
Chapter 1 Introduction
1.1 Data structure basics
Definition: No standard definition, study the storage and processing of specific relational data through abstract methods
Research content: logical relationships between data and corresponding operations, storage implementation of logical relationships, and operation implementation of operations in storage mode
Data logical structure classification:
Collection structure: the order between elements is arbitrary, only "belongs to the same set" to contact
Linear structure: an ordered sequence of data elements. Except for the beginning and end elements, the other elements have unique advances and successors.
Tree structure: Except for the root element, each node has only one forward trend, and the number of successors is unlimited.
Graphic structure: There is no limit on the number of forward and successors for each element
Data structure operations: Create, clear, insert, delete, search, update, access, traversal (including specific operations of each data structure)
Data structure storage implementation:
Components: data element storage, relational storage between data elements
Physical structure: storage node (storage data elements), logical structure representation (relational storage), additional information (such as linked list header nodes)
Basic storage method: The storage node can be used for structures/objects, adopt generic programming ideas, and focus on relational storage
Relational storage method:
Sequential storage: using storage location table relationships, implemented in arrays
Link storage: Use pointers to explicit table relationships, such as single linked table linear relationships
Hash storage: dedicated to collection structures, hash functions that associate data elements with storage locations
Index storage: storage nodes are stored continuously, and the node relationship between the index area table is set
1.2 Algorithm Analysis
Core association: data structures process data, and operation implementation is an algorithm under storage mode. Each operation has multiple implementations.
Algorithm quality evaluation: correctness (implementing predetermined functions), readability (easy to debug and modify), robustness (confronting environmental changes), high efficiency (high spatiotemporal performance), data structure focuses on spatiotemporal performance
Algorithm resource analysis:
Time: The program runs, and it is necessary to avoid excessive time-consuming
Space: The space required for the program to run, and it is necessary to avoid excessive memory usage
Key concepts: spatial complexity, time complexity, algorithm calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of calculation of
Space complexity: including fixed space requirements (not related to the scale of the problem) and variable space requirements (related to the amount of data), dealing with the worst case, and the calculation is simpler
Factors influencing program running time: problem scale and input data distribution, compiler target code quality, computer system performance, algorithm advantages and disadvantages (key factors)
Importance of effective algorithms: Poor algorithms have no practical significance. Give examples to illustrate the efficiency differences in computer data processing under different time functions, emphasizing improving algorithm efficiency rather than machine speed
Time complexity: The relationship between abstract measurement calculation volume and problem scale, including the best, worst, and average case time complexity
Calculation of algorithmic operations: select standard operations and determine the number of standard operations under a given input as the calculation quantity
Example analysis: Taking the product of the maximum value and d as an example, comparing the operational quantities of different algorithms
Progressive notation:
Core idea: do not consider the specific running time function, pay attention to the law of changing the running time with the scale when the problem is large, and take the order of magnitude of the running time function
definition:
Big O: There are normal numbers c and N0, when N≥N0, T(N)≤cF(N), T(N) is O(F(N))
Large Ω: There are normal numbers c and N0, when N≥N0, T(N)≥cF(N), T(N) is Ω(F(N))
Big ▲: T(N) is both O(F(N)) and Ω(F(N))
Small O: T(N) is O(F(N)) but not ▲(F(N))
Example: T(n)=(n 1)² is O(n²)
F(N) Selection: Select a simple function form, ignore the low-order terms and coefficients
Typical growth rate: constant (c), logarithmic (logN), log square (log₂N), linear (N), NlogN, square (N²), cubic (N³), exponent (2ⁿ)
Algorithm classification:
Polynomial time algorithm: incremental time complexity has polynomial time boundaries
Exponential time algorithm: gradual time complexity has exponential time limit
Complexity relationship: O(1)<O(logN)<O(N)<O(NlogN)<O(N²)<O(N³); O(2ⁿ)<O(N!)<O(Nⁿ)
Big O notation calculation:
Steps: Define standard operations, calculate the number of standard operations to get the function, and take the main term to get the large O expression
theorem:
Summarization theorem: T1(n) T2(n)=O(MAX(f(n),g(n)))(T1(n) is O(f(n)), T2(n) is O(g(n)))
Production theorem: T1(n)×T2(n)=O(f(n)×g(n))(T1(n) is O(f(n)), T2(n) is O(g(n)))
Calculation rules:
Rule 1: Simple statement time complexity O(1)
Rule 2: Conditional statement time complexity O(1) max(O(then clause), O(else clause))
Rule 3: The time complexity of the loop statement is the running time of the loop body × the number of loops (including the cost of loop control)
Rule 4: Use the product theorem for nested loops, such as the double-layer loop time complexity O(n²)
Continuous statement: Use the summing theorem to take the high complexity as the overall complexity
Simplified calculation: find the most complex program segment to calculate the time complexity, that is, the time complexity of the entire program
1.3 Typical Examples - Maximum Continuous Subsequence Problem
Problem description: Given a sequence of integers (negative), find the continuous subsequence with the largest value, and the sum of all integers is 0 when all integers are negative
Example: Enter {-2,11,-4,13,-5,2}, answer 20; enter {1,-3,4,-2,-1,6}, answer 7
solution:
Solution 1 (exhaustive method): Find the sum of all possible sub-sequences, find the maximum value, time complexity O(n³)
Solution Two (O(n²) Algorithm): Using the recursive relationship of subsequence sums, omitting the innermost loop
Solution Three (O(n) algorithm): Optimize loops based on the characteristics that negative subsequence does not constitute the largest continuous subsequence
1.4 Object-oriented data structure
Process data structure: respectively introduce storage methods (records) and operation implementations (functions)
Object-oriented data structure:
Core: Encapsulating the storage and processing process of data structures into tools
Advantages: Users only need to know the logical characteristics, no need to know storage and operation implementation
Learning requirements: Know storage and processing methods, understand packaging to adapt to user needs
The course adopts an object-oriented approach
Data structure description and implementation:
Logical properties: abstract class description, pointing out the provided operations
Implementation: Several implementation classes are inherited from the corresponding abstract classes to ensure logical characteristics
Generic programming: Data structures focus on relational storage, data elements can be of any type, and classes (including abstract classes and non-abstract classes) are templates.
1.5 Homework
Chapter 2 Questions 7, 8, 27
Chapter 2 Tables, stacks and queues
2.1 Linear Table
2.1.1 Table concept
Definition: A set composed of N identical characteristic nodes A₀, A₁,…, Aₙ₋₁, except A₀ and Aₙ₋₁, each element has a unique predecessor (Aᵢ₋₁) and successor (Aᵢ₊₊), A₀ has no predecessor, Aₙ₋₁ has no successor
Terms: N is the table size, A₀ is the head node, Aₙ₋₁ is the tail node, empty table (number of elements is 0), position (Aᵢ position is i)
Operations of tables: create (create()), clear (clear()), find length (length()), insert (insert(i,x)), delete (remove(i)), search (search(x)), access (visit(i)), traverse()
2.1.2 Table Implementation
The order implementation of linear tables:
Storage structure: Nodes are stored in continuous storage space, stored in logical order with the help of continuity, and the physical position is consistent with the logical position.
Program implementation: Use dynamic arrays, three variables are required: pointer (referring to element type), array size (capacity), and number of elements (table length).
Operation implementation:
length(): Returns length value
visit(i): Returns the data[i] value
traverse(): output the first length element of data
clear(): Set length 0
create(maxSize): apply for a dynamic array of maxSize size
search(x): traversal search, return position or -1
insert(i,x): expand the capacity when the table is full (such as double the size), insert after moving the element, add 1 in length
resize(): reapply for a larger array, copy the original content, and free up the original space
remove(i): move element overwrite deleted elements, length minus 1
Algorithm analysis:
O(1) Operation: length, visit, clear, create
O(n) operations: traverse, search, insert (worst, average), remove (worst, average)
Advantages and Disadvantages: Good positioning access performance, inserting and deleting requires moving a large amount of data, suitable for linear tables with static and frequently positioning access
Linked implementation of linear tables:
Storage structure: Each node is stored in an independent storage unit, the logical relationship is represented by an additional pointer, and the physical locations can be adjacent or not adjacent.
Classification:
Single-linked list: Each node contains data and next pointer (referring to direct successor), and the tail node next is empty
Double-linked list: Each node contains data, prior (referring to direct predecessor) and next (referring to direct successor) pointers. The head node prior is empty, and the tail node next is empty.
Circular linked list: single-circular linked list (headless node), double-circular linked list (head node prior refers to the tail node, tail node next refers to the head node, no head and tail node)
Single-linked list (leading node) operation implementation:
create(): Apply for the header node, the header node pointer is empty
clear(): Release all node space, and empty the header node pointer
length(): traversal count or use variables to store length
insert(i,x): find the location and insert a new node
remove(i): find the location and delete the node
search(x): traversal search, return position or -1
visit(i): traversal and find the location and return the data
traverse(): traverse the output data
2.1.3 Implementation of linear table classes
Linear table abstract class: template class, including clear(), length(), insert(), remove(), search(), visit(), traverse() pure virtual functions and destructors
Sequence table class:
Definition: Inheriting the list abstract class, including data (pointer), currentLength, maxSize members, and doubleSpace() function
Constructor: Initialize array, handle illegal initial size
Destructor: Release data array
Member function implementation: clear(), length(), insert(), remove(), doubleSpace(), etc.
Double-linked list class:
Definition: Inheriting list abstract class, including embedded node class (data, prev, next pointer), head pointer, tail pointer, currentLength, and move() function
Constructor: create head and tail nodes, initialize pointers and currentLength
Destructor: Release the head and tail nodes after clear()
Member function implementation: clear(), insert(), remove(), search(), visit(), traverse(), move(), etc.
Disadvantages: Double-linked list features are not used (such as reverse search)
2.1.4 Implementation of tables in STL
Introduction to STL: C data structure implementation, called collection or container
Linear table implementation:
Vector: Sequential implementation, size can grow
Features: Maintain original array, capacity, and size, provide copy structure, assignment overload, modify capacity, [] overload, iterator, etc.
Definition: including theSize, theCapacity, objects members, as well as functions such as construction, destruction, resize(), reserve(), [] overloading, push_back(), pop_back(), iterator operations, etc.
Use: Sample code shows initialization, push_back(), size(), capacity(), traversal ([] and iterator)
List: Double-linked list implementation
Features: Allow insertion and deletion at any location, support bidirectional iterators
Definition: Contains node structure, theSize, head, tail, init(), as well as functions such as construction, destruction, iterator operation, size(), empty(), clear(), front(), back(), push_front(), push_back(), pop_front(), pop_back(), and other functions
Use: Sample code to show push_front(), push_back(), insert(), traversal
2.2 Stack
2.2.1 Stack concept
Definition: Last-in, first-out (LIFO) or first-in, last-out (FILO) structure, the earliest (late) arrives at the node at the latest (first), such as table tennis balls entering, exiting box
Related concepts: bottom of the stack (the header of the structure, the node arrives the earliest), top of the stack (the tail of the structure, the node arrives the latest), stack entry (Push, insert top of the stack), stack exit (Pop, delete top of the stack), empty stack (the number of nodes is 0)
ADT of the stack: template class, including isEmpty(), push(), pop(), and top() pure virtual functions
2.2.2 Stack Implementation
The order of stack implementation:
Implemented with arrays:
Storage: Use continuous space, the top of the stack is on the back end of the array, and requires elem (pointer), top_p (top subscript of the stack), and maxSize (maximum capacity) members.
Implementation: seqStack class, including construct (initialization array), destruction (release array), isEmpty(), push() (full time expansion), pop(), top(), doubleSpace() (expansion) functions
Use: Sample code to show push(), pop(), top()
Implemented with vector:
Storage: vector stores elements, the array header is the bottom of the stack, and the array tail is the top of the stack
Operation: Use push_back() to push, use pop_back() to pop, use top back(), and use isEmpty to empty()
Implementation: sqlStack class, inheriting stack class, including push(), pop(), top(), isEmpty() functions
Use: Sample code to show push(), pop(), top()
The link implementation of the stack:
Directly implement with single linked list:
Storage: headless node single linked list, top of the stack is at the head of the table, bottom of the stack is at the end of the table
Implementation: linkStack class, including embedded node class, elem (header pointer) members, as well as construct (elem empty), destruction (release all nodes), isEmpty(), push() (header insertion), pop() (header delete), top() functions
Use: Sample code to show push(), pop(), top()
Implemented with list:
Storage: List storage elements, the header of the linked list is the top of the stack, and the tail of the linked list is the bottom of the stack
Operation: Use push_front() to push, use pop_front() to pop, use front() to top, use empty() to isEmpty
Implementation: linkStack class, inheriting stack class, including push(), pop(), top(), isEmpty() functions
2.2.3 Stack application
Non-recursive implementation of recursive functions: use the stack to simulate function calls, and the current on-site stack is entered when the function calls, such as fast sorting non-recursive implementation
Symbol Balance Check:
Question: Check if brackets ((, [, {) are paired
Algorithm: If the left bracket is entered into the stack, if the right bracket is released into the stack, if the stack is empty or does not match, an error will occur.
Requirements: Design the checkBalance program, support input file name or command line transmission parameter checking, output errors and line numbers
Implementation: Includes Symbol structure (information information), CommentType enumeration (comment type), as well as main function, CheckBalance(), GetNextSymbol(), CheckMatch(), NextChar(), PutBackChar(), SkipComment(), SkipQuote() functions
Evaluation of expressions:
Infix and suffix: prefix (ab), infix (a b), and suffix (ab). The suffix does not need to consider the operator priority.
Suffix calculation: initialize the stack, read operands and put them into the stack, read operators and put them out of the stack, and the result is put into the stack after the two operands are calculated, and the remaining results are left in the stack.
Infix to suffix: the operand is output immediately, and the operator is out of the stack when the bracket is closed (the bracket is out of the stack), and the operator is added to the stack when the bracket is opened. The operator is processed according to priority (the operator is given at the top of the stack when the priority is high, and the operator is given until the bottom is lower). After the end, the remaining operators are given out.
Simple calculator implementation: input an infix expression, calculate the addition, subtraction, multiplication, division, and multiplication of positive integers, support brackets, and use calc class (including expression storage, BinaryOp(), operator=(), result() functions)
2.3 Queue
2.3.1 The concept of queues
Definition: First-in-first-out (FIFO) structure, arrives early and leaves early. For example, banks queue up and print queues, you can imagine as pipelines, nodes flow in from the tail of the team and out from the head of the team.
Basic concepts: dequeue (dequeue, team leader delete), join (Enqueue, team tail insert), judge team empty (isEmpty)
ADT of the queue: template class, including isEmpty(), enQueue(), and deQueue() pure virtual functions
2.3.2 Queue Implementation
Queue order implementation:
Storage: Use array, element subscript 0 to MaxSize-1
Organization method:
Team leader is in the 0th position of the array: Departing the team requires moving a large amount of data, which is inefficient
Basic order implementation: use front (team head pointer, pointer, pointer, pointer, pointer, pointer), team empty front = rear, team full rear = MaxSize
Loop queue: Logically 0 is MaxSize, the full condition of the team (rear 1)%MaxSize==front
Problem solution: Create a larger array when the team is full, copy the original content, reset the front and rear
Job requirements: Implement circular queues, automatic expansion, inherit from queue ADT
Link implementation of queues:
Storage: headless node single linked list, the head of the table is the head of the team, the tail of the table is the tail of the team, including front (head pointer) and rear (tail pointer)
Operation examples: initialization (front=rear=NULL), joining the team (header/insert in table), dequeue (header deletion)
Link queue design:
Data member: head and tail pointer (referring to nodes, including data and next pointer)
Member functions: construct (front=rear=NULL), destructuring (release all nodes), isEmpty() (front==NULL), enQueue() (end of the team), deQueue() (delete of the team)
Implementation: linkQueue class, inherits queue class, including embedded node class, front and rear members, as well as construct, destructor, isEmpty(), enQueue(), and deQueue() functions
Use: Sample code to show enQueue() and deQueue()
Comparison of sequential implementation and link implementation:
Time: The average constant time completes the basic operation, and the sequential queue is more troublesome due to back-rewind processing
Space: Link queue has multiple pointer fields per node, and the sequential queue has a lot of unused space
2.3.3 Queue Application - Simulation of Queuing System
Introduction to simulation: Use computer to simulate real-life system operations, collect statistical data, such as bank operating system simulation
Discrete event simulation: including two types of events: customer arrival and customer departure after service, statistics on customer queue length, waiting time, etc.
Basic method: Use probability function to generate input streams of customer arrival time and service time, sort by arrival time, and use "tick" as time unit
Time-driven simulation: The clock starts from 0, add 1 tick per step to check events, suitable for situations where event intervals are small, otherwise the efficiency is low
Event-driven simulation:
Core: Each step jumps to the next event moment, handles the event and sets the current time
Processing process:
Leave events: Collect statistics, and if there are waiting customers, they will be assigned to cashiers, calculate the departure time and enter the event set
Arrival event: If there is a free cashier, the departure time will be assigned and calculated to enter the event set, otherwise it will enter the waiting queue.
Advantages: High efficiency, suitable for situations with large event intervals
Implementation key:
Random events are generated: uniform distribution (such as customer arrival interval), negative index distribution (service time), Poisson distribution (customer arrival)
Events queue by time: Use priority queue (insert in order)
Random event generation: Random number generator is used for uniform distribution
Priority queue: insert in order based on the queue, inherit the linkQueue class and rewrite enQueue()
Banking system simulation example:
Hypothesis: K cashiers, the customer arrival interval is evenly distributed, and the service time is evenly distributed
Requirements: Calculate the average waiting time of customers
Simulation process: generate customer arrival events and enter the event queue, initialize the waiting queue, counter status, and wait time, handle events (the counter is allocated or enter the waiting queue when arriving, and process wait for customers or counter is idle when leaving), calculate the average waiting time
Tool preparation: Waiting queue (normal queue), event queue (priority queue), random event generation function
Simulator design:
Function: Obtain simulation parameters and calculate the average waiting time
Class definition: simulator class, including the number of counters, customer arrival parameters, service time parameters, simulated customer number members, and eventT structure (event time, type), constructor (get parameters), avgWaitTime() (calculate the average waiting time) function
Main function: Create simulator object and output the average waiting time
2.4 Homework
P108 Questions 4, 20, 24, 35
Chapter 3 Tree
3.1 The foundation of the tree
3.1.1 Definition of a tree
Definition: The finite set T of n (n≥1) nodes has a root node, and the remaining nodes are divided into m (m≥0) non-intersecting sets (subtrees)
Example diagram: Show the structure of the tree (A is the root, and the subtree contains B, C, D, etc.)
Terms: node, degree of node, leaf node, internal node, son node, father node, brother node, ancestor node, descendant node, node level, tree height, orderly tree, disorderly tree, forest
3.1.2 ADT of the tree
Data: A limited set of data elements/nodes of the same data type
Relationship: father-son relationship
Operation: Find the root node, find the parent node, find the i-th child, delete the i-th child, build a tree, and the traversal of the tree
3.1.3 Tree storage structure
Standard form: Each node contains data fields and K pointer fields (K is the degree of the tree)
Generalized standard form: Add father node pointer field based on standard form
Example: Tree generalized standard storage with degree K=3 (table display node values, s1, s2, s3, p)
Disadvantages: There are many empty pointer fields, waste of space
3.1.4 The link storage structure of the tree
Left son and right brother notation: Each node contains data, the root pointer of the first subtree tree, and the pointer of the brother node, and a binary tree represents the tree
Example diagram: Displaying the tree's left son and right brother storage structure
3.1.5 Tree traversal
Pre-order traversal: access the root node, pre-order traversal of each subtree
Post-order traversal: Post-order traversal of each subtree and access the root node
Example: Show the traversal results of the preamble (A, B, L, E, C, F, D, G, I, H) and the postsequence (L, E, B, F, C, I, G, H, D, A) of the tree
3.1.6 Tree Application - Directory Structure Traversal
Application scenario: Operating system directory structure (such as Unix, Windows/DOS), list directory and subdirectory file names, and indent d file names into d tab characters
Implementation: pre-order traversal, listAll() function (print name, if it is a directory, traverse subdirectories)
Example: Show directory structure and traversal output
3.2 Binary Tree
3.2.1 The concept of binary tree
Definition: A finite set of nodes, empty or composed of roots and two left and right subtrees that do not intersect each other. The left and right subtrees are also binary trees, which strictly distinguish the left and right subtrees.
Basic forms: empty binary tree, root and empty left and right subtrees, root and left subtrees, root and left subtrees, root and left subtrees, root and right subtrees
Different shapes of binary trees with 3 nodes: Show all possible shapes
Common operations: create an empty tree, find the root node, find the parent node, find the left child, find the right child, build a binary tree (for left and right children and root), delete the left child, delete the right child, traversal
3.2.2 Properties of binary trees
Property 1: Non-empty binary tree layer i has up to 2ⁱ⁻¹ nodes (i≥1), examples and proofs (inductive method)
Property 2: A binary tree with a height of k can reach up to 2ᵏ⁻¹ nodes, prove (summarize 2⁰ to 2ᵏ⁻¹)
Property 3: Non-empty binary tree, leaf node number n₀=Number of node with degree 2 n₂ 1, prove (derivation of the relationship between total nodes and total branches)
3.2.3 Full Binary Tree and Full Binary Tree
Full binary tree: a binary tree with a height of k and 2 ᵏ⁻¹ nodes, the number of nodes of any layer reaches the maximum value, example diagram
Complete binary tree: Several nodes are drawn from the bottom layer of the full binary tree from right to left. Characteristics (the leaf node is at the lowest two layers, and the right subtree of any node is high k, and the left subtree is high k or k 1), example diagram (completely compared with non-complete)
Complete binary tree height characteristics (property 4): n nodes full binary tree height k=⌊log₂n⌋ 1, prove (range derivation)
Complete binary tree numbering characteristics: n nodes Complete binary trees are numbered in layer sequence (root 1), and the numbered i nodes (1≤i≤n):
i=1 is the root, i>1 is the parent node number ⌊i/2⌋
2i>n, there is no left son, otherwise the left son number is 2i
2i 1>n, there is no right son, otherwise the right son number is 2i 1
Example diagram: Shows the complete binary tree number and correspondence
3.2.4 Traversal of binary trees
Pre-order traversal: empty is empty, otherwise access the root, pre-order traversal of the left subtree, pre-order traversal of the right subtree
Middle order traversal: empty is empty, otherwise middle order traversal the left subtree, access the root, middle order traversal the right subtree
Post-order traversal: empty is empty, otherwise the post-order traversal of the left subtree, and the post-order traversal of the right subtree
Example: Show the tree's preamble (A, L, B, E, C, D, W, X), intra-order (B, L, E, A, C, W, X, D), and post-order (B, E, L, X, W, D, C, A) traversal results
Preamble The binary tree is uniquely determined by the middle order: Example (preamble A, B, D, E, F, C, mid order D, B, E, F, A, C), gradually constructing the binary tree
3.2.5 Storage of binary trees
Sequential storage:
Complete binary tree: store by number, omit left and right child fields, sample diagram (number corresponds to array)
Ordinary binary tree: patched into a complete binary tree and then stored, sample diagram (normal tree and patched array)
Example of right single branch tree: Displays the order storage of right single branch tree (large amount of empty values)
Link storage:
Standard form (binary linked list): nodes contain data, left, and right pointers, sample diagram
Generalized standard form (trilateral linked list): nodes include data, left, right, parent pointers, example diagram
Example: Showing standard and generalized standard link storage for binary trees
3.2.6 Implementation of binary tree class
Node design: embedded in tree class, including data, left, right pointers, constructors and destructors
BinaryTree design:
Storage: Pointer to the root node
Operation: judge empty, clear, traverse, find high, find scale, create, merge
Member function implementation (recursive):
Find the scale: left subtree scale, right subtree scale 1
Find height: 1 max (left subtree height, right subtree height)
Three types of traversals: recursive implementation
Deletion of the tree: Recursively delete the left and right subtrees, and then delete the root
Recursive function design: Public functions (no parameters) call private recursive functions (with parameters)
Class definition: template <class Type> class BinaryTree, including Node structure, root member, and construct, destructor, getRoot(), getLeft(), getRight(), makeTree(), delLeft(), delRight(), delTree(), isEmpty(), size(), height(), printPreOrder(), printPostOrder(), printInOrder(), createTree() functions, private member functions (height(), delTree(), size(), printPreOrder(), printPostOrder(), printInOrder())
Create a tree (createTree()):
Process: Enter the root node, use the queue to store new nodes, and exit the queue to enter the son (the specific value table is empty)
Example: Show queue changes and tree construction
Code implementation: Use linkQueue, enter the node value, and process the son node
Binary tree application: sample code (create tree, find height, find scale, traversal, merge), execution example (input and output results)
3.2.7 Iterator class for binary tree traversal
Iterator requirements: Provide tools to access tree elements in a certain order (such as accessing the next element), corresponding to three types of traversals
Iterator class behavior specification: TreeIterator class, including construct, destructor, First() (first node), operator () (next node), operator () (node), operator () (node), operator() (return current data) functions, protecting members T (binary tree), current (current node pointer)
Preorder traversal iterator (Preorder class):
Inheriting TreeIterator, including stack (storage node pointer) members
Non-recursive algorithm: root enters the stack, accesses from the stack, right and left sons (non-empty) enter the stack, repeating to empty stack
Constructor: Initialize the stack, root into the stack
operator (): If the stack is empty, the current is empty, otherwise the stack is currently out, and the right and left sons enter the stack
First(): Clear the stack, root into the stack, call operator ()
Postorder traversal iterator (Postor class):
Inheriting TreeIterator, including stack (Store StNode, including node pointer and TimesPop) members
Algorithm: nodes are put into the stack (flag 0), and left subtree is taken out to process the left subtree (flag 1), then right subtree is taken out to process the right subtree (flag 2), and flag 2 is visited
StNode structure: contains node (node pointer), TimesPop (number of pop-ups)
Constructor: Stack push root node (StNode)
operator (): If the stack is empty, current is empty, otherwise the loop will be released. TimesPop adds 1, and the flag is 3, current=node, otherwise push back to the stack, processing left/right subtree
First(): Clear the stack, root into the stack, call operator ()
In-order traversal iterator (Inorder class):
Inherit Postorder, only rewrite operator ()
Algorithm: Similar to the post-order, the flag 2 accesses, and the right subtree is processed
operator (): If the stack is empty, current is empty, otherwise the loop will be released. TimesPop adds 1, and the flag is 2, current=node, push the right son (not empty), otherwise push back to the stack and process the left subtree
Iterator application: Sample code (create a tree, traversed with preorder, middle, and postorder iterators)
3.2.8 Application of Binary Tree - Expression Tree
Expression tree: Arithmetic expressions are represented as binary tree, and the result is obtained after the order is traversed. Example diagram ((4-2)*(10 (4 6)/2) 2)
Design objective: Use expression tree to calculate expressions
Construction process: recursively construct, the subexpression in brackets is a subtree, and handles operators and operators (by priority)
Class design: Calc class, including node structure (type, data, lchild, rchild), root member, and create(), postOrder(), getToken(), result(), public construct (constructed from expression), result() (calculate result) functions
getToken(): Take syntax units (DATA, ADD, SUB, MULTI, DIV, OPAREN, CPAREN, EOL) from expressions, and process spaces, numbers, operators, and brackets.
create(): recursively builds an expression tree and processes operators, brackets, and operators (by priority)
postOrder(): Post-order traversal output (verification expression tree)
result(): recursive calculation, post-order traversal, operation number returns data, operator calculates the results of left and right subtrees
Application: Sample code (create calc object, calculate expression results)
Problem: Not considered incorrect expression
3.3 Huffman Tree and Huffman Code
3.3.1 Uses of Huffman Trees
Data compression: variable length encoding, commonly used short character encoding, examples (character frequency a(10), e(15), etc., fixed length requires 174bit, Huffman encoding requires 146bit)
Huffman encoding: The character with a large frequency is close to the root of the tree, the path (left 0, right 1) is the encoding, sample diagram
3.3.2 Huffman Algorithm
step:
Given n weight sets F={T₁,T₂,…,Tₙ}
Initial A=F
Loop n-1 times: select the two nodes with the minimum weight value as the left and right sons of the internal node bᵢ, the weight value of bᵢ is the sum of the two sons, delete the two nodes in A and add bᵢ
Finally, the remaining nodes in A are root
Example: A(10), e(15) and other characters to build a Huffman tree, example diagram
Huffman encoding generation: from leaf to root, left 0 to right 1, example (code 001 of a)
3.3.3 Storage of Huffman Trees
Features: The coded element is a leaf node, the others are a degree 2 node, and the size is 2n-1 (n is the number of coded elements)
Storage: size 2n array, 0 is not used, root is stored 1, leaf nodes are stored n 1 to 2n, each element stores data, weights, parent, left and right child positions
Example: Table shows Huffman tree storage (value, weight, parent, left, right)
Generation process: Gradually display the changes in array elements
3.3.4 Huffman tree class
Node definition: Node structure (data, weight, parent, left, right)
Returns the coded storage structure: hfCode structure (data, code)
Huffman tree class definition: hfTree class, containing elem (Node array), length members, as well as constructor (building Huffman tree), getCode() (getCode), destructor
Constructor: Initialize the leaf node and loop to build the internal node (select the minimum two weight nodes)
getCode(): From the leaf node to the root, record the path (left 0, right 1), generate the encoding
Use: Sample code (generate Huffman encoding of character set a(10) etc., output result)
3.4 Binary sorting tree
3.4.1 Definition of binary sorting tree
Definition: empty or satisfies: any node p, left subtree (non-empty) all node keywords <p keyword, right subtree (non-empty) all node keywords> p keyword, left and right subtree is also a binary sorting tree
Example diagram: Showing the binary sorting tree (including characters and values)
3.4.2 Operation of binary sorting tree
Search: If the root is equal, it will be successful, it will be smaller than the left subtree, and it will be larger than the right subtree. Example (Find 122, 110, etc.)
Search recursive description: empty returns false, root equals return true, less than search left, greater than search right
Insert: If the new node is empty, it is the root. If the parent node is not empty, it is searched. If the parent node is inserted as a leaf according to the size, example (Sequences 122, 99, etc. build trees)
Insert recursive implementation: if empty, insert the root, smaller than insert the left, larger than insert the right
delete:
Delete leaf node: delete it directly, change the parent pointer to empty
Delete a node with a son: the son replaces the deleted node location
Delete nodes with two sons: select the largest node of the left subtree or the smallest node of the right subtree as a subtree, copy the data, delete the subtree
Example diagram: Shows three deletion situations
Delete recursive implementation: empty return, less than deletion left, greater than deletion right, equal to (the right subtree will be replaced by two sons and the right subtree will be replaced by the smallest, otherwise the son will be replaced)
3.4.3 Design of binary sorting tree class
Class definition: BinarySearchTree class, including BinaryNode structure (element, left, right), root member, and construct, destructor, findMin(), findMax(), contains(), isEmpty(), makeEmpty(), insert(), remove(), operator=(), printInOrder(), private member functions (insert(), remove(), findMin(), findMax(), contains(), makeEmpty(), clone(), printInOrder())
Design features: embedded BinaryNode class, root is a data member, and public functions call private recursive functions
Member function implementation:
contains(): Recursive search
insert(): Recursively insert
findMin(): Recursively find the leftmost node
findMax(): Non-recursively find the rightmost node
remove(): Recursively deleted
printInOrder(): Recursive in-order traversal
Destructor: Call makeEmpty()
makeEmpty(): Recursively delete all nodes
3.5 AVL tree (balanced binary tree)
3.5.1 Definition of balanced binary tree
Background: When the tree degenerates into linked lists, it is necessary to balance the tree
Equilibrium factor: left subtree high at node - right subtree high, empty tree height -1
Balanced binary tree: Each node has a balance factor of 1, -1, 0, or the height difference between left and right subtrees is up to 1
Height characteristics: height up to about 1.44log(N 2)-1.328
Example diagram: Showing equilibrium and unbalanced trees (equilibrium factor)
3.5.2 Operation of Balance Tree - Insert
Insert process: Same as binary sorting tree, after insertion, it may be:
Do not destroy the balance: modify the balance from the bottom up, and stop if the balance is unchanged at a certain node.
Breaking balance: need to be adjusted
Example: Insert 29 (not destroyed), Insert 2 (destruction)
Insertion causes imbalance:
LL: Left child left subtree insert
LR: Left child right subtree insert
RL: Right child left subtree insert
RR: Right child right subtree insertion
Adjustment method:
LL/RR (single rotation): LL, the left son is the root, the original root is the right subtree, and the original right son is the left subtree of the root; RR is the opposite, example picture
LR/RL (double rotation): LR will first rotate the son and grandson, then rotate the root and new son; RL will be the opposite, example diagram
3.5.3 Implementation of AVL tree class
Node definition: AvlNode structure (element, left, right, height)
Find the node height: height(t), return -1 by empty
Insert: insert(x, t), recursively insert, judge the balance factor, perform corresponding rotation (LL, LR, RR, RL), update the height
Rotate function:
rotateWithLeftChild() (LL rotation)
rotateWithRightChild() (RR rotation)
doubleWithLeftChild() (LR rotation, RR first and LL)
doubleWithRightChild() (RL rotation, LL first and then RR)
Class definition: AvlTree class, including AvlNode structure, root member, and construct, destructor, isEmpty(), findMin(), findMax(), contains(), insert(), remove(), makeEmpty(), printTree(), private member functions (insert(), remove(), findMin(), findMax(), contains(), makeEmpty(), clone(), rotateWithLeftChild(), rotateWithRightChild(), doubleWithLeftChild(), doubleWithRightChild(), height())
3.5.4 Other operations and performance of balanced trees
Delete: late deletion (mark)
Find performance: proportional to tree height, O(logN)
Theorem: The height h of N nodes equilibrium tree satisfies log₂(N 1)≤h≤1.44log₂(N 1)-0.328, prove (derivation of full binary tree, minimum node tree)
3.6 Stretch the tree
3.6.1 Defects of balanced tree
Additional balance information is required
Complex implementation, high cost for insertion and deletion
Not exploited 90-10 rules (90% of accesses are targeted at 10% of data)
3.6.2 Average analysis method
Definition: limit M continuous operation costs and evenly distribute them to each operation
Example: Array doubled, total cost of M operations O(M)
3.6.3 Concept of stretching trees
Core: After accessing a node, it moves to the root through rotation, and often accesses the node near the root, which has the side effects of balancing
Basic idea: Continuously rotate with parent node to root, example (rotation after accessing 3)
Basic method defect: long and bad access sequences may occur, example (insert 1-N and access 1-N)
3.6.4 Stretching Strategy
Goal: Extend from bottom to top to balance the tree, so that the logarithmic distribution is equal to the boundary
Strategy: By rotating from the bottom to the root, there are three situations (zig, zig-zig, zig-zag)
Rotation type:
zig: x parent is root, single rotation, sample diagram
zig-zag: x is the right child, father is the left child (or vice versa), double rotation, sample diagram
zig-zig: x and the parent are both left/right children, first rotate the parent and grandfather, then rotate the x and the parent, example diagram
Example: After inserting 1-N, accessing 1, the tree height is reduced, sample diagram
Advantages: Commonly access the near root of nodes, equal time O(logN)
3.7 B-tree
3.7.1 Background of B-tree
Scenario: The amount of data is large, the memory cannot be stored, and external data structure is required to reduce the number of disk accesses
Example: 10⁷ records, the worst 10⁷ disk accesses (463 hours), average 32 times (5 seconds), typical 100 times (16 seconds)
Solution: M fork to find the tree, reduce the tree height, log_M N
3.7.2 Definition of B-tree
M-order B-tree:
Data storage leaves
Non-leaf node up to M-1 key (boot search)
The root may be a leaf, or have 2-M sons
Non-root, non-leaf, son number ⌈M/2⌉-M
All leaves are in the same layer, with ⌈L/2⌉-L data items
Example diagram: 5th order B-tree, displaying nodes (disk blocks), keys, and sons
3.7.3 L and M selection
Example: Block capacity 8192 bytes, key 32 bytes, branch 4 bytes, M=228 (non-leaf node), L=32 (leaf, 256 bytes per record)
Scale: 10⁷ records, up to 625,000 leaves, 3-4 layers of index
3.7.4 Insertion of B-tree
process:
Find the insert leaf node
Ye Dissatisfied: Insert and adjust the order
Leaf: Split into two and a half full leaves, update the parent node
The father is full and continues to split, the worst splits the root (root allows 2 children)
Example: Insert 57, 55, 40 to show the changes in the tree
3.7.5 Deletion of B-tree
process:
Find delete items and delete them
The number of leaf elements is lower than the minimum:
Neighbors are not the smallest
Neighbors are the smallest, and the father loses his son. If the father is below the smallest, then continues.
If only one son is left, the root will be deleted, and the son will be the new root.
Example: Delete 99, display the changes of the tree
3.7.6 Time benefits of B-tree
Height: log_{⌈M/2⌉} (2N/L), 3-4 layers
Operation: Find the disk at most 5 times to read, insert and delete more than 1 write, split/merge the extra 4 writes (even-apart can be ignored)
3.8 Collections and mappings in STL
3.8.1 Set
Features: Ordered containers that do not allow duplicate elements
traversal: iterator and const_iterator
Operation: Basically similar to vector and list
Sample code: Display insert(), size(), traversal (iterator), output results
3.8.2 Map (map)
Features: Store keywords (unique) - value splices, different keywords can correspond to the same value
Operation: Similar to set
Application: Find words that change one letter to another word (such as wine→fine, etc.), example (the dictionary stores vector, and the result stores map<string, vector<string>>)
Implementation: computeAdjacentWords() (build map), printHighChangeables() (output result), algorithm optimization (grouped by length)
3.9 Homework
P175 Questions 4.9, 4.10, 4.19
Genealogy management system: set up family tree, add descendants, modify descendants' status, and query
Chapter 4 Hashing method
4.1 Basic Concept
Background: Small non-negative integers can be used directly with arrays (initialization, insert, find, remove, all O(1)), but large integers and strings are not applicable
Solution:
Hash function: map large numbers to small subscripts
String processing: interpreted as integers
Definition: Hash method (hash method), directly searching nodes by keywords, time O(1), does not support orderly operations
4.2 Hash function
Definition: H(key), value range 0-m-1, few conflicts
Common hash functions:
Direct address method: H(key)=key or a×key b, example (keyword {100, 400, etc.}, H(x)=x or x/100)
Remaining method: H(key)=key MOD p or c (p≤m prime number), example (m=1024, p=1019), select p as the reason for prime number (avoid space waste)
Digital analysis method: select bits with uniform distribution of numbers as address
Hash function selection: Calculate time, keyword length, hash table length, keyword distribution, search frequency
4.3 Conflict resolution
Conflict: Multiple keywords map to the same storage unit
Solution:
Closed hash table (using this hash table spare unit):
Linear detection method: H(key)=key MOD 11, conflict detects the next empty unit, example (keywords 17, 60, 29, 38)
Delete and search: Find (traversing to empty or found), delete (delete late to avoid search problems)
Problem: Primary aggregation (same hash address), secondary aggregation (different hash address competition units), worst O(n)
Hash table definition: HashTable class, containing HashEntry structure (element, info (ACTIVE/EMPTY/DELETED), array (vector), currentSize members, as well as constructs, contains(), makeEmpty(), insert(), remove(), private member functions (isActive(), findPos(), rehash(), myhash())
Quadratic detection method: address sequence i 1², i 2²,..., theorem (the table size prime number, the table can be inserted if it is at least half full, no repeated detection), proof (reverse proof method)
Hash method again: use the second hash function, the i-th collision address f(i)=i×hash2(x), hash2(x)=R-(x MOD R) (R< table length prime number)
Open hash table (link method): Colliding nodes have a linear table outside the hash table, example diagram (synonym chain)
Hash table based on chain address method: HashTable class, including theLists (vector<list<HashedObj>>), currentSize members, as well as constructs, contains(), makeEmpty(), insert(), remove(), private member functions (rehash(), myhash())
Rehash: Load factor >0.5 will double the capacity, recalculate the hash value insertion
4.4 Homework
P208 Question 1
Chapter 5 Priority Queue
5.1 Priority Queue Model
Definition: Data structure that supports at least insert (enQueue) and deleteMin (deQueue) operations
5.2 Simple implementation of priority queues
Single linked list:
Insert in the head, delete the traversal (O(n)); insert the ordered single-linked list to find the position (O(n)), delete the traversal (O(1))
Binary search tree: insert and delete O(logn), but no need for all elements to be ordered
5.3 Binary heap
5.3.1 Definition of binary heap
Definition: Complete binary tree, satisfying:
Minimize heap: kᵢ≤k₂ᵢ and kᵢ≤k₂ᵢ₊₁ (i=1..⌊n/2⌋)
Maximize heap: kᵢ≥k₂ᵢ and kᵢ≥k₂ᵢ₊₁ (i=1..⌊n/2⌋)
Example: Minimize heap {2,3,4,5,7,10,23,29,60}, maximize heap {12,7,8,4,6,5,3,1}, sample diagram
5.3.2 Features of Binary Heap
Structural: Complete binary tree
Order: Minimize/maximize heap characteristics
Discussion basis: Minimize heap
5.3.3 Heap application
Priority queue: maximum/minimum element is root
Heap sorting: build heap, take root, adjust heap, repeat until heap is empty
5.3.4 Main operations of the heap
Build a stack:
Method: N times insert (O(NlogN)), or call percolateDown (O(N)) at the reverse hierarchy
Example diagram: Showcase the heap construction process
insert:
Process: End of the queue, filter upwards (percolate up) to satisfy order
Example diagram: Showcase the insertion process
Code: insert(x), expansion, hole=currentSize, upward comparison exchange
Time: Worst O(logN), average 2.6 comparisons, upwards by 1.6 layers
delete:
Process: Delete the root, finally put the element in the root, filter down to satisfy order
Example diagram: Showcase the deletion process
Code: deleteMin(), deleteMin(minItem), percolateDown(hole)
Time: Worst, Average O(logN)
5.3.5 Implementation of binary heap classes
Class definition: BinaryHeap class, including currentSize, array (vector) members, as well as constructs, isEmpty(), findMin(), insert(), deleteMin(), deleteMin(minItem), makeEmpty() functions, private member functions (buildHeap(), percolateDown())
Constructor: default constructor, constructed from vector (buildHeap())
buildHeap(): Inversely call percolateDown() from currentSize/2
percolateDown(): filter downwards and find the younger son to exchange until the order is satisfied
5.4 Application of priority queues
5.4.1 Selection Questions
Question: Find the kth largest element in N elements table
solution:
Solution 1: Take the kth one in order, O(N²)
Solution 2: Build a heap, deleteMin k times, O(N klogN)=O(NlogN)
Solution 3: Find the k-th largest, read k elements to build a heap, and insert the remaining elements larger than the root, delete the root, O(Nlogk)
5.4.2 Event-driven simulation
Scenario: Bank queueing system, generate arrival events, and process the earliest event (generate service time back to event queue)
Implementation: Set event queue to priority queue (time is priority)
5.5 D-heap
Definition: Each node has k sons, the tree is shorter
operate:
Insert: O(log_d N)
Delete: O(dlog_d N) (d elements are found to be the smallest)
Advantages and disadvantages: fast insertion, long run time (no shift optimization)
Purpose: Insert more queues than deletes, large queues are stored outside
5.6 Left stack
5.6.1 Definition of the left heap
Empty path length (npl): x to the shortest path of nodes less than two children, leaf npl=0, empty npl=-1
Left heap: Each node has left child npl ≥ right child npl, left tilt, sample diagram (left heap and non-left heap)
5.6.2 The main operation of the left heap - merge
Recursive method:
The heap with large roots merges the right subtree with small roots
If the left heap definition is violated after merge, the left and right subtrees will be exchanged.
Terminate: If a pile is empty, return to another pile
Example diagram: Show the merge process (H1, H2 merge)
5.6.3 Implementation of left heap class
Class definition: LeftistHeap class, including LeftistNode structure (element, left, right, npl), root member, and construct, destructor, isEmpty(), findMin(), insert(), deleteMin(), deleteMin(minItem), makeEmpty(), merge(), operator=() functions, private member functions (merge(), merge1(), swapChildren(), reclaimMemory(), clone())
Public merge(): merge two left heaps, rhs empty
Private merge(): merge the right subtree with the small root and the large root, and call merge1()
merge1(): Recursively merge the right subtree, swap the left and right subtrees (if needed), update npl
5.7 Slanted stack
Definition: satisfies heap order, does not satisfies structure, does not require npl, the right path is arbitrarily long
Time: Worst O(N), M operations are evenly aligned O(MlogN), each operation is evenly aligned O(logN)
Merge: Similar to the left heap, unconditionally exchange left and right subtrees after merge (except the largest node of the right path)
Example diagram: Showcase the merger process
Advantages: No need for npl, simple implementation
5.8 Benuri queue
5.8.1 Benuri tree and queue definition
Benuri tree: B₀s single node, B€ is composed of two B€₋₁ trees, satisfying the order of the heap, example graph (B₀, B₁, B₂, B₃)
Benuri tree characteristics: B€ has 2 ᵏ nodes, and the number of nodes in the d layer is Benuri coefficient
Benuri Queue: Benuri tree collection, at most one tree at each height, corresponding to the number of elements in binary representation, example diagram (6, 7 element queues)
5.8.2 Operation of Benuri queue
Merge:
The trees with the same height are merged from low to high, and one tree is left to merge from the other two trees.
Combination of trees of the same height: the roots are the roots, and the roots are the subtrees
Time: O(logN)
Example: Combination of H1 and H2, display process
insert:
Single node tree merged with the original queue, O(logN) (worst), average O(1) (carry terminating)
delete:
Find the smallest root tree T, delete T, and delete root, merge T with the original queue
Example: Delete the smallest element, display process
5.8.3 Storage of Benuri queue
Tree: Kids Brothers Chain
Forest: A linear table of pointers to the roots of a tree
Example diagram: Showcase queue storage structure
5.8.4 Implementation of Benuri queue class
Class definition: BinomialQueue class, including BinomialNode structure (element, leftChild, nextSibling), currentSize, theTrees(vector<BinomialNode*>), and construct, destructuring, isEmpty(), findMin(), insert(), deleteMin(), deleteMin(minItem), makeEmpty(), merge(), operator=() functions, private member functions (findMinIndex(), capacity(), combineTrees(), makeEmpty(), clone())
5.9 Priority queues in STL
Header file: queue
Class template: priority_queue
Implementation: Binary heap
Main members: push(), top(), pop(), empty(), clear()
5.10 Homework
P251 Questions 2, 3, 19, 32, 34
Chapter 6 Sorting
6.1 Introduction
Assuming: elements store arrays, N is the number of elements, elements support < and > operations, and comparison-based sorting algorithm
6.2 Insert sort
6.2.1 Simple insertion sort
Principle: Insert the objects to be sorted into the previous sorted sequence by keywords in each step
Process: 1≤j<n, compare a[j] with a[0..j-1] from right to left, insert into the appropriate position
Example diagram: Show the sorting process
Algorithm: insertionSort(vector<Comparable> &a), double-layer loop, tmp temporary storage a[p], insert after moving
Time complexity: O(N²) (worst reverse order), O(N) (best sorted), suitable for situations where there are few elements and close sorting
6.2.2 Fold half insert sort, Hill sort
Fold and half insert sort: Use fold and half to find the insertion position to reduce the number of comparisons, and still need to move the elements, O(N²)
Hill sort: See Section 6.4
6.3 The lower bound of simple sorting
Reverse order: i<j but Aᵢ>Aⱼ ordered pair, example ({8,5,9,2,6,3} has 10 reverse orders)
Theorem: N different elements arrays are averaged inverse number N(N-1)/4, prove (the total number of inverse order between array and inverse array)
Theorem: The algorithm that exchanges adjacent elements sorting takes Ω (N²) time on average, proves (exchange eliminates an inverse order)
6.4 Hill sort
Idea: Improve insertion sorting, first compare the far elements, then close elements, and gradually approach basic insertion sorting
Algorithm ideas:
Take gap<n, divide gap subsequences (distance gap), and insert and sort each subsequence simply
Shrink gap, repeat until gap=1
Example diagram: Show the sorting process
Properties: hk-ordered arrays are still hk-ordered after being sorted by hk-1-
Hill increment: gap is divided from N/2 to 1
Algorithm: shellsort(vector<Comparable> &a), double-layer loop, gap decrement, inner layer insertion sort
Time complexity: Hill increment worst O(N²), average O(N³/²), dependent increment sequence
6.5 Heap sorting
principle:
buildHeap build heap (O(N))
N times deleteMin to take the element, and the result sorting (O(NlogN))
Space issues:
Problem: Double space required (heap result)
Solution: After deleteMin, the heap is reduced, the deleted elements are stored at the last position, and the maximum heap is incrementally sorted
Example diagram: Show the sorting process (59, 36, etc. sorting)
Algorithm: heapsort(vector<Comparable> &a), buildHeap, exchange root and last element, percolateDown, repeat
6.6 Ordering
6.6.1 Principle of merge sorting
Thought: Merge two sorted ordered tables, divide and govern
Merge example: Show the process of combining two ordered numbers
Merge sorting method:
N=1 is sorted
Otherwise, recursively merge the first and the last half, merge the two ordered arrays
Example diagram: Showcase the merger and sorting process
6.6.2 Implementation of merge sorting
Package function: mergeSort(vector<Comparable> &a), create tmpArray, call mergeSort with parameter
mergeSort with parameter: mergeSort(a, tmpArray, left, right), recursively sort left and right, merge
merge function: merge(a, tmpArray, leftPos, rightPos, rightEnd), merge two ordered subarrays, and copy them back to the original array
Time complexity: O(NlogN), additional space required
6.7 Quick sort
6.7.1 Quick sorting principle
step:
S empty or 1 returns
Select pivot (center point)
S-{v} is L (≤v), R (≥v)
Return to Quicksort(L) v Quicksort(R)
Key: Select the center point and divide it
6.7.2 Select the center point
Error method: Select the first element, sorted/inversely divided difference, O(N²)
Safe selection: random selection, time-consuming
The median value division of three elements: select the first, middle, and tail median, example (select 6 of 8, 1, 4, etc.), avoid the different sorting of the
6.7.3 Division method
process:
Pivot is put at the end, i left to right, j right to left, i stops the big element, j stops the small element, if it does not cross, it will be exchanged, otherwise pivot and i will be exchanged
Example: Showcase the partitioning process (8, 1, 4, etc.)
6.7.4 Small-scale array optimization
Small scale (≤10) sort with insertion, terminate recursively to improve efficiency
6.7.5 Quick sorting program
Package function: quicksort(vector<Comparable> &a), call quicksort with parameter
median3 function: select the median value of three elements and put right-1
Quicksort with parameter: quicksort(a, left, right), sort with insertion on a small scale, otherwise choose pivot, divide, and recursively sort left and right.
Time complexity:
Worst: O(N²) (pivot min/maximum)
Best: O(NlogN) (even division)
Average: O(NlogN)
6.7.6 Application of Quick Sorting in Selecting Questions
Quickselect(S,k):
S1 returns the unique element
Choose pivot and divide L and R
k≤|L| will recursively Quickselect(L,k), k=|L| 1 will return pivot, otherwise recursive Quickselect(R,k-|L|-1)
Time: Worst O(N²), Average O(N)
6.8 Indirect sorting
Background: Frequent exchange of large objects takes time
Solution: Use pointer array to point to the object, swap pointers, and rearrange the object by pointer after sorting
6.9 General lower bound for sorting
Conclusion: The comparison-based sorting algorithm requires at least O(NlogN) time
prove:
The input is an arrangement of 1..N, a total of N!
The number of comparisons determines the arrangement. Each comparison is halved and the arrangement is possible. Log(N!) comparisons are required.
log(N!)≈NlogN-1.44N, so at least O(NlogN)
6.10 Pocket sorting method
Scenario: Enter a positive integer with a value of 1-M
Principle: Suppose the count array with size=M, read Ai and count[Ai], scan the count output
Time complexity: O(M N)
6.11 Outer sort
6.11.1 Outer sorting model
Device dependency: Consider tape, 2-3 tape drives are required
6.11.2 Simple algorithm (two-way merge)
Assumption: 4 tapes A1, A2, B1, B2, M records are stored in memory
process:
Read M records sorting, write B1, B2 (run) in turn, and reverse
Combine the run of B1 and B2, write A1 and A2 in turn, and reverse
Repeat until a run (length N)
Example diagram: Shows the initial and merged tape configuration
Time: log(N/M) times merge, add initial run construct
6.11.3 Multi-channel merger
Principle: K-way merge reduces the number of mergers, and use the priority queue to find the minimum K elements
Example: 6 tapes and 3 channels merge, display process
Time: log_K (N/M) merge
6.11.4 Multi-stage merger
Principle: K 1 tape realizes K-way merger, non-uniform distribution run
Example: 3 tapes 34 runs, distributions 21 and 13, showing the merge process
Initial run distribution: Fibonacci number, if not enough, fill in virtual run
6.11.5 Replacement selection
Principle: Generate longer runs, read M elements to build heap, deleteMin output, read the element larger than the output, insert it, otherwise the spare position is stored, and the heap is empty will be new run (use spare element)
Example: 81, 94 and other generation run, display process
6.12 Homework
P306 Questions 3, 12, 16, 20, 30, 38, 52
Chapter 7: Non-intersection collection class
7.1 Equivalence Relationship
Relationship: on set S, aRb is true/false
Equivalence relationship: reflexivity (aRa), symmetry (aRb↔bRa), transitiveness (aRb∧bRc→aRc)
7.2 Dynamic Equivalence Issues
Background: The equivalence relationship is implicit, and it is necessary to efficiently handle find (find equivalent classes) and union (combined equivalent classes)
Equivalent class: Subset of S, containing all related elements, forming segmentation
Operation: find, union, no intersection set (and search set)
7.3 Basic data structure
Represents: forest (tree), root is equivalent class name, and the array has parent node (s[i]=-1 as root)
operate:
union: merge two trees (roots are subtrees), O(1)
find: find root, O(N) (worst)
Example diagram: Show the union process (union(4,5), union(6,7), union(4,6))
7.4 Intelligent Algorithm
Background: Basic algorithm find linear, M operation O(MN)
Intelligent and:
Merge by scale: small trees are large subtrees, and the roots have negative scale
Example diagram: Show the difference between normal and by scale
Theorem: merge by scale, node depth ≤logN
Combined by height: shallow trees are deep subtrees, roots are
Implementation by size:
Class definition: DisjointSet class, including set (array), size members, and constructor, find(), and Union() functions
Member functions: construct (initialize set to -1), find() (find root), Union() (combined by scale)
Use: Sample code (find, Union operation, output result)
7.5 Path compression
Background: The algorithm M operation O(MlogN) can be improved
Path compression: When finding, change the parent node of the node on the path to the root. Example diagram
Compatibility: Compatible with scale merger, not fully compatible with height merger (using rank)
Performance: M operation evenly aliquot O(Mα(N)) (α(N) inverse Ackerman function, ≤5)
7.6 Application - Maze Problem
Scene: M×N rectangular unit maze, upper left entrance, lower right exit, generate maze (demolition of walls to connect)
algorithm:
Initial: Full wall, unit is not connected
Select the wall randomly. If the partition unit is not connected, the wall will be demolished. Combine the equivalent category.
Repeat until fully connected
accomplish:
Connection is an equivalent relationship, unit number, adjoining unit judgment, wall removal and merge
Example diagram: Show the initial and wall removal process
7.7 Homework
P335 Question 1, 4
Chapter 8 Picture
8.1 Definition of the diagram
8.1.1 Basic definition of diagram
Definition: G=(V,E), V vertex set, E edge (arc) set
Classification:
Directed graph: edges have directions, <A,B>≠<B,A>, example graph
Undirected graph: edges without direction, (A,B), example graph
Weighted graph: edges have weights, <Vi,Vj,W>/(Vi,Vj,W), sample graph
8.1.2 Terms of the figure
Adjacent: Directed graph Vi is adjacent to Vj, undirected graph Vi is adjacent to Vj
Degree: the number of adjacent edges of the undirected graph; the incoming degree (incoming edges), the outgoing degree (outgoing edges) of the directed graph
Sub-picture: G'=(V',E'), V'⊆V, E'⊆E, sample diagram
Undirected graph connectivity:
Path: Vertex sequence
Loop/ring: The same path at the beginning and end
Simple loop/ring: No duplicate vertices except for the beginning and end
Connection: There are paths at two points
Connection diagram: any two points connected
Connection component: Extremely connected sub-graph, example diagram
Directed graph connectivity:
Strong connection diagram: any two points connected
Strong connectivity component: Extremely strong connectivity sub-picture
Weak connected graph: considered as undirected graph connected, example graph
Complete graph: undirected complete graph (n(n-1)/2 edge), directed complete graph (n(n-1) edge), example graph
Directed Acyclic Graph (DAG): Acyclic