Red-black tree principle of rotation and balanced binary tree Detailed _ a little class (multi-shore College)

Red-black tree

  1. Introduction of the tree

    • Trees have features are:

    (1) each node has zero or more child nodes

    (2) no parent node is called the root node

    (3) Each non-root node has only one parent node and

    (4) In addition to the root, each sub-node can be divided into a plurality of disjoint sub-trees

    mark

    • Noun understand:
      • Node: refers to an element tree;
      • Of node: refers to the number of nodes of the subtree has a binary tree is not greater than 2;
      • Leaves: node 0 degree, also referred to as terminal nodes;
      • Height: the height of the leaf node is 1, the maximum height of the root node;
      • Parent: if a node contains a child node, this node is called a parent node to its child nodes
  • Child nodes: the child node is a parent node of the next layer.
    - level node: starting from the root, root for the first layer, the second child of the root layer, and so on
    - sibling node: The node share a common parent of mutual called sibling

    • degree

      mark

    • Depth (Depth) is the maximum height of the tree or hierarchical tree node.

      mark

  1. Binary Tree

    • Binary tree each node has at most two sub-trees tree structure.

    • It has five basic forms: the binary tree may be an empty set; root is left empty subtree or right subtree; or the left and right subtree are all empty. img
  2. Why do we need a balanced binary tree?

    Write pictures described here

    To prevent this from happening, we hope to have an algorithm, our unbalanced binary sort tree into a balanced binary tree sort. This allows us to sort of binary tree structure optimization.

  3. Balance factor

    • The height of the left subtree of the node - the height of the right child node of the tree, not only about poor sub-tree height.

    1558936914926

  4. Balanced binary tree rotation

    • LL型
      1558937058556

    • LR type
      Here Insert Picture Description

    • Type RR
      Here Insert Picture Description

      • A rotation adjustment
        Here Insert Picture Description
    • RL type
      Here Insert Picture Description

      • The first rotation, RR type
        Here Insert Picture Description
      • Second rotation, RR type adjustment
        Here Insert Picture Description
    • I.

      • Add a new node 3
        Here Insert Picture Description
      • According to LL type, once a right-handed adjustment
        Here Insert Picture Description
    • Scenario 2

      • Adding nodes 7
        Here Insert Picture Description
      • According to LL type, once a right-handed adjustment
        Here Insert Picture Description
    • Three cases

      • Adding node 13
        Here Insert Picture Description
      • It requires two rotations, the first left-handed rotation type Cheng LL
        Here Insert Picture Description
      • According to LL type, a right-handed
        Here Insert Picture Description
    • Four cases

      • Adding node 14
        Here Insert Picture Description

      • LL first rotary drive type, the first rotation
        Here Insert Picture Description

      • Second, based on the LL type, adjust

        Here Insert Picture Description

    • Other add nodes to the right, the mirror is adjusted based on several cases, the first type is adjusted to RR and RR based secondary adjusted to balance.

      • Examples of a
        Here Insert Picture Description
      • Add 17 nodes
        Here Insert Picture Description
      • RR based on type, can be a rotating balance
        Here Insert Picture Description
      • Example Two
        Here Insert Picture Description
      • RR first rotary drive type, making adjustments
        Here Insert Picture Description
      • Based on RR type adjustment
        Here Insert Picture Description
  5. Red-black tree Introduction

    红黑树(Red Black Tree) 是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构。

    它是在1972年由Rudolf Bayer发明的,当时被称为平衡二叉B树(symmetric binary B-trees)。后来,在1978年被 Leo J. Guibas 和 Robert Sedgewick 修改为如今的 “红黑树” 。

    R-B Tree,全称是Red-Black Tree,又称为“红黑树”,它一种特殊的二叉查找树。红黑树的每个节点上都有存储位表示节点的颜色,可以是红(Red)或黑(Black)。

  6. 红黑树的特性

    • 性质1. 节点是红色或黑色。

    • 性质2. 根节点是黑色。

    • 性质3 每个叶节点(NIL节点,空节点)是黑色的。

    • 性质4 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)

    • 性质5. 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。

  7. 左旋和右旋

    • 左旋

    Write pictures described here

    • 右旋:

    Write pictures described here

  8. 插入操作:

    在对红黑树进行插入操作时,我们一般总是插入红色的节点,因为这样可以在插入过程中尽量避免对树的调整。 那么,我们插入一个节点后,可能会使原树的哪些性质改变列?

    如果插入的节点是根节点,性质2会被破坏,如果插入节点的父节点是红色,则会破坏性质4。 因此,总而言之,插入一个红色节点只会破坏性质2或性质4。

    我们的恢复策略很简单,插入修复具体操作情况:

    1. 情况1:插入的是根节点。

    原树是空树,此情况只会违反性质2。

    对策:直接把此节点涂为黑色。

    1. 情况2:插入的节点的父节点是黑色。

      此不会违反性质2和性质4,红黑树没有被破坏。

      对策:什么也不做。

    2. 情况3:当前节点的父节点是红色且祖父节点的另一个子节点(叔叔节点)是红色。

      此时父节点的父节点一定存在,否则插入前就已不是红黑树。与此同时,又分为父节点是祖父节点的左子还是右子,对于对称性,我们只要解开一个方向就可以了。 在此,我们只考虑父节点为祖父左子的情况。 同时,还可以分为当前节点是其父节点的左子还是右子,但是处理方式是一样的。我们将此归为同一类。

      Solution: The parent and uncle of the current node black, painted red grandparent, grandparent point to the current node, the current node restart from the new algorithm.

      3 for the previous case, a change [the current node is node 4]:
      Write pictures described here

    Changes:

    Write pictures described here

    1. Case 4: The parent of the current node is red, black uncle node, the current node is the right child of its parent

      Solution: the parent node of the current node as the new current node to the new current node as a fulcrum left.

      As shown below, the pre-change [the current node is node 7]:
      Write pictures described here

    Changes:

    Write pictures described here

    1. Case 5: The parent of the current node is red, black uncle node, the current node is the left child of its parent

      Solution: parent node to black, to red grandparent node, grandparent node as a fulcrum in a right-handed

      As shown below [the current node is node 2]
      Write pictures described here

    Changes:

    Write pictures described here

    to sum up:

    After the above case of three operations to repair a schematic view of the insertion case 3, case 4, case 5 and the like, one will find that the repair of the latter case is inserted into a series of 4, 5 are for the case 3 is inserted into the case after the node 4, the operation performed, However, pointing to the current node pointer N has been changing. So, you can think of: the entire down, 3,4,5 situation is a complete insertion of the repair of operational processes.

Little public classroom No.
Public concern number - free access to core knowledge [JAVA]! !
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yidiankt/p/11458040.html