Harman tree, Harman

Source: Internet
Author: User

Harman tree, Harman

I am not talking about the basic definition of the Harman tree. I have not written many features about this tree. It is a job in our reference books. There are two functions, one is the expression of the child's parent and parent, and the most binary tree is shown. The other is gone. In addition, during my debugging today, I found that there are still many deficiencies in my code. For example, there is no way to complete the output in my drawing function, however, the last four nodes cannot be output. I am still searching for the reason,

Next, let's talk about my ideas on the concept of the Harman tree:

First of all, I wrote it as a binary tree. First, I declare a node function element constructed by a node. In the element, I use all the int values, because in the HA fa full tree, we need the weights of each node, so we don't need to use the class template, but use int to solve this class directly. It is even more complicated in this category. Just declare and explain the functions you want. It is difficult for me to create selected and plotting functions in the constructor. Below # ifndef HFMTREE_H
# Define HFMTREE_H
Const int Max = 100;
# Include "BiTree. h"
# Include <iostream>
Using namespace std;
Struct element
{
Int weight;
Int lchild, rchild, parent;
};
Class HfmTree
{
Public:
HfmTree (int size );
Void Creat (int n );
Void SeLect (int k, int & s1, int & s2, int z );
Void reset (int size );
Void print (int size );
Void Makeary (int size );
Void printInl (int size, int I );
Void printInr (int size, int I );
Void Build (int size );
Private:
Int m1;
Int top = 0;
Element * root;
Int maxsize;
Int * pz;
Element * root3;
};

Void HfmTree: reset (int size)
{
Int m;
M = 2 * size-1;
Pz = new int [m];
Int I;
Pz [0] = root [m]. weight;
For (I = 1; I <m; I ++)
Pz [I] = 0;
 
}
Void HfmTree: Build (int size)
{
BiTree <int> zxh1 (pz, 2 * size-1, 0 );
Cout <zxh1;
}
Void HfmTree: printInl (int m, int I)
{
If (m =-1) return;
If (m! =-1)
{
// Pz [I-1] = root [m]. weight;
Pz [2 * I-1] = root [root [m]. lchild]. weight;
Pz [2 * I] = root [root [m]. rchild]. weight;
M = root [m]. lchild;
PrintInl (m, 2 * I );
M = root [m]. lchild;
}
}
Void HfmTree: printInr (int m, int I)
{
If (m =-1) return;
If (m! =-1)
{
// Pz [I-1] = root [m]. weight;
Pz [2 * I-1] = root [root [m]. lchild]. weight;
Pz [2 * I] = root [root [m]. rchild]. weight;
M = root [m]. rchild;
PrintInr (m, 2 * I + 1 );
M = root [m]. rchild;
}
}
Void HfmTree: Makeary (int size)
{
Int m;
M = 2 * size-1;
Pz = new int [m];
Int I;
For (I = 1; I <= m; I ++)
Pz [I] = 0;
I = 1;
While (root [m]. parent! = Size-1)
{
If (m =-1) break;
Pz [I-1] = root [m]. weight;
I = 2 * I;
M = root [m]. lchild;
}
M = 2 * size-1; I = 1;
While (root [m]. parent! = Size-1)
{
If (m =-1) break;
Pz [I-1] = root [m]. weight;
I = 2 * I + 1;
M = root [m]. rchild;
}
BiTree <int> zxh1 (pz, 2 * size + 1, 0 );
Cout <zxh1;
}
Void HfmTree: SeLect (int k, int & s1, int & s2, int z)
{
Int w1, w2, I = 1, j;
W1 = w2 = Max;
// S1 = s2 = I;
For (I = 1; I <= k; I ++)
{
// If (I = s1 | I = s2) continue;
If (root [I]. parent =-1)
{
If (root [I]. weight <w1)
{
W2 = w1;
W1 = root [I]. weight;
S2 = s1;
S1 = I; // w1, s1 is corresponding, and their weight is smaller
}
Else if (root [I]. weight <w2)
{
W2 = root [I]. weight;
S2 = I;
}
}
}
}
Void HfmTree: Creat (int n)
{
Int * w1;
Int I, m, * w, s1, s2; element * p;
If (n <1) throw "build error! ";
W = new int [2 * n-1];
W1 = new int [2 * n-1];
M = 2 * n-1; // There are 2 * n-1 nodes in the Process of n nodes
M1 = m;
Root = new element [m + 1];
For (I = 1; I <= 2 * n-1; I ++)
{
// Root [I]. weight = NULL;
Root [I]. lchild =-1;
Root [I]. rchild =-1;
Root [I]. parent =-1;
}
For (p = root + 1, I = 1; I <= n; I ++, p ++)
{
Cout <"Enter full time:" <endl;
Cin> w [I];
P-> weight = w [I];
P-> lchild =-1;
P-> rchild =-1;
P-> parent =-1;
}
For (I = n + 1; I <= m; I ++, p ++)
{
P-> weight = NULL;
P-> lchild =-1;
P-> rchild =-1;
P-> parent =-1;
}
For (I = n + 1; I <= m; I ++)
{
SeLect (I-1, s1, s2, m );
Root [s1]. parent = I-1;
Root [s2]. parent = I-1;
Root [I]. weight = root [s1]. weight + root [s2]. weight;
Root [I]. lchild = s1;
Root [I]. rchild = s2;
}
For (int I = 1; I <= n; I ++)
{
W1 [I-1] = w [n-I + 1];
}
}

HfmTree: HfmTree (int size)
{
Creat (size );
}
Void HfmTree: print (int size)
{
Int m, I;
M = 2 * size-1; // The total number of Tree nodes.
Cout <"The constructed Harman tree is" <endl;
Cout <"no" <"" <"weight" <"" <"Parent" <"<" lchild "<" rchild "<endl;
For (I = 1; I <= m; I ++ ){
Cout <I <"" <root [I]. weight <"" <root [I]. parent <"" <root [I]. lchild <"" <root [I]. rchild <endl;
}
}
# Endif
----------------------------------------------------------------------
# Include "HfmTree. h"
// # Include "BiTree. h"
# Include <iostream>
Using namespace std;
Void main ()
{
Int num;
Cout <"Enter the number of nodes (num) of the Harman tree you want to construct:"; cin> num;
HfmTree zxh (num );
Zxh. print (num );
// Zxh. Makeary (num );
Zxh. reset (num );
Zxh. printInl (2 * num-1, 1 );
Zxh. printInr (2 * num-1, 1 );
Zxh. Build (num );
} Mainly explained by code

Zookeeper

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.