1.7~1.8 arithmetic review PART 1

 

An even-numbered node must be the left child, and an odd-numbered node must be the right child. To determine whether two adjacently numbered nodes are brothers, just look at whether the results after dividing them by 2 are the same. 

 

Since pre-order traversal is "root-left subtree-right subtree", and post-order traversal is "left sub-tree-right subtree-root", if the pre-order and post-order sequences of a binary tree are exactly opposite, Then the binary tree can only have one left and right subtree at each level, that is, the height of the binary tree must be equal to the number of nodes.
 If there is only one node in each layer, the relative order of each layer remains unchanged and becomes the access order.

 

The operation takes N times, and then terminating the recursion takes 1 time, that is, N+1 times 

 

The value of s is the sum of the arithmetic sequence 

 

  1. The correct statement below is (D).
    A) A logical data structure can only have one storage structure.
    B) The logical structure of data is a linear structure, and the storage structure is a non-linear structure
    . C) A logical data structure can have multiple storage structures, and various storage structures do not affect data processing. Efficiency
    D) A logical data structure can have multiple storage structures, and various storage structures affect the efficiency of data processing.

 

Logical structure: The structure definition is a mathematical description of the operation object and describes the logical relationship between data elements. For example, linear structure, tree structure, graph structure or mesh structure. They are all logical structures.

Physical structure: Also known as storage structure, it is the representation of data structure in the computer (also known as image). For example, arrays, pointers.

Linear list: It is a linear structure in logical structure , including sequence list and linked list.

Sequential table: A type of linear table. It is a linear table implemented using an array, so its storage structure (physical structure) is continuous.

Linked list : A type of linear list . Its storage structure uses any set of storage units to store data elements. So its storage structure can be continuous or not. Generally, the linked lists we talk about are discontinuous. There is a type of linked list represented by an array, called a static linked list, and its storage structure is continuous.

Array : A physical structure whose storage units are contiguous.
 

 

 

If it is empty, it is judged that the head of the team coincides with the tail of the team. If it is full, it is judged that the tail + 1 is the head of the team. 

 

Storage structure, similar to physical structure 

 

 

Constructing hash function method, number analysis method, square centering method, piecewise superposition method, pseudo-random number method, division and remainder method 

The purpose of setting the head node of a singly linked list is to simplify operations.

 

 

 

You have to compare yourself with yourself 

 

 

2,4,,7,12 

 

 

 

 

 

First 2, 5, ans=7, then 7, 7, ans=7+14=21, then 14, 9, ans=21+13=44 

 

The length of the weighted path of the Huffman tree is not equal to the number of root nodes. The two have nothing to do with each other. It means that the length of the weighted path has been added to the weighted path during construction.

#include<iostream>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int>>q;
int n,num,ans=0;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>num;
        q.push(num);
    }
    while(q.size()>1){
        int num1=q.top();
        q.pop();
        int num2=q.top();
        q.pop();
        ans+=(num1+num2);
        q.push(num1+num2);
    }
    cout<<ans;
    return 0;
}

 

 

The previous nodes are either larger or smaller than the later nodes. 

94 is not satisfied and is smaller than 91 

 

 

 

 

For each tree, except for the root node, there is an edge on each node. Then there are x trees, the number of non-root nodes is nx (the so-called non-terminal nodes), and the sum of the edges of all trees is nx =k, so x=nk 

 

 

 

If 39 is encountered for the first time, it does not match, then all subsequent numbers encountered should be larger than 39; if 101 is encountered again, it does not match, then all subsequent numbers encountered are smaller than 101; if 25 is encountered, the condition is not met. , so it is not a search path 

 

 

That is to say, for leaf nodes, the relative order of leaf nodes appearing is fixed, always after the bottom layer, and then from left to right.

That is, the relative order of nodes in each layer is fixed. Different traversal methods change the location of each layer. 

 

For example, the last node is a root node of the right subtree, and this root node has a left child. In mid-order seeding, this root node is still the last node, but in pre-order seeding, it is not necessarily the last node. , and its left child is the last 

