Some concepts of trees and heaps

For a large amount of input data, the linear access time of a linked list is too slow. A tree is a simple data structure with an average running time of O(logN) for most of its operations. The binary search tree is the basis of the implementation of TreeSet and TreeMap.

Trees can be defined in several ways. One way is recursion, a tree is a collection of nodes. This set can be an empty set, if not, the tree consists of a node root called the root and zero or more non-empty subtrees. A tree is a collection of N nodes and
N-1 edges.
二叉树:Each of these nodes cannot have more than two child nodes. A property of binary trees is that the depth of an average binary tree is much smaller than the number of nodes N. The average depth of a binary search tree is O(logN), and the maximum is N-1.

二叉查找树:The property that makes a binary tree a binary search tree is that for each node X in the tree, all items in its left subtree have values ​​less than those in X, and all items in its right subtree have values ​​greater than those in X item. This means that all elements of the tree can be ordered in some consistent way.

AVL树(平衡二叉查找树):The depth of the tree must be O(logN). A balanced binary search tree is a binary search tree in which the heights of the left and right subtrees of each node differ by at most 1. For broken balance after insertion, adjust by rotating. (single rotation, double rotation)

伸展树:The tree guarantees that M consecutive operations on the tree starting from an empty tree take at most O(MlogN) time. It is based on the fact that worst-case time O(N) per operation is not bad for a binary search tree, as long as it happens relatively infrequently. (do not understand)

堆:is a binary tree that is completely filled, with the possible exception of the bottom layer, where elements are filled from left to right. Such a tree is called a complete binary tree. A complete binary tree of height h has 2^k to 2^(k+1)-1 nodes. This means that the height of a complete binary tree is logN.
A complete binary tree is so regular that it can be represented as an array without using chains. For any element at position i in the array, its left child is at position 2i, its right child is at 2i+1, and its father is at position i/2.
The property that makes operations fast is heap-ordering. Since we want to find the least element quickly, the least element should be at the root. If we consider that the subtree should also be a heap, then any node should be smaller than all its descendants.
So in a heap, for every node X, the key (value) in X's parent is less than the key in X. According to this heap ordering property, we can get the minimum value in constant time.
Insert we adopt the upper filtering strategy. To delete we use the lower filtering strategy. (specific code implementation)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325438006&siteId=291194637