Java data Structure-two fork find tree continuation and balanced binary search tree

Source: Internet
Author: User

?? The previous article described the implementation of the two-fork lookup tree, where the insert operation is implemented using a non-recursive method, where a recursive implementation of the insert operation, the Java code is as follows, it is recommended to add to the previous article corresponding to FOBinarySearchTree.java ;

/** * @TODO 二叉排序树插入元素(递归方法) * @param e 需要插入的元素 * @return true or false */public boolean insert(E e){    insert(root,e);    return true;}/** * @TODO 二叉排序树插入元素的实现(递归方法) * @param node 二叉排序树跟结点 * @param e 需要插入的元素 * @return true or false */private FOBinarySearchTreeNode<E> insert(FOBinarySearchTreeNode<E> node , E e){    if (node == null) {        return new FOBinarySearchTreeNode<E>(e,null,null,null);    }    int cmpResult = e.compareTo(node.getE());    if (cmpResult > 0) {        node.setRightChild(insert(node.getRightChild(),e));        node.getRightChild().setParent(node);    } else if(cmpResult < 0){        node.setLeftChild(insert(node.getLeftChild(),e));        node.getLeftChild().setParent(node);    }else{        ;    }    return node;}

?? As for the recursive method of the binary search tree deletion, it is not implemented here, but continues to use the previous article, "Find the precursor to delete the node or the successor method to delete the operation."

Balanced binary search tree:
?? On the basis of the definition of binary search tree, the concept of balance is added, that is, the height of the Saozi right subtree of each node is up to 1 of the two-fork search tree.

?? The main point of the balance binary search tree is in its rotation algorithm, which is the rotation algorithm (that is, the binary balance finding tree loses balance needs to make the operation) has the following four kinds of situations:

LL: Also referred to as "left left"; the left subtree of the root node is inserted one at a time, and after inserting a node, the Zuozi of the left subtree of the root is also non-empty, resulting in a "height of the left subtree of the root" greater than 2 of the right subtree of the root, resulting in the AVL tree losing its balance.

LR: Also known as "around". The right subtree of the left son of the root node is inserted one at a time, and after inserting a node, the right subtree of the left subtree of the root is also a non-empty child node, resulting in a "height of the left subtree of the root" greater than 2 of the right subtree of the root, resulting in the AVL tree losing its balance.

RL: called "right-left". The left subtree of the right son of the root node is inserted once, and when a node is inserted, the Zuozi of the right subtree of the root is also non-empty, causing the "height of the right subtree of the root" to be 2 larger than the left subtree of the root, causing the AVL tree to lose its balance.

* rr*: called "right-right". The right subtree of the root node is inserted one at a time, and after inserting a node, the right subtree of the right subtree of the root is also a non-empty child node, causing the "height of the right subtree of the root" to be 2 larger than the height of the left subtree of the root, causing the AVL tree to lose its balance.

Here are two examples of four rotations:
?? Here is an excerpt from

LL's spin

?? ll loses balance and can restore the AVL tree back to balance with a single rotation. Such as:

?? The left side of the graph is the tree before the rotation, and the right is the tree after the rotation. As you can see, the rotated tree becomes the AVL tree, and the rotation takes only one time to complete.

?? For ll rotation, you can interpret this as: ll rotation is carried out around the "loss of balance of the AVL root node", that is, the node K2; and because it is ll situation, that is, left-to-left situation, with the hands of the "left child, that is K1" shaking vigorously. The K1 becomes the root node, K2 becomes the right subtree of K1, and the right subtree of K1 becomes the K2 left subtree.

The rotation of the LR

?? When LR is out of balance, it takes two rotations to restore the AVL tree to a balanced position. Such as:

?? The first rotation is "RR rotation" around "K1", and the second is "ll rotation" around "K3".

Copyright NOTICE: This article for Bo Master original article, if you need to reprint please specify the source and attached link, thank you.

Java data Structure-two fork find tree continuation and balanced binary search tree

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.