Left heap, p2c

Source: Internet
Author: User

Left heap, p2c

Package com. iflytek. heap;/*** left heap * @ author fgtian **/public class LeftListHeap {public static class HeapNode {int mValue; int mNpl = 0; HeapNode mLeftChild; HeapNode mRightChild ;} heapNode mRoot = null; public void insert (int value) {HeapNode node = new HeapNode (); node. mValue = value; merge (node);} public int delMin () {if (null = mRoot) {throw new NullPointerException ("null = mRoot ");} int value = mRoot. mValue; mRoot = merge (mRoot. mLeftChild, mRoot. mRightChild); return value;} public void merge (HeapNode node) {mRoot = merge (mRoot, node); if (npl (mRoot. mRightChild)> npl (mRoot. mLeftChild) {HeapNode l = mRoot. mLeftChild; mRoot. mLeftChild = mRoot. mRightChild; mRoot. mRightChild = l;} mRoot. mNpl = Math. min (npl (mRoot. mLeftChild), npl (mRoot. mRightChild) + 1;} public void merge (LeftListHeap heap) {merge (heap. mRoot);} public static int npl (HeapNode h) {if (null = h) {return-1;} return h. mNpl;} public static HeapNode merge (HeapNode h1, HeapNode h2) {if (null = h1) {return h2;} else if (h2 = null) {return h1 ;} else {// neither of them are nullint v1 = h1.mValue; int v2 = h2.mValue; if (v1 <= v2) {// use his h1.mRightChild = merge (h1.mRightChild, h2 ); if (npl (h1.mLeftChild) <npl (h1.mRightChild) {HeapNode node = h1.mLeftChild; h1.mLeftChild = h1.mRightChild; h1.mRightChild = node;} h1.mNpl = Math. min (npl (h1.mLeftChild), npl (h1.mRightChild) + 1; return h1;} else {h2.mRightChild = merge (h1, h2.mRightChild); if (npl (h2.mLeftChild) <npl (h2.mRightChild) {HeapNode node = h2.mLeftChild; h2.mLeftChild = h2.mRightChild; h2.mRightChild = node;} h2.mNpl = Math. min (npl (h2.mLeftChild), npl (h2.mRightChild) + 1; return h2 ;}}}}


What is the difference between the left heap and the oblique heap in the data structure? Detailed bonus points

Left-side heap Learning
Blog category: Data Structure
Algorithm Data Structure
Today I learned about the left-side heap and summarized it.

1. Left stack definition:

Yes, the following properties

1. The value of the parent node attribute is smaller than the value of the child node attribute;

2. For any node in the heap, the left son's zero path length> = the right son's zero path length;

Binary Tree.

Note: The Zero path length (npl) refers to the shortest path length from a node X to a node Y that does not have two sons, it can be seen that the npl of the node with 0 or one son is 0, and the npl (null) =-1 is defined;

2. left-side node definition:

Java code
Class Node <T> {

// Element
T element;
// Left Node
Node <T> left;
// Right Node
Node <T> right;
// Zero path length
Int npl;

Public Node (T element ){
This (element, null, null );
}

Public Node (T element, Node <T> left, Node <T> right ){
This. element = element;
This. left = left;
This. right = right;
This. npl = 0;
}
}

Class Node <T> {

// Element
T element;
// Left Node
Node <T> left;
// Right Node
Node <T> right;
// Zero path length
Int npl;

Public Node (T element ){
This (element, null, null );
}

Public Node (T element, Node <T> left, Node <T> right ){
This. element = element;
This. left = left;
This. right = right;
This. npl = 0;
}
} Operations on the left side:

The main operation on the left heap is "merge", because the merge will destroy the feature of the Left heap, and operations such as insert and delete will involve the merge of the heap, for example, delete the root node, it is equivalent to dividing a tree into two trees. Here we use a recursive idea. If h1 and h2 are left-side heaps, their Subtrees must also be left-side heaps, the sub-tree of the sub-tree must also be a left heap. Every leaf node of the tree that has been removed must comply with this rule. Therefore, when merging the sub-tree, we can think in the opposite direction, A small left heap is formed first, and then a large left heap is formed.

4. left-side heap definition code:

Java code
Public class LeftistHeap <T extends Comparable <T >{...... the remaining full text >>>

What is the difference between the left heap and the oblique heap in the data structure? Detailed bonus points

Left-side heap Learning
Blog category: Data Structure
Algorithm Data Structure
Today I learned about the left-side heap and summarized it.

1. Left stack definition:

Yes, the following properties

1. The value of the parent node attribute is smaller than the value of the child node attribute;

2. For any node in the heap, the left son's zero path length> = the right son's zero path length;

Binary Tree.

Note: The Zero path length (npl) refers to the shortest path length from a node X to a node Y that does not have two sons, it can be seen that the npl of the node with 0 or one son is 0, and the npl (null) =-1 is defined;

2. left-side node definition:

Java code
Class Node <T> {

// Element
T element;
// Left Node
Node <T> left;
// Right Node
Node <T> right;
// Zero path length
Int npl;

Public Node (T element ){
This (element, null, null );
}

Public Node (T element, Node <T> left, Node <T> right ){
This. element = element;
This. left = left;
This. right = right;
This. npl = 0;
}
}

Class Node <T> {

// Element
T element;
// Left Node
Node <T> left;
// Right Node
Node <T> right;
// Zero path length
Int npl;

Public Node (T element ){
This (element, null, null );
}

Public Node (T element, Node <T> left, Node <T> right ){
This. element = element;
This. left = left;
This. right = right;
This. npl = 0;
}
} Operations on the left side:

The main operation on the left heap is "merge", because the merge will destroy the feature of the Left heap, and operations such as insert and delete will involve the merge of the heap, for example, delete the root node, it is equivalent to dividing a tree into two trees. Here we use a recursive idea. If h1 and h2 are left-side heaps, their Subtrees must also be left-side heaps, the sub-tree of the sub-tree must also be a left heap. Every leaf node of the tree that has been removed must comply with this rule. Therefore, when merging the sub-tree, we can think in the opposite direction, A small left heap is formed first, and then a large left heap is formed.

4. left-side heap definition code:

Java code
Public class LeftistHeap <T extends Comparable <T >{...... the remaining full text >>>

Related Article

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.