Algorithm design and data structure learning _ 4 ("data structure and problem solving" part4 notes)

Source: Internet
Author: User

 

  Preface:

The data structure can only be understood in terms of concept. The key is to use it in subsequent practices. Well, this section is still the notes in data structures and algorithm analysis in C ++ (second edition). Take notes in some water, part 1 of the book, part 1 is skipped for the moment (that is a specific example ). The contents of this operation include stack and queue, linked list, tree, binary tree search, hash table, and binary heap. The queues and linked lists are relatively simple, and tree, binary heap, and hash tables are relatively difficult.

 

  Chap16:

If the data member of the class is first-class (such as vector), the big-three for the class will be automatically implemented without the need to complete it. If you want to implement copy constructor, You can first implement copy assignment, and then call copy assignment in copy constructor, because copy-related operations only need to copy data members in the object.

Stack and queue can be implemented in two ways: array-based and linked list-based. The common operations of these two methods are completed in constant time. Generally, array-based implementation is faster to use, but the array-based implementation is slightly more complicated and wastes some space.

Generally, when small objects are stored, stacks and queues can be implemented in the form of arrays, while large objects are stored in the form of linked lists.

 

  Chap17:

In a linked list, adding a header node is very important. There is no data member in the header node and only a pointer member. It points to the first element in the linked list and has a header node, it is convenient to delete any element in the linked list.

Friend functions are irreversible.

Incomplete class declaration is because some other classes need to be used inside the class, and these classes are not implemented yet. It tells the compiler that these classes will be implemented later, therefore, you can declare it in advance.

Design a linked list. Three corresponding classes can be designed: The linked list class itself lList, the linked list node class llistnode, and the class llistitr for the position of the traversal chain table. among them, llistitr is mainly responsible for interaction, and sometimes it is necessary to design a constlistiter.

Circular linked list and two-way linked list can be used together.

After inheriting from a common linked list, you can form a sort linked list. That is, the size of element values (not pointers) in the linked list is ordered.

 

  Chap18:

The implementation of the tree can be recursive or non-recursive. Non-recursive implementations are more straightforward to understand, and the Implementation Code of recursion is more compact.

The height of a node in the tree indicates the length of the segment to the deepest node connected to the node.

The size of a node refers to the number of its child nodes (including its own), so the size of the leaf node is 1.

Because the number of subnodes of a node in the tree is uncertain, if you give each node a fixed number of pointers, you can only take the largest number of children in the node as the final number of pointers, this will waste a lot of storage space. The common solution to this problem is the first child/next brother (first child/next sibling) method, that is, each node provides two pointers, one of which points to its leftmost child, another Pointer Points to its next brother. If no corresponding node exists, it is left empty.

The time for first-order traversal and later-order traversal to access each node is constant time, and the time for traversing the entire tree is linear.

Expression Tree, Harman tree, Binary Search Tree, and priority queue are common applications of Binary Trees.

A binary tree can combine two trees into one tree.

* & P is equivalent to * (& P), where p is a reference to the pointer variable.

Reference transfer and pointer transfer are different, although they are all a local variable in the space of the called function stack, however, any processing of referenced parameters will operate the relevant variables in the main function in an indirect addressing method. If you change the pointer address in the called function for pointer passing parameters, it will not affect the variables related to the main function. If you want to change the relevant variables in the main function by passing the pointer parameter, you must use a pointer to the pointer or pointer reference. Therefore, when a function transmits a pointer reference, if the reference is deleted within the function, the reference must be set to null before the function returns.

The node replication in the tree adopts the method of sequential traversal, while the node size, height, and makeempty use the method of sequential traversal.

First-order traversal, middle-order traversal and post-order traversal can be implemented using stacks, while hierarchical traversal can be implemented using queues. Stack is used to implement the traversal of the first three trees. An access counter is required for each node. For example, when the access counter of the node is 1, it indicates that the number of its left child is accessed, if the value is 2, it indicates the number of the right child. If the value is 3, it indicates that the current node is accessed. Other traversal methods are similar.

 

  Chap19:

A binary search tree is a tree in which all values of the Left subtree of a node in a binary tree are smaller than those of the node, and all values of the right subtree are larger than those of the node. Therefore, if you use the Middle-order traversal function, all nodes in the tree are sorted in ascending order. Nodes of the same size are allowed in the binary search tree (depending on the degree of rigor ).