If the preorder and midorder are exactly the same, the preorder is the left and right roots, the midorder is the left root and the right root, and the postorder is the left and right roots, so there is no right node in either of them. 

3 nodes can form 5 binary trees, three forms of binary trees

 N represents the total amount of data, FN represents the total possibility of popping so much data from the stack, and then assume that the Nth...number among these N numbers is finally popped off the stack. If the last one popped off the stack is N, then the previous There are

F(1)=F(0)F(0)=1,F(2)=F(0)F(1)+F(1)F(0)=2,F(3)=F(0)F(2)+F(1)F(1)+F(2)F(0)=5

F(4)=F(0)F(3)+F(1)F(2)+F(2)F(1)+F(3)F(0)=14

F(5)=F(0)F(4)+F(1)F(3)+F(2)F(2)+F(3)F(1)+F(4)F(0)=28+10+4=42

That is to say, the total number of operations in which K numbers were pushed to the bottom of the stack was always F(K-1), that is, K elements were pushed onto the stack, K was pushed onto the stack, but K-1 elements were popped out of the stack, and K was retained. , followed by all the remaining elements, with NK elements left, then there are F (NK) ways for them to enter and exit, and finally exit K, which is a

From F3=5 

 

First of all, the array subscript cannot be negative, so when inserting an element, you should add 1 first and then store the value. Otherwise, if you store the value first and then add 1, the array will go out of bounds, that is, A[0] will be stored at the bottom of the stack. element 

 

 

 

For binary search failure, 100 nodes constitute a complete binary tree. When the height is 1, the total number of nodes is 1, when 2 is 3, when 3 is 7, when N is 2^N-1, when N=6, The total number of nodes is 63, and when it is 7, the total number is 127. Therefore, it is a dissatisfied tree. When the search fails, it must be compared at least 6 times and at most 7 times.

The deleted position must also be marked and cannot be regarded as empty.

If the forest turns into a binary tree, the brother is on the right and the child is on the left.

 

V is the number of nodes, E is the number of edges 

 

 

 

The weighted average length means that the weight of the leaf nodes must also be taken into account instead of only calculating the path to the leaf nodes.

If you want a tree with n vertices, you need N-1 edges, and you need to subtract m-(n-1)=m-n+1 edges.

 

 

 

 

The preorder of the forest is the preorder of the binary tree, and the postorder is the inorder of the binary tree, thus determining the only binary tree. 

The smallest element must be the rightmost node. There is no right subtree, that is, there is no element smaller than it, but there may be a left subtree, that is, an element larger than it is connected to it; for A, there may be no children or only A child; for C, adjustments may be needed; for the largest element, there must be no left subtree. The left subtree in this question means larger than it, so the largest element should not have a left subtree, which is the same as B. reason

 

log2n+1 is the height of the binary tree, which is the height that is about to be filled or has just been filled. The minimum number of comparisons is log2n (if it is not a full binary tree)

 

The Kth layer has at most 2^(k-1) nodes, and a maximum of 2^k-1 nodes are shared.

 

That is to say, there are leaf nodes in the sixth time. It is not certain that the sixth time is the bottom layer. It is the layer to be filled. It may also be the next layer. The sixth layer is gradually covered. If it is the filled layer, then That’s 2^5-1+8=39

If it is covered, the sixth layer has 2^(6-1)=32 nodes, then there are still 8 leaf nodes, indicating that 24 nodes have been filled to the seventh layer. Pay attention to the last part of the sixth layer. A non-leaf node may have one child or two children. To ask the most, it is regarded as having two children, that is, 2^6-1+24*2=63+48=111

 

The number of nodes in the right subtree of p is n

Convert the forest to a binary tree, with children becoming left nodes and brothers becoming right nodes 

The first step is to convert each tree into a binary tree, using children as left nodes, and then merging each binary tree, using only brothers as right nodes; in the binary tree obtained in the first step, their root nodes are He has no children 

