Hello, welcome toPeanut Shell B2B Foreign Trade Network_Professional B2B E-commerce Platform_Free B2B Information Publishing Network!
18951535724

Establishment, insertion, deletion of fork stacks

   2026-08-28 NetworkingName2070
Key Point:I. Basic concepts:1 complete fork tree: if the depth of the fork tree is h, all the nodes of the layers except the h layer reach the maximum and all the nodes of the h layer are concentrated in the left subtree。2. Fork tree: the fork tree is a special, full fork tree with the maximum value for all layers。Two fork stackThe fork stack is a special fork tree that is based on complete fork trees and is subject to certain conditionsz

I. Basic concepts:

1 complete fork tree: if the depth of the fork tree is h, all the nodes of the layers except the h layer reach the maximum and all the nodes of the h layer are concentrated in the left subtree。

2. Fork tree: the fork tree is a special, full fork tree with the maximum value for all layers。

Two fork stack

The fork stack is a special fork tree that is based on complete fork trees and is subject to certain conditions。

Depending on the constraints, the fork stacks can be divided into two types:

Big and small。

Big top pile

That is, the value of any parent node is greater than or equal to the value of the child node it's about。

No reverse construction of the fork tree

Little top pile

So the value of any parent node is less than the value of the child's node. The root node of the fork stack is called the top of the stack, which is the maximum value inside the large top pile, the smallest value in the small top stack。

No reverse construction of the fork tree

Ii. Construction of the largest stack

The original data are a = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7} and are stored sequentially, the corresponding complete fork tree is shown in the following figure:

No reverse construction of the fork tree

1. Basic thinking

Each leaf node is first treated as a stack, and each leaf node is constructed with its parent node to create a stack with more nodes. Therefore, in constructing the stack, the parent point of the last node is first to be found, starting with the maximum stack from that node; until all branch nodes in front of the node are processed, the largest pile is constructed。

Assuming that the number of nodes of trees is n, the numbering starts with 1 and ends with n. For node i, the parent is i/2; for the left child is i2 and for the right child is i*2+1. The last node is marked as n and the parent node as n/2。

As shown in the chart below, the operation of our side against the upper arrays is 7 for the last node, 16 for the father, and the maximum stack is constructed from that node; after the construction has been completed, it is moved to the next node 2 until all the paternity points have been constructed。

No reverse construction of the fork tree

2. Codes achieved

Because stacks are based on complete fork trees, we don't need to use chain structures, we can save them directly in arrays。

Assuming the following table for the parent node is parent, get the child node from the parent node:

Left node bottom: 2 parent+1*

Subscript to right: 2 parent+2*

Assumptions that subnodes are down to son (both left and right):

Subscript parent node: (son-1)/2

So we can very simply show the relationship between nodes. Here's the code:

I'm sorry.
/ / number of data elements
i'm sorry.
// space size of storage elements
i'm sorry.
// storage of data elements
integer [data];

public maxheap (integer[] pdata, int maxsize){
this. Maxsize =maxsize;
data = new integer [maxsize];
for (int i = 0; i 0){
int parentindex = getparent (index);
/ current parent, right and left points
if (getright (parentindex) right node
if (leftdata > = rightdata)
/ left node greater than the parent node
if (leftdata > parentdata)
/ / left and father nodes, exchange of data
data [parentindex] = leftdata;
= parentdata;
// after each comparison is completed, the latter nodes are compared and re-comparison is made directly
index = heapsize-1;
♪ i'm sorry ♪
♪ orse ♪
/ right node, > left node, > parent node points
if (rightdata > parentdata)
// right and parental exchange of data
data [parentindex] = rightdata;
= parentdata;
// after each comparison is completed, the latter nodes are compared and re-comparison is made directly
index = heapsize-1;
♪ i'm sorry ♪
♪ i'm sorry ♪
♪ orse ♪
/ only left
integer leftdata = data [get left (parentindex)];
integer parentdata = data [parentindex];
/ left node greater than the parent node
if (leftdata > parentdata)
/ / left and father nodes, exchange of data
data [parentindex] = leftdata;
= parentdata;
index = heapsize-1;
♪ i'm sorry ♪
♪ i'm sorry ♪
index...
♪ i'm sorry ♪
♪ i'm sorry ♪

public int get heapsize()
i don't know.
♪ i'm sorry ♪

public integer[] getdata(){
integer[ ] integer = new integer [heapsize];
for (int i = 0; i)

Iii. Inserting nodes

The idea of the largest pile of inserted nodes is to add a node at the end of the stack and then rise along the stack of trees. It's about the same as the maximum stack of initialization processes。

Integer num
heapsize++;
data [heapsize-1] = num;
initmapheap (heapsize-1);
♪ i'm sorry ♪

Delete the top node

Delete the idea at the top of the maximum stack as follows: refer the last node of the stack to the root node, then remove the maximum value and then place the new node in the right place。

Public void del (integer num)
/ / found index to delete elements
int i = 0;
for (;i)
 
ReportFavorite 0Tip 0Comment 0
>Related Comments
No comments yet, be the first to comment
>SimilarEncyclopedia
Featured Images
RecommendedEncyclopedia
Table of Contents4 sections