MindMap Gallery data structure java
Data structure tutorial (java language description): Length: used to reduce the length of the linear list. When the parameter nlen is wrong (nlen<0 or nlen>n), the corresponding exception is thrown. If nlen is equal to the length of the original singly linked list, it is returned directly. Otherwise, find the node p with the sequence number nlen-1, and set it as the tail node, so that the length of the new singly linked list is nlen.
Edited at 2022-11-22 18:02:04Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
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!
Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
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!
data structure
Chapter One Introduction
definition
Data is a collection of numbers, characters, and all symbols that can be input into a computer and processed by computer programs to describe objective things.
Usually data elements are used as the basic unit of data
A data item is the smallest unit of data with independent meaning, also called a domain.
A data object is a limited collection of data elements with the same properties, which is a subset of data.
Data structure refers to the collection of data elements involved and the relationship between data elements. The relationship between data elements constitutes the structure. Therefore, the data structure can be regarded as a collection of structured elements.
The element relationships discussed in data structures mainly refer to adjacent relationships or adjacency relationships.
logical structure
The logical structure of data is the whole set of logical relationships between data elements. The logical structure is user-oriented, reflects the logical relationship between data elements rather than the physical relationship, and is independent of the computer.
Tuples are usually used to represent the logical structure of data.
B=(D,R) Among them, B is a logical data structure, D is a collection of data elements, there may be multiple relationships between data elements on D, and R is a set of all relationships. Right now: D={di | 0≤i≤n-1, n≥0} R={rj | 1≤j≤m,m≥0}
R={rj | 1≤j≤m,m≥0} A certain relation rj (1≤j≤m) in R is a set of ordered pairs. For any ordered couple <x, y> (x, y∈D) in rj, x is called the first element of the ordered couple, and y is called the second element of the ordered couple. It is also called the first element of the ordered couple. The precursor element of the second element is called the successor element of the first element. For example, in the sequence of <x, y>, x is the predecessor element of y, and y is the successor element of x. If an element has no predecessor elements, it is called a starting element; if an element has no successor elements, it is called a terminal element. For symmetric ordinal pairs, this condition is met: if <x, y>∈r (r∈R), then <y, x>∈r (x, y∈D), parentheses can be used instead of angle brackets, that is ( x,y)∈r.
The logical structure of data is divided into linear structure and non-linear structure
Set (element relationship 0:0): There is no other relationship between data elements in the structure other than the relationship of "belonging to the same set", which is the same as the concept of sets in mathematics.
Linear structure (element relationship 1:1): If the structure is non-empty, there is and is only one start element and terminal element, and all elements have at most one predecessor element and one successor element.
Tree structure (element relationship 1:n): If the structure is non-empty, there is and is only one element as the starting element (also called the root node). There can be multiple terminal elements, and each element has zero or Multiple successor elements, each element except the start element has exactly one predecessor element.
Graphical structure (element relationship n:m): If the structure is non-empty, each element can have multiple predecessor elements and multiple successor elements.
storage structure
The storage structure of data is the way data is stored in computer memory. Storage structures are for programmers.
The mapping should meet two requirements
Store relationships between data elements
Store all elements
Common storage structure types
sequential storage structure
All elements are stored in a memory unit with consecutive addresses.
Logically adjacent elements are also physically adjacent, so no additional space is needed to express the logical relationship between elements.
chain storage structure
Data elements are stored in any storage unit, and this group of storage units can be continuous or discontinuous.
The logical relationship of data elements is reflected through pointer fields.
Index storage structure
Usually, an additional index table is created while storing element information.
Hash (or hash) storage structure
The basic idea of the hash storage structure is to directly calculate a value based on the keyword Tonggu Huaxi function of the element, and use this value as the storage address of the element
Data operations
The purpose of storing data in a computer is to perform one or more operations. Operations include function description (or operation function) and function implementation (or operation implementation). The former is based on logical structure, user-defined, and abstract. The latter is based on a storage structure and is expressed by programmers in computer language or pseudocode. It is a detailed process. Its core is to design the processing steps to realize a certain computing function, that is, algorithm design.
The same logical structure can correspond to multiple storage structures. The implementation process of the same operation in different storage structures is different.
Data structures and data types
A data type is a collection of values of the same nature and a set of operations defined on this collection.
Data structure refers to the organizational form and mutual relationships of data elements processed by computers, while data types are data structures that have been implemented in a certain programming language. With the support of data types provided by programming languages, various new data structures describing these data models can be gradually constructed based on various data models abstracted from the problem.
Abstract data types (ADT) refer to data logical structures and operations (abstract operations) abstracted from the mathematical model of solving problems, without considering the specific implementation of the computer.
Abstract data types are essentially a formal description of a solution problem (independent of computers), and programmers can implement it based on understanding.
ADT abstract data type name { Data Object: Declaration of data object Data relationships: declaration of data relationships Basic operations: declaration of basic operations }ADT abstract data type name
algorithm
An algorithm is a description of the steps to solve a specific problem. It is a finite sequence of instructions.
Five characteristics of algorithms
Finite. It means that the algorithm ends automatically after executing a limited number of steps without infinite loops, and each step is completed within an acceptable time.
Certainty. The operations performed in each case have a definite meaning in the algorithm and there is no ambiguity. And under any conditions, the algorithm has only one execution path.
feasibility. Each instruction of the algorithm is executable and can be completed by humans with the help of paper and pen.
Inputability. Algorithms have zero or more inputs. Input parameters are necessary in most algorithms, but for simpler algorithms, such as calculating the value of 1 2, no input parameters are required, so the input to the algorithm can be zero.
Outputability. An algorithm has at least one or more outputs. Algorithms are used for certain data processing. If there is no output, such an algorithm is meaningless. The output of an algorithm is a quantity that has some specific relationship with the input.
Analysis of Algorithms
Algorithm design goals
Correctness. Usability. readability. Robustness. High time performance and low storage requirements.
The purpose of algorithm analysis: analyze the spatiotemporal efficiency of the algorithm in order to improve the algorithm performance.
algorithm complexity
time complexity:
• It is used to measure how quickly the algorithm execution time increases as the problem size increases;
• Is a function of problem size: T(n) is a function of time scale. Time complexity mainly analyzes the order of magnitude of T(n)
• T(n)=O(f(n)) f(n) is the frequency of basic operations in the algorithm. Generally, we consider the time complexity in the worst case
Space complexity:
• It is used to measure the speed of the space required by the algorithm as the size of the problem increases;
• Is a function of problem size: S(n)=O(g(n)); the growth rate of the space required by the algorithm is the same as the growth rate of g(n).
Focus on complexity calculation
Commonly used time complexity relationships:
How to calculate complexity
Time complexity calculation (single loop body)
Directly focus on the number of executions of the loop body, set to k
Time complexity calculation (multiple loop bodies)
Two operation rules: multiplication rule and addition rule.
Chapter 2 Linear Table
definition
A linear list is a finite sequence of data elements with the same characteristics.
All data elements are of the same type.
A linear table is composed of a limited number of data elements.
Data elements in a linear table are position-related, that is, each data element has a unique sequence number.
Logical structure representation of linear table
The unique position of each element ai in the linear table is represented by the serial number or index i. For the convenience of algorithm design, the logical serial number and the storage serial number are unified, and both are assumed to start from 0, so that the element serial number i of a linear table containing n elements satisfies 0 ≤i≤n-1.
Abstract data type description
Sequence table
A linear list of length n is stored in a sequence table
The data array stores linear table elements The capacity of the data array (the maximum number of elements stored) is capacity. The actual number of data elements in the linear table size
basic arithmetic
When dynamically allocating space in a sequence table, the initial capacity is set to initcapacity. The capacity may need to be expanded when elements are added or inserted, and the capacity may need to be reduced when elements are deleted.
1. Overall establishment sequence table Create a sequence table from all the elements of the array a containing several elements, that is, add the elements in a to the end of the data array in sequence, and when overflow occurs, expand the capacity by twice the actual number of elements, size.
(1) Add element e to the end of the linear table Add(e)
(2) Find the length of the linear table size()
(3) Set the length of the linear table Setsize(nlen) Used to reduce the length of the linear table. When the parameter nlen is correct (0≤nlen≤size-1), set the length size to nlen, otherwise the corresponding exception will be thrown.
(4) Find the element with serial number i in the linear table GetElem(i)
(5) Set the element with serial number i in the linear table SetElem(i, e)
(6) Find the logical sequence number of the first element with value e in the linear table GetNo(e)
(7) Swap the elements of sequence number i and sequence number j in the linear table swap(i,j)
(8) Insert e as the i-th element in the linear table Insert(i, e)
(9) Delete the i-th data element in the linear table Delete(i)
(10) Convert linear table to string toString()
linked list
Single list
If each node only has one pointer member pointing to its successor node, such a linked list is called a linear one-way linked list, or singly linked list for short.
Each node is a LinkNode<E> generic class object, including the data member data that stores elements (let its data type be E) and the pointer member next that stores subsequent nodes.
Single linked list generic class LinkListClass<E>
Create table
Create a singly linked list from an array a containing n elements. There are two commonly used methods to create a singly linked list: head insertion and tail insertion.
Head insertion method
Starting from an empty list, read the elements in array a sequentially. Generate a new node s and store the read data in the data member of the new node. Insert the new node s into the head of the current linked list.
The order of the data nodes in the singly linked list created by the head interpolation method is exactly the opposite of the order in the a array.
tail insertion method
Starting from an empty list, read the elements in array a sequentially. Generate a new node s and store the read data in the data member of the new node. Insert the new node s into the end of the current linked list.
Need to set a tail pointer t
The order of the data nodes in the singly linked list created by the tail insertion method is exactly the same as the order in the a array.
insert
Inserting node operation: Insert node s after node p in the singly linked list. Inserting node operation: Insert node s after node p in the singly linked list.
Add element e to the end of the linear list Add(e)
Insert e as the i-th element in a linear table Insert(i, e)
delete
Delete node operation: delete the successor node of p node in a singly linked list.
Find
Find the node with serial number i (0≤i≤n-1, n is the number of data nodes in the singly linked list)
Find the element with serial number i in the linear table GetElem(i)
Find the logical sequence number of the first element whose value is e in the linear table GetNo(e)
Delete the i-th data element in a linear table Delete(i)
Revise
Set the element with serial number i in the linear table SetElem(i, e)
Swap the elements of sequence number i and sequence number j in the linear table swap(i,j)
length
Find the length of the linear table size()
Set the length of the linear table Setsize(nlen)
Used to reduce the length of the linear list. When the parameter nlen is wrong (nlen<0 or nlen>n), the corresponding exception is thrown. If nlen is equal to the length of the original singly linked list, it returns directly. Otherwise, the node p with the serial number nlen-1 is found. , set it as the tail node, so that the length of the new singly linked list is nlen.
Convert linear table to string toString()
logic
Double linked list
If two pointer members are set in each node to point to its predecessor node and successor node, such a linked list is called a linear doubly linked list, or doubly linked list for short.
Each node is a DLinkNode<E> generic class object, including the data member data that stores the element (let its data type be E), the pointer member prior that stores the predecessor node, and the pointer member next that stores the successor node.
DLinkNode<E>generic class
Double linked list generic class DLinkListClass<E>
Create table
Create a doubly linked list from an array a containing n elements. There are two commonly used methods to create a doubly linked list: head insertion and tail insertion.
Head insertion method
tail insertion method
insert
Inserting node operation: Insert node s after node p in the doubly linked list.
Let the indirect node pointer be modified as early as possible!
Algorithm for inserting a node with value e at position i in doubly linked list dhead
You can also find the node p with the serial number i in the doubly linked list (find the successor node), and then insert the s node before the p node (the successor only inserts the new node before).
delete
Delete node operation: delete the p node in the double linked list.
Algorithm for deleting node with serial number i in double linked list dhead
You can also find the node p with the serial number i-1 (find the predecessor node), and then delete its successor node.
Chapter 3 Stack and Queue
stack
definition
A stack is a linear list that can only be inserted or deleted at the same end. The end of the table that allows insertion and deletion operations is called the top of the stack, and the other end of the table is called the bottom of the stack. The insertion operation of the stack is usually called pushing or pushing, and the deletion operation of the stack is usually called popping or popping.
Features
Last-in-first-out means that the elements pushed into the stack last are popped out first. A stack is also called a last-in-first-out list.
abstract data type
basic arithmetic
sequence stack
Initially, the top pointer of the stack is set to -1, and the four elements of the sequential stack are as follows: The condition for the stack to be empty is top==-1. The condition for the stack to be full (stack overflow) is top==capacity-1. Here, the method of dynamically expanding the capacity is used, that is, when the stack is full, the capacity of the data array is doubled. The operation of pushing element e onto the stack is to first increase the top pointer of the stack by 1, and then place the element e at the top pointer of the stack. The pop operation is to first remove the element at the top pointer of the stack, and then decrement the top pointer of the stack by 1.
Sequential stack generic class SqStackClass<E>
Determine whether the stack is empty empty()
push(e) onto the stack
pop()
Take the top element of the stack peek()
chain stack
Initially, it contains only one head node head and sets head.next to null. The four elements of the chain stack are as follows: The condition for the stack to be empty: head.next==null. Since stack fullness occurs only when memory overflows, this situation is usually not considered. Push operation of element e onto the stack: Insert the node s containing the element as the first node. Pop operation: return the first node value and delete the node.
Method to realize
Type of node LinkNode<E>
Link stack class template LinkStackClass<E>
Determine whether the stack is empty empty()
push(e) onto the stack
pop()
Take the top element of the stack peek()
queue
definition
A queue is a linear list that can only be inserted or deleted at different ends. The end where insertion is done is called the rear, and the end where deletion is done is called the head or front. The insertion operation of the queue is usually called entering the queue or entering the queue (push), and the deletion operation of the queue is usually called dequeuing or leaving the queue (pop).
Features
First in, first out, that is, the elements in the advanced queue are dequeued first. Queues are also called first-in-first-out lists.
abstract data type
Queue abstract data type = linear structure Basic operations of queue
Use the data<E> array to store the elements in the queue. Convention: the head pointer is front (actually the previous position of the head element), and the tail pointer is rear (exactly the position of the tail element).
sequential team
circular queue
definition
Connect the front end and back end of the data array to form a circular array, that is, the table that stores the queue elements is logically viewed as a ring, which is called a circular queue (also called a ring queue).
When it is specified that there are at most m-1 elements in the queue, the queue empty condition is still rear==front. When the queue has m-1 elements, (rear 1)%MaxSize==front must be satisfied. In this way, the circular queue is initially set to front=rear=0, and its four elements are as follows: Team empty condition: rear==front. Team full conditions: (rear 1)%MaxSize==front (equivalent to trying to enter the team once, if rear reaches front, the team is considered full). Element e enters the queue: rear=(rear 1)%MaxSize, and places element e at this position. Element dequeuing: front=(front 1)%MaxSize, take out the element at this position.
The circular queue is connected end to end. When the rear pointer of the queue is rear=MaxSize-1, it should reach the 0 position if it advances one position. This can be achieved by using the mathematical remainder operation (%): (1) The head pointer of the team loops into 1: front=(front 1)%MaxSize (2) The tail pointer of the queue advances by 1: rear=(rear 1)%MaxSize
Sequential queues (including circular queues and non-cyclic queues) identify the queue status through front and rear, which are generally implemented using their relative values, namely |front-rear|. If the capacity of the data array is m, then there are m 1 states of the queue, which are empty, 1 element in the queue, 2 elements in the queue, ..., and m elements in the queue (queue full). The value ranges of front and rear are both 0~m-1, so |front-rear| has only m values. Obviously, m 1 states cannot be distinguished directly by |front-rear|, because there must be two states that cannot be distinguished. For this reason, there are only m-1 elements in the queue at most, so that the queue has exactly m states, and all states can be distinguished by the relative values of front and rear.
Generic class of circular queue SqQueueClass<E>
Since capacity expansion is complicated, a fixed-capacity queue is used here.
Determine whether the queue is empty()
Enter the queue push(e)
Dequeue pop()
Get the head element of the queue peek()
non-circular queue
Initially set both front and rear to -1 (front==rear) When an element enters the team, rear increases by 1 The element is dequeued and front is increased by 1
Initially, both front and rear are set to -1 (front==rear). The four elements of this sequence are as follows: Team empty condition: front==rear. The queue is full (overflow) condition: rear==MaxSize-1 (because each element entering the queue increases rear by 1, and when rear reaches the maximum subscript, it cannot increase anymore. The operation of element e entering the queue: first increase the queue tail pointer rear by 1, and then place the element e at that position (elements that enter the queue are always inserted at the end). Dequeue operation: First increase the queue head pointer front by 1, and then take out the element at that position (the dequeue element always comes out from the queue head).
Generic class SqQueueClass<E> for non-circular queues
Since capacity expansion is complicated, a fixed-capacity queue is used here.
Determine whether the queue is empty()
Enter the queue push(e)
Dequeue pop()
Get the head element of the queue peek()
chain team
How queues are implemented
Set front=rear=null initially. The four elements of a chain team are as follows: Team empty condition: frontr=rear==null, you might as well use front==null as the team empty condition. Since the queue is full only when the memory overflows, this situation is usually not considered. Element e is added to the queue: insert the s node storing e at the end of the singly linked list, and let the queue tail pointer point to it. Dequeue operation: Take out the data value of the head node of the queue and delete it from the chain queue.
Type of node LinkNode<E>
Chain queue generic class LinkQueueClass<E>
Determine whether the queue is empty()
Enter the queue push(e)
Dequeue pop()
Get the head element of the queue peek()
Chapter 4 String
definition
A string is a finite sequence of zero or more characters. Recorded as str="a1a2…an" (n≥0). The number n of characters contained in a string is called the string length. When n=0, it is called an empty string. A subsequence composed of any consecutive characters in a string is called a substring of the string. The string containing the substring is accordingly called the main string. Two strings are said to be equal if their lengths are equal and their corresponding characters are equal.
abstract data type
sequence string
definition
Like the sequence table, a data array and an integer variable size are used to represent a sequence string. Size represents the actual number of characters in the data array. For the sake of simplicity, the data array uses a fixed capacity of MaxSize (it can imitate the sequential table and change it to dynamic capacity).
Sequential string class SqStringClass
basic arithmetic
Find substrings: For a sequential string, find the substring starting from sequence number i and with length j.
Implementation: First create an empty string s. When the parameters are correct, the character sequence of the s substring is data[i..i j-1], with a total of j characters. When i and i j-1 are not in the valid sequence number 0~ If it is within the range of size-1, the parameter is incorrect and an empty string is returned.
Chain string
Use a singly linked list with the head node to represent the linked string
The node type of the link string is LinkNode (node size is 1)
A link string is uniquely identified by a head node, the link string class LinkStringClass
String insertion: Chain string inserts string t at position i
Implementation: First create an empty string s. When the parameters are correct, use the tail interpolation method to create the result string s: (1) Copy the first i nodes of the current chain string to s. (2) Copy all nodes in t to s. (3) Then copy the remaining nodes of the current string to s.
string
Main methods of String class The difference between the "==" operator and the equals method in string comparison: "==" can be used for comparison of basic data types. When used for reference object comparison, it is to determine whether the reference points to the same block address of heap memory. The function of the equals method is to determine whether two variables are references to the same object, that is, whether the contents in the heap are the same, and the return value is of Boolean type.
(1) When the objects are different and the content is the same, "==" returns false and equals returns true, for example: String s1 = new String("java"); String s2 = new String("java"); System.out.println(s1==s2); //false System.out.println(s1.equals(s2)); //true (2) For the same object, the results of "==" and equals methods are the same, for example: String s1 = new String("java"); String s2 = s1; System.out.println(s1==s2); //true System.out.println(s1.equals(s2)); //true (3) When String is used as a basic type, if the values are different, the objects will be different, so the results of "==" and the equals method are the same. For example: String s1 = "java"; String s2 = "java"; System.out.println(s1==s2); //true System.out.println(s1.equals(s2)); //true
String A string is a constant whose value cannot be changed after it is created. For this purpose, Java provides StringBuffer, a string buffer that supports variable strings. StringBuffer operates on the object itself every time instead of generating a new object, so it is recommended to use StringBuffer when the string content is constantly changing.