The number of left subtree nodes plus the number of root nodes of the converted binary tree is the number of nodes of the first tree, that is, mn, and the rest is the number of root nodes and left subtree nodes.

That is to say, after the original n turns, there are still n numbers. One more means that the final tree has no brothers, so the right pointer field of one more is empty. 

 

A simple path requires that all vertices are non-repeating, and a simple loop requires that all vertices are non-repeating except for the starting point and end point. 

Pay attention to the circular singly linked list with the tail pointer. The next pointer to the tail pointer is the head pointer, so it can play the role of a double linked list.

Because the double linked list can facilitate the first pointer to find the tail pointer forward. 

For a Huffman tree, it is a complete binary tree (note that complete ≠ complete). The degree of each node is 0 or 2, and the weight of the left child of any node is less than or equal to the weight of the right child. A non-leaf The weight of a node is equal to the sum of the weight of its left child and the weight of its right child.

A perfect binary tree means that there is no node with degree 1. It means that it can be stored sequentially. In this case, the weight of the tree may be larger than the leaf node, which will result in a tree with the left subtree as the leaf node and the right subtree as the tree. Then it is not a complete binary tree.

 

 

 

 

 That is to say, if they are all synonyms, they will be stacked backward. Then when deleting, the previous synonyms will be deleted first. If they are directly marked as empty, then when searching for subsequent synonyms, they will be directly marked as failed in advance and no longer The search continues, so deletion means putting a delete mark. When searching, you still have to continue searching backwards when you encounter it. But when inserting, you encounter a delete mark and you have to insert it.

That is, the so-called truncation of the search path of the synonym node that is filled in the hash table after it

 

 

Note that it is or, that is, it includes two situations. Synonym conflicts will occupy the space of non-synonyms. That is, if a conflict occurs, it will also occupy the space of synonyms.

 

Common hash functions include direct addressing method, digital analysis method, averaging method, remainder method, folding method, and random method. 

 

That is to say, first let 5 nodes form a fully connected graph, and then add any edge. Note that what is said here is at least, guaranteed

It is not the minimum requirement; when there are 11 edges, it is a connected graph no matter how it is placed.

 

 

FN=FN-1+FN-2+1,F1=1,F2=2,F3=4,F4=7,F5=12.F6=20, so the maximum height is 6

 

It is equivalent to two people, the left and right are determined, and then there are 4 people left, all arranged between AB

 

 

 

Note that it is a sorted tree, not necessarily a balanced tree, so the worst case is a skewed tree, ON, and when it is a balanced tree, it is LOGN.

Here, / is considered to have a higher priority than *, that is, the * is in the denominator, instead of calculating the previous fraction result.

There is such a suffix expression 

That is, calculating infix means converting it to suffix and then calculating the suffix expression. 

 

 

The so-called number of branches is actually the in-degree 

The path is actually a sequence of vertices, and the elements of the sequence are the intermediate nodes between the starting point and the source point.

 

 

 

In other words, this is to directly compare with the final sorting result to determine how many elements are at its final actual position. If it is greater than or equal to the restriction, it is possible, otherwise it is impossible. The same method applies to other sorting methods where each pass determines a final element (but they are on one side) 

Note that the storage size is m+1, and also note that the head pointer and tail pointer are both real pointers.

 

Selection sort means to select the largest/smallest element every time and put it into the specified position. Naive selection sort is to traverse the array every time to determine, and heap sort is to build a heap and get the smallest element directly from the top of the heap every time. Put it at the end, so it is selection sorting, which just optimizes the process of finding the minimum value.

2^10=1024, round down, and then +1. This +1 represents the part of the layer that is being filled but has not yet been filled, that is, the decimal part of the logarithmic result; the minimum height is a complete binary tree, that is, logn+1 , the largest is the leaning tree

 

 

The sum of the degrees of all vertices in an undirected graph is even, but not necessarily in a directed graph. 

In the discussion of data structures, data structures are logically divided into (C)

