TreeMap source code analysis 1

They are required to meet a binary basic properties - i.e. the value of any node in the tree is greater than its left child and its right child of less than. According to this basic nature of such a tree search efficiency is greatly improved. We know that in the process of generating a binary tree is very easy to imbalance, the worst case is one-sided (only the right / left subtree), this will inevitably lead to the retrieval efficiency is greatly reduced binary tree (O (n)), so in order to maintain binary tree balance, Daniel had proposed various implementations of the algorithm, such as: AVL , SBT , splay trees , Treap  , red-black tree and so on.

Balanced binary tree must have the following characteristics: it is an empty tree or a subtree left and right height not exceeding the absolute value of the difference between 1 and the left and right sub-trees are a balanced binary tree. Which means that any child nodes of the binary tree and so on, the left and right sub-tree height were similar.

To reach both nodes must be parent-child relationship.    Only one path from the root node to any node.    A plurality of sub-node has only one parent node. All parent node of a path is unique.     

Any node has only one parent, going up from only one path, you can reach other nodes on this path, not on the path to the node can not reach, each layer only one node on this path. 

Binary search to find more efficient than sequential search , but not for insertion.

To support efficient insertion operation, we seem to need a chain structure.

Single-link chain is unable to use binary search , as efficient from the binary search can quickly obtain any sub-array indexed by the middle element . To flexibility binary search efficiency and the list together, we need to be more complex data structures.

Can have both at the same time is a binary search tree BST .

Exchange left and right subtrees of all nodes:

public  class InvertBinaryTree {
     // switching nodes all 
    public the TreeNode invertTree (the TreeNode the root) {
         IF (= the root! null ) { 
            the TreeNode T = root.left; // exchange root 
            root.left = root.right; 
            root.right = T; 
            invertTree (root.left); // exchange left subtree 
            invertTree (root.right); // exchange the right subtree 
            return the root; 
        } the else {
             return  null ; 
        } 
    } 

    // 层序遍历
    public TreeNode invertTree_bfs(TreeNode root) {
        if (root == null) {
            return null;
        } else {
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                TreeNode p = queue.poll();
                TreeNode t = p.left;
                p.left = p.right;
                p.right = t;
                if (p.left != null) {
                    queue.offer(p.left);
                }
                if (p.right != null) {
                    queue.offer(p.right);
                }

            }
            return root;
        }
    }
}
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int x) {
        val = x;
    }
    public TreeNode(int val, TreeNode left, TreeNode right) {
        super();
        this.val = val;
        this.left = left;
        this.right = right;
    }
    public TreeNode() {
        super();
    }
}

Binary sort tree , also known as binary search tree, also known as Binary Sort Tree , also known as Binary Search Tree , also known as BST

It has the following properties:

Define the empty tree is a BST

Values ​​of all the nodes is less than the value of the left subtree of the root

Values ​​of all the nodes are greater than the value of the right subtree of the root of

Left and right subtrees are BST ( recursive definition )

Ascending sequence preorder

Sequentially inserted 4 , 2 , 6 , 2 , 1 , 3 , 5 , 1 , 7 :
insert4root node is directly inserted, insert2is less than4on the left, inserted6is greater than4on the right, insert2is less than4on the left, and4left2Comparative equal not inserted, insert1is less than4on the left, on the left there is2, less than2in the2left, insert3is less than4on the left, is greater than2on the right side,5is greater than4 on the right side, 5 is less than 6 on the left, a less than 4 on the left, a less than 2 on the left, there is a 1 is equal to not move, 7 is greater than 4 is greater than 6.

Key-value pairs inserted:

Sequentially inserted {4: a}, {2 : b}, {6: c}, {2: d}, {1: a}, {3: d}, {5: e}, {1: f}

{2: d} replace {2: B} , {. 1: F} Replace {1: a}

Binary search tree Binary Search Tree  BST : large side of the small side.

AVL : while large side of small , while the left and right subtrees height difference can only be 0 , 1 . Solve the binary search tree is to become extreme conditions list.

Red-black tree: RedBlackTree , RBT . AVL adjust the tree needs about rotation, AVL high search efficiency but low insertion and deletion of efficiency , on the now red-black tree .

1. red-black tree like AVL as always maintain absolute balance

2. The relative balance

3. If H (left)> = H (right) , then: H (left) <= 2 * H (right) + 1'd , but BH (left) === BH (right ) Black equal the height , H (left ) <H (right) Likewise

4. Theorem: N nodes of the RBT , the maximum height is 2 log (N +. 1) , the reference rigorous proof CLRS

5. query efficiency RBT less than AVL

6. insert, delete, efficiency better than AVL

Red-black tree is a balanced binary tree, the balance catechu B tree, is essentially a class B-Tree . Small aside large on the other side, in order to prevent the list are small becomes , so to balance.

AVL is achieved by balancing the height, the SBT is maintained by the balance node domain, the red-black tree to maintain the balance by red and black nodes. Red-black tree traversal sequence nondecreasing.

The first tree: preorder violation unabated.

Lesson 3: The root node is black.

Lesson 4: Red child node is black.

Fifth trees: black high are not equal.

Red-black tree traversal is simple: Less than go left, left greater than right.

Traversal into the breadth and depth-first priority.

Red-black tree shortest path does not exceed the longest path from the root to the leaf node 2 times . Ensures self-balance, will not cause the node to become a full list leads on a path so that low search efficiency.

map.put(12, 12);
map.put(1, 1);
map.put(9, 9);
map.put(2, 2);
map.put(0, 0);
map.put(11, 11);
map.put(7, 7);
map.put(19, 19);
map.put(4, 4);
map.put(15, 15);

TreeMap interfaces:

 

 

Guess you like

Origin www.cnblogs.com/yaowen/p/11204228.html