Data structure AVL tree and AVL rotation, Haffman encoding

Source: Internet
Author: User

1. AVL Tree

The maximum height difference for the two subtrees of any node in the AVL tree is one, so it is also known as a height-balanced tree. Find, insert, and delete are O (log n) in both the average and worst case scenarios. Additions and deletions may require one or more tree rotations to rebalance the tree.

The balance factor of a node is the height of its left subtree minus the height of its right subtree (sometimes opposite). Nodes with a balance factor of 1, 0, or-1 are considered to be balanced. Nodes with a balance factor of 2 or 2 are considered unbalanced and need to rebalance the tree . The balance factor can be stored directly in each node or calculated from the height of the subtree that may be stored in the node.

1.2AVL rotation

The basic operations of the AVL tree generally involve the same algorithms that operate in an unbalanced two-fork search tree. But to do one or more of the so-called "AVL rotations" beforehand or later.

The table shows four cases in four columns , and each row represents the action to take in that case. In the case of left and right, only one rotation is required, and two rotations are required in the left and right cases.

Root is the root node of the lost balance tree, pivot is the root node of the rebalance tree after rotation

    • Insert

      Inserted into the AVL tree, you can insert a given value into the tree as if it were an unbalanced two-fork lookup tree, then retrace the root node from bottom to top, and rotate on all nodes that become unbalanced during insertion.

    • Delete

      Removed from the AVL tree, it can be done by rotating the node to be deleted into a leaf node and then directly removing the leaf node.

    • Search

      It can be done like a normal binary lookup tree, so it consumes O (log n) time because the AVL tree is always balanced.

1.3AVL node Count

An AVL tree with a height of H, with a maximum of node points n.

 

Think of what? Fibonacci is not ... The corresponding height h can be calculated according to the number of nodes N.

2. Huffman Tree

Huffman tree, also known as the optimal binary tree, is a two-fork tree with the shortest length of a weighted path . The so-called path length of the tree is the length of all the leaf nodes in the tree (the Joghen node is 0 layers, the path length of the leaf node to the root node is the layer of the leaf node).

The tree's weighted path length is recorded as wpl= (W1*L1+W2*L2+W3*L3+...+WN*LN), n weights wi (i=1,2,... N) constitutes a two-fork tree with n leaf nodes, and the corresponding leaf node path length is Li (i=1,2,... N). It can be proved that the wpl of Huffman tree is the smallest.

2.1 Structure of Huffman Tree

Assuming there are n weights, the Huffman tree is constructed with n leaf nodes. N weights are set to W1, W2 、...、 WN respectively, then the structure rules of Huffman tree are:
  
① will W1, W2 、..., wn as a forest with n trees (each tree has only one node);

② in the forest to select two root nodes of the minimum weight of the tree merge, as a new tree left and right sub-tree, and the new tree root node weights for its left and right subtree node weights of the sum;

③ removes the selected two trees from the forest and joins the new tree into the forest;

④ repeat ②, ③ step, until there is only one tree left in the forest, the tree is the Huffman tree obtained.

2.2 Huffman Code

Huffman coding is a kind of variable word length coding (VLC). The method is based on the probability of character occurrence to construct the shortest average length code word, sometimes called the best coding.

 

There are a,b,c,d,e five characters, the frequency (that is, the weight) is 5,4,3,2,1, respectively, Huffman coding.

First, the a,b,c,d,e is constructed into a Huffman tree (2.1);

By starting from the Haffman node, the left dial hand tree allocation code "0", the right sub-tree allocation Code "1", until the leaf node, and then from the root along each path to the leaf node of the code arranged together, then got Huffman code.

 

So the corresponding encoding for each character is: a->11,b->10,c->00,d->011,e->010

Huffman coding is an no-prefix code. Decoding is not confusing. It is mainly used in data compression, encryption and decryption and other occasions.

Data structure AVL tree and AVL rotation, Haffman encoding

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.