A Internal structure and external structure B Static structure and dynamic structure C  Linear structure and nonlinear structure D Compact structure and non-compact structure.

Logically divided into linear and nonlinear

 

Knowledge points 

Basic concepts of data structures and algorithm evaluation


Abstract data type (ADT) describes the logical structure and abstract operations of data , usually represented by triples ( data objects, data relationships, basic operation sets ) (or includes data and operations), so a complete data can be defined structure.

The data structure is a tuple (D, R), D is a finite set of data elements, and R is a finite set of relations on D.

Abstract data types can be divided into atomic types (int, char), variable aggregate types (sequences, variable length), and fixed aggregate types (complex numbers, composed of two real numbers, in a determined order) according to the different characteristics of their values.

There are three levels of data: data, data elements, and data items, where data elements are the basic units that make up data.

A data structure is a collection of data elements that have one or more specific relationships with each other.

The logical structure of data is independent of the storage structure.

All data elements in the same logical structure have the same characteristics, which means that not only the number of data items contained in the data elements must be the same, but also the types of the corresponding data items must be consistent.

The storage structure of data is the mapping of logical structure in the computer and depends on the computer language.

There are four basic structures in data structures: set structure, linear structure, tree, and graph.

The adjacency list is not a linear list, the adjacency list is a storage structure, and the linear list is a logical structure.

Hash storage is mapped to physical space through hash functions and cannot reflect the logical relationship between data.

In a linear structure, there is only one data element called "the first". Except for the first element, each data element in the set has only one predecessor.

Sets are the most primitive data structure.

Dictionaries can be organized in linear tables, hash tables, and skip tables.

The feasibility of the algorithm means that the operations can be realized by executing the basic operations a limited number of times.

Input and output are not criteria for measuring algorithms.

The time complexity of an algorithm cannot be used as an absolute measure of its execution time.

The relationship between algorithms and programs:

An algorithm represents a solution to a problem, and a program is a specific implementation of the algorithm on a computer.
Computer programs are a means of implementing algorithms, but they are not the only means. Cryptographical machines can also implement algorithms.
An in-place algorithm is an algorithm that uses a small, fixed amount of extra space to transform data.

The so-called time complexity refers to an upper bound on the estimated algorithm execution time in the worst case.

For the same algorithm, execution efficiency has nothing to do with language level.

The time complexity of the algorithm depends on the state of the data to be processed and the size of the problem.

Recursively defined data structures (lists, trees) usually use recursive algorithms to operate on them.

Theoretically, all recursion can be replaced by non-recursion.

The high-order bits of the data are placed at low addresses, and the low-order bits are placed at high addresses, which is the big-endian mode.

linear table


Arrays are structures that cannot be frequently inserted and deleted.

In a sequence list , elements that are logically adjacent are also physically adjacent.

Assume that the linear list has n elements. When you want to exchange two elements in the list, the sequential list is more efficient than the linked list. The sequential list requires three exchange operations, while the linked list needs to find the predecessor of the two elements. When you want to output these n elements in sequence, the efficiency of the sequential list and the linked list is the same, both O(n).

Linked storage uses pointers to represent logical structures, and the setting of pointers is arbitrary, so it can easily represent various logical structures.

Sequential storage is not only suitable for storing linear structures, but also for non-linear structures such as trees and graphs.

The sequential storage method is not used for the storage representation of various logical structures.

A linear table is stored in a cyclic singly linked list with a head node, and the head pointer is head. When head->next->next==head is true, the length of the linear table may be 0 or 1.

The storage space of a known linked list does not need to be dynamically allocated (static linked list), only the storage space of an unknown or changing linked list needs to be dynamically allocated (singly linked list, linear linked list).

The storage structure of a linear list that needs to allocate a large space and does not require moving elements when inserting and deleting elements is a static linked list. This is not possible with a singly linked list because the storage space of a singly linked list is dynamically allocated.

The head pointer of a singly linked list is head, there is no head node, and the null condition is head==0.

The pointer in a static linked list represents the position of the next element in the array.