It is easier to use the binary search tree to find a node, and it is easier to find the minimum and maximum values. It is troublesome to delete a node. When the deleted node is a leaf node, it can be directly deleted. When the deleted node has only one child, it can be directly replaced by a child node. When the deleted node has two children, replace the deleted node with the smallest node in the right subtree (that is, the leftmost node in the right subtree, since the smallest node is either a leaf node or only a single child (and can only be right), the processing after it is removed is similar to the previous processing.

If a class contains an overloaded function and one of the overloaded functions is a virtual function, is its overloaded function also a virtual function?

If a number is randomly selected in a set to construct a search binary tree, the probability of the constructed Balance Tree is higher than that of the non-Balance Tree.

The internal path length of a binary tree is defined as the sum of the depth (depth) of all nodes in the tree. It is often used to evaluate the cost of a successful search on the tree. If the number is arranged with equal probability, the average length of the internal path of a binary tree is about 1.38nlogn.

The length of the external path of a binary tree is defined as the sum of the depth of all N + 1 null nodes (these nodes are also known as external Tree nodes) next to the leaf node. It is often used to evaluate the cost of a unsuccessful search in the tree (or the cost of executing the insert operation ).

The length of the external channel is expressed by EPL (t), the length of the internal channel is expressed by IPL (T), and N represents the number of nodes in the binary tree. Therefore, EPL (n) = IPL (N) + 2N.

The average time complexity of common operations on the binary search tree is O (log (n )). however, if ordered data is input, the worst operation time may occur. This is mainly due to the imbalance of nodes in the number. Therefore, we need to add some external structural constraints to the tree, for example, if the depth of some nodes is not allowed to be too deep than that of other nodes, the balanced binary tree uses this idea. The balanced binary tree is more complicated to delete and insert operations, however, the search speed is faster than that of the standard binary tree.

The condition of the balanced binary tree is that the depth of the tree is O (log (n )).

The AVL Tree weakens the condition of the balanced binary tree. It only needs to meet the maximum difference between the height of the Left subtree and the right subtree of all nodes in the tree.

A tree with a height of H must have at least C ^ h nodes, and an AVL tree must have at least F (H + 3)-1 nodes.

Inserting a number in the AVL tree may make the tree no longer an AVL Tree. Therefore, you need to rotate nodes that do not meet the AVL features, two common types of rotation are single rotation and double rotation.

There are two types of single rotation: 1st, which is caused by inserting a node on the left of the Left subtree of the current node. At this time, the single rotation is to rotate the left subtree of the current node. 2nd cases are caused by inserting a node to the right of the right subtree of the current node. At this time, the single rotation is to rotate the right subtree of the current node.

Double rotation is also divided into two situations. The 1st types are caused by inserting a node to the right of the Left subtree of the current node. In this case, you must first select the right subtree of the Left subtree of the current node, select the left subtree of the current node. 2nd cases are caused by inserting a node on the left of the right subtree of the current node. At this time, you must first rotate the left subtree of the right subtree of the current node, and then rotate the right subtree of the current node.

The property of the red/black tree: The red/black tree belongs to the binary search tree. each node in the tree is colored in red or black. The root node is black. If a node is red, the child node must be black. The path from a node to a null node must contain the same number of black nodes.

If the path from the root node to the null node contains B black nodes, the tree contains at least 2 ^ black nodes of the B-1. The search time complexity of the red/black tree is log (n ).

When a node is inserted into the red-black tree (from bottom to top), the two consecutive nodes are red, which violates the definition of the Red-black tree. Therefore, the nodes need to be rotated and discolored. Symbol mark: the current node is P, its subnode is X, its parent node is g, and its sibling node is S. If P is red, rotation and discoloration can be divided into two categories. The first type is when S is black. If X is inserted to the left of P, the left subtree of G is rotated and the color is changed properly, if X is inserted to the right of P, you must first rotate the right subtree of P, then rotate the left subtree of G, and change the color accordingly. The second case is that when S is red, it is difficult to handle, so we should avoid insertion in this case.

When the color of the red/black tree changes from top to bottom, if two subnodes of a node are met, the color of the node and its two subnodes needs to be reversed, if both the node and the parent node are red after the reversal, the previous method is used to solve the problem through one or two rotations (the S node is red at this time, this is a top-to-bottom scan. If this happens, it will be processed in the previous scan ).

When implementing the red/black tree class, you need to set two sentinel signs. One is nullnode, which is used to represent the NULL pointer and it is always black. One is the header, a pseudo root node. Its right node points to the real root node, and its element value is defined as negative infinity.

A cycle can be used for operations on a node, and a recursion can be used for batch nodes.

In the red/black tree, when a node has two red child nodes or a new node is inserted, the color is reversed.

Only red nodes can be deleted when the red and black trees are deleted. If the black nodes are deleted, the last red and black nodes are not satisfied.

When deleting a node from top to bottom (only black nodes are considered here, because Red nodes can be deleted directly ), you also need to determine whether the sibling node of the parent node is a red node to rotate or reverse the color.

The nature of the AA tree: The left child node must not be red on the basis of the red/black tree. This condition is much simpler than the operation on the red/black tree. In addition, nodes in the tree are not represented by color, but by level. The level of the leaf node is 1. The level of the red node is the same as that of its parent node. The level of the Black node is smaller than that of the parent node.

Horizontal links are used to connect a node with its sub-nodes with the same level (in fact, they are connected to a node and its red right node ). Therefore, it is impossible to have two consecutive horizontal links. If the level of a node is greater than or equal to 2, it has two subnodes.

Like the red and black trees in front, if a node is inserted at the bottom of the tree, only red nodes can be inserted. At this time, you may need to rotate the tree to satisfy the properties of the AA tree.

If the insertion of the AA tree destroys the properties of the AA tree, the continuous skew and split methods can be used for processing. Skew mainly processes the horizontal link line to the left, and split mainly processes the continuous link line to the right.

Set has more functions than list, so it is complicated to implement.

When implementing set, you can use a stack to store the paths of all current nodes.

When using a binary tree to store a large amount of data, if the memory is not enough, you need to read data from the hard disk, and the speed of reading data from the hard disk is several orders of magnitude slower than that in the memory, it is unreasonable to use big-Oh to analyze the time complexity. To reduce the data access time, we need to reduce the height of the nodes in the small tree. We can use more branches of the tree. B-tree is one of them.

B-tree needs to meet the following features: data is stored only on the leaf node, and key values (KEYS) for judgment are not stored on the leaf node ). Assuming that each node in Tree B can only have a maximum of M sub-trees, a non-leaf node can have a maximum of M-1 key values. The root node subtree must be in the range of 2 ~ M. The number of child nodes of other non-leaf nodes must be in m/2 ~ M. The number of data values contained in the leaf node must be in the range of [l/2 ~ Between L.

 

  Chap20:

Hash Tables only support partial operations on elements, such as common insert, delete, and search operations.

The hash function converts an item into a small array index value.

A common problem in the hash table is access conflict. To solve this problem, common methods include linear probing, quadratic probing, and separate chaining.

If linear probing is used and its loading rate is λ, each 1/(1-λ) in the hash table has a value (assuming independence). If it is not independent, the value is about (1 + 1/(1-λ) ^ 2)/2. In linear probing, the hash function results occupy the table units unevenly and form a region, which may easily cause a clustering.

In quadratic probing, if a conflict exists, K ^ 2 is added instead of K.

If the table size is a prime number and the quadratic probing method is used to solve the conflict, when half of the table is empty, each new element can be inserted, and each unit will not be inserted twice. To expand a table, make sure its size is a prime number, and the Members in the original hash table need to recalculate its position in the new table.

Quadratic probing can solve a clustering problem, but it also has a secondary clustering problem. Double hashing (that is, the second hash function) can solve this problem.

Separate chaining hashing is a common practice of adding new elements in the form of a linked list at the position where a conflict occurs in the hash table. As long as the linked list is not too long, its operation time is still relatively small.

The compiler uses the hash table to save the variables declared in the source code. At this time, the hash table is also called a symbol table. The hash table is designed in games (especially chess games ), spelling checks and other fields are also frequently used.

 

  Chap21:

The performance of binary heap is between the Binary Search Tree and the queue. It is often used to implement the priority queue.

The depth of A Complete Binary Tree with N nodes is floor (log (N). The left and right pointers are not required in the Complete Binary Tree because they are stored in arrays.

Features of heap-order:

All parent nodes must be no less than or greater than child nodes. It is a complete binary tree, And the binary search tree is not necessarily a Complete Binary Tree.

The simplest operation of heap-order is to find the minimum value, because the root node is the minimum value. In the insert operation of heap-order, first Insert the element to the end, and then compare it with its parent node value. If it is larger than it, OK. If it is smaller than the parent node, You need to swap it with the parent node and continue to compare it with the new parent node until the conditions are met.

If the height of the optimal Complete Binary Tree is H, the number of nodes N is 2 ^ (H + 1)-1, and the height of all nodes is the sum of N-H-1.

Generally, the speed of the heap sorting algorithm is not fast, but it is easier to implement the heap sorting algorithm.

 

References:

Data Structures and algorithm analysis in C ++ (second edition), Mark Allen Weiss.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.