There are no basic operations in the data structure. For example, multi-dimensional arrays have no insertion and deletion operations, and stacks and queues do not need to be searched.

In in-place reverse order operation, arrays are faster than linked lists.

Returning the head node, arrays are as fast as linked lists.

Linear table <= Pure table (tree) <= Reentrant table (there are connections between sibling nodes) <= Recursive table (picture)

Note that the head pointer ≠ the head node. The head node is an empty node and does not store elements.

Not every node in a linked list contains exactly one pointer.

stacks and queues

Rewriting a recursive program in a non-recursive way does not necessarily require the use of a stack . For example, calculating the Fibonacci sequence only requires a loop.

When using a singly linked list to implement a queue, the head of the queue is set at the head of the linked list to facilitate deletion of the head element.

The stack is a special linear structure that requires insertion and deletion operations to be limited to the end of the table.

For data structures such as chain queues and circular queues, you should pay attention to how many elements the queue can store at most, M or M-1.

Application of stack: expression evaluation, bracket matching, recursion, base conversion, maze solving, xml verification node is closed.

Applications of queues: level traversal, print buffer, CPU resource competition, limited breadth search, page replacement algorithm.

 

When this node has a left child, if its mid-order predecessor also has a right child, the node will not be accessed, so its mid-order predecessor must not have a right child. 

picture


Concept review: arcs, simple graphs, multigraphs, complete graphs, directed complete graphs, connected graphs, unconnected graphs, maximal/small connected subgraphs, connected components, strongly connected graphs, maximal strongly connected subgraphs, strongly connected Components, spanning trees, and spanning forests.

A graph is a sequence of edges consisting of vertices and adjacent vertices.

Complete graphs are divided into directed complete graphs and undirected complete graphs.

Two points should be noted when traversing the graph: 1. Each vertex is only visited once; 2. The points in the graph may not be connected.

A strongly connected directed graph has paths from any vertex to all other vertices, but not necessarily arcs.

A loop corresponds to a path, and a simple loop corresponds to a simple path.

If the elements below the diagonal of the adjacency matrix of a directed graph are 0, then the topological sequence of the graph must exist.

A connected component is a maximal connected subgraph of an undirected graph. All edges attached to the vertices in the connected component need to be added, so there may be loops in the connected component and cannot be used as a spanning tree of the graph. The spanning tree of the graph is a graph. a minimally connected subgraph of .

For edges with the same weight in an undirected graph, the spanning tree is not necessarily unique, because the graph itself may be a tree.

The shortest path must be a simple path.

Dijkstra's algorithm is suitable for finding the shortest path of a weighted graph with a loop, and can also solve the shortest path of any two vertices. It is not suitable for finding the shortest path with negative weights.

How to determine whether a directed graph has a cycle:

Depth-first traversal: During the depth search, if there is a vertex with an edge pointing to a vertex that has been traversed, and this vertex is not the vertex visited in the previous step, then a cycle exists.
Topological sorting: During the traversal process, there are still vertices in the graph, but the next vertex that can be added to the topological sequence cannot be found, indicating that there is a loop.
Union lookup can only determine whether there is a cycle in an undirected graph, but cannot determine whether a cycle exists in a directed graph.
In topological sorting, you can use a stack or a queue to temporarily store vertices with degree 0.

If the vertices of a directed graph cannot be arranged in a topological sequence, it means that there is a loop with a number of vertices greater than 1 in the graph, and this loop constitutes a strongly connected component.

Strongly connected graphs are for directed graphs. A strongly connected graph of n points has at least n edges (forming a loop) and at most n (n-1) edges.

The connectivity of directed graphs includes strong connectivity, weak connectivity, and multi-side connectivity.

In a graph structure, the relationship between nodes can be arbitrary, and any two data elements in the graph may be related.

In the AOE network, accelerating a certain key activity may not necessarily lead to the completion of the project ahead of schedule, but delaying any key activity will definitely cause the project to be delayed.

In an AOE network, there may be several critical paths at the same time, and the directed edges through which all critical paths pass are called bridges.

The numbering of each fixed point in the figure is artificial and can be changed as needed.

Define the path between vertices vi and vj in an undirected graph as a vertex sequence from vi to vj.

A graph can have no edges but cannot have no vertices.

When the edge weights are all equal, the BFS algorithm can be used to solve the single-source shortest path problem.

In any graph there must be an even number of nodes with odd degrees.

Find


Static search does not involve insertion and deletion operations, while dynamic search is designed for insertion and deletion operations, so the fundamental difference between the two search methods is the different operations applied.

That is, the binary search is a strict binary balanced search tree. 

An ordered sequence list of length 16, using half to find non-existing elements, and comparing up to 5 times.

log16+1=5, the height of a complete binary tree is 5, the worst is 5, at least 4

B+ tree supports sequential search and random search, B tree only supports random search, and both B tree and B+ tree can be used as file index structures.

B+ tree is a deformation of B-tree produced in response to the needs of the file system. The former is more suitable for operating system file indexing and database file indexing in practical applications than the latter. The former has lower disk read and write costs and more stable query efficiency.

Lexical analysis in the compiler uses finite automata and syntax trees.

Fast lookup of routing tables in the network mainly relies on cache, routing table compression technology and fast lookup algorithms.

The system generally uses a free space linked list to manage disk free blocks.

The "stacking" problem caused by hashing to the same address in open addressing is caused by conflicts between synonyms or between non-synonyms.

When the open addressing method is used to handle hash table conflicts, the average search length is higher than the chaining method, or the open hashing method is more efficient than the closed hashing method.

When the open hashing method is used to resolve conflicts in the hashing method, the value of the loading factor can be greater than 1.

A third-order B-tree must be a balanced three-way search tree, but a three-way search tree is not necessarily a third-order B-tree.

In a B-tree, all leaf nodes are on the same level, and the number of null pointers in all leaf nodes is equal to the total number of all keys + 1. (Similar to the number of non-leaf nodes + 1 = the number of leaf nodes in a full n-ary tree).

Clustering is less likely to occur when using rehashing to handle conflicts.

Synonym conflict does not equal aggregation. When the chain address method handles conflicts, synonyms are placed in the same linked list, which will not cause aggregation.

In hash searches that use open addressing to resolve conflicts, the main reason for aggregation is improper selection of conflict resolution methods.

The Hash operation can directly locate the storage address of the data based on the hash value. A well-designed Hash table can find the required data in constant time and is more suitable for searches in memory.

The internal implementation of STL_Map is a red-black tree, which is established in memory and cannot be used for disk operations. The search performance version is not as good as Hash search.

During the insertion process of the B-tree, if the root node splits, the tree height must be +1.

Internal sorting


Usually, when taking the k largest\smallest elements in a large amount of data, heap sorting is preferred.

To sort a 10TB data file, the merge method should be used.

When sorting internally, if insertion sort is not selected but merge sort is selected, the possible reason is that merge sort is more efficient.

Radix sort cannot sort real numbers of float and double types.

Both Hill sorting and heap sorting use the random access characteristics of sequential storage. If the sequential storage is replaced by chain storage, the time efficiency of the algorithm will be reduced.

When sorting an array in which most elements are sorted, direct insertion sorting (worst case O(n)) is more efficient than simple selection sorting (worst case O(n2)) because there is a gap between elements during direct insertion sorting. The number of comparisons is small.

Quick sort is most effective when the sorted data is completely disordered. When the data is basically ordered, it will degenerate into bubble sort.

In the halved insertion sort method, the number of comparisons during the sorting process is fixed and has nothing to do with the initial arrangement

Unstable sorting algorithms: fast, selection, heap, hash.

Increasing the number of merge paths can reduce the number of merge passes, thereby reducing the number of disk reads and writes and improving the speed of external sorting.
 

 


 

Guess you like

Origin blog.csdn.net/m0_73553411/article/details/135446887