Home > Back-end >  Construct a height (depth) as the 7 of 10 forks tree, with the algorithm of how ah?
Construct a height (depth) as the 7 of 10 forks tree, with the algorithm of how ah?

Time:10-04

I considered using a recursive way, but found no way to limit height is 7 ah, built-in flag ji number or something, use cycle calls to build and obviously too troublesome, a rookie, I hope god can help me with, convenient to the key part of the code, right ~

CodePudding user response:

I also entanglements, electronic science and technology university

CodePudding user response:

Alas, it seems everyone is done very upset ah..

CodePudding user response:

Attach to temporarily code.. But later found in the data file didn't write ah, don't know where is wrong..
 # include & lt; iostream> 
#include
#include
#include
using namespace std;
Int the depth=1;
Struct TreeNode
{
Char the depth;
TreeNode * parent;
TreeNode * child0;
TreeNode * child1;
TreeNode * child2;
TreeNode * child3;
TreeNode * child4;
TreeNode * child5;
TreeNode * child6;
TreeNode * child7;
TreeNode * child8;
TreeNode * child9;
Char rank;
};
Invoke the TreeNode * (TreeNode * & amp; The node, int depthtemp, int flag)
{TreeNode * p=new TreeNode.
P - & gt; The depth=depthtemp;
Node - & gt; The parent=node - & gt; Child0=node - & gt; Child1=node - & gt; Child2=node - & gt; Child3=NULL;
Node - & gt; Child4=node - & gt; Child5=node - & gt; Child6=node - & gt; Child7=node - & gt; Child8=node - & gt; Child9=NULL;
The switch (flag)
{
Case 0:
Node - & gt; Child0=p;
Node - & gt; Rank=0;
break;
Case 1:
Node - & gt; Child1=p;
Node - & gt; Rank=1;
break;
Case 2:
Node - & gt; Child2=p;
Node - & gt; Rank=2;
break;
Case 3:
Node - & gt; Child3=p;
Node - & gt; Rank=3;
break;
Case 4:
Node - & gt; Child4=p;
Node - & gt; Rank=4;
break;
Case 5:
Node - & gt; Child5=p;
Node - & gt; Rank=5;
break;
Case 6:
Node - & gt; Child6=p;
Node - & gt; Rank=6;
break;
Case 7:
Node - & gt; Child7=p;
Node - & gt; Rank=7;
break;
Case 8:
Node - & gt; Child8=p;
Node - & gt; Rank=8;
break;
Case 9:
Node - & gt; Child9=p;
Node - & gt; Rank=9;
break;
}
Return the node;
}
Ofstream& Writetreetofile (ofstream& Fout, TreeNode * node)//the city 岃 繘 e ? ? ? stamping rev 鏂 囦 huan Char ch;
int data;
If (node==NULL)
{
Ch=0;//0 XFF light hon 妭 flow gauge 爣 Jian? Fout. Write (& amp; Ch, sizeof (char));
}
The else
{
Ch=1;//0 x01 Wei 炵 ┖ 鑺 傜 偣 self-contained cannonballs woven
Fout. Write (& amp; Ch, sizeof (char));
Data=https://bbs.csdn.net/topics/node-> the depth;
Fout. Write ((char *) & amp; The data, sizeof (char));
Writetreetofile (fout, node - & gt; Child0);
Writetreetofile (fout, node - & gt; Child1);
Writetreetofile (fout, node - & gt; Child2);
Writetreetofile (fout, node - & gt; Child3);
Writetreetofile (fout, node - & gt; Child4);
Writetreetofile (fout, node - & gt; Child5);
Writetreetofile (fout, node - & gt; Child6);
Writetreetofile (fout, node - & gt; Child7);
Writetreetofile (fout, node - & gt; Child8);
Writetreetofile (fout, node - & gt; Child9);
}
Return fout.
}
Void addroot (TreeNode * & amp; Node, int depthtemp)//鎻 掑 Ai self-contained silicon 妭 chains decyl Ren 傛 speech &drama 灉 Chen chi 粡 ying birch 湪 froe file Wu 鎺 ヨ 繑 cheese? {
If (node==NULL)
{
The node=new TreeNode.
Node - & gt; The depth=depthtemp;
Node - & gt; Rank=0;
Node - & gt; The parent=node - & gt; Child0=node - & gt; Child1=node - & gt; Child2=node - & gt; Child3=NULL;
Node - & gt; Child4=node - & gt; Child5=node - & gt; Child6=node - & gt; Child7=node - & gt; Child8=node - & gt; Child9=NULL;
}
}
Void erasetree (TreeNode * & amp; The node)//e ? ? ? 犻 {
If (node==NULL)
return;
Erasetree (node - & gt; Child0);
Erasetree (node - & gt; Child1);
Erasetree (node - & gt; Child2);
Erasetree (node - & gt; Child3);
Erasetree (node - & gt; Child4);
Erasetree (node - & gt; Child5);
Erasetree (node - & gt; Child6);
Erasetree (node - & gt; Child7);
Erasetree (node - & gt; Child8);
Erasetree (node - & gt; Child9);
TreeNode * p=node - & gt; The parent;
If (p!=NULL)
{switch (node - & gt; Rank)
{case 0:
P - & gt; Child0=NULL;
break;
Case 1:
P - & gt; Child1=NULL;
break;
Case 2:
P - & gt; Child2=NULL;
break;
Case 3:
P - & gt; Child3=NULL;
break;
Case 4:
P - & gt; Child4=NULL;
break;
Case 5:
P - & gt; Child5=NULL;
break;
Case 6:
P - & gt; Child6=NULL;
break;
Case 7:
P - & gt; Child7=NULL;
break;
Case 8:
P - & gt; Child8=NULL;
break;
Case 9:
P - & gt; Child9=NULL;
break; }
}
Delete the node;
The node=NULL;
}
Void createtree (TreeNode * & amp; The node)//e ? ? ? Si {
The depth +=1;
If (the depth==8)
{the depth -=1;
return;
}
TreeNode * p0, p1, p2, p3, p4, p5, p6, * p7 p8, * p9;
P0=invoke the (node, the depth, 0);
P1=invoke the (node, the depth, 1);
P2=invoke the (node, the depth, 2);
P3=invoke the (node, the depth, 3);
P4=invoke the (node, the depth, 4);
P5=invoke the (node, the depth, 5);
P6=invoke the node, the depth, (6);
P7=invoke the (node, the depth, 7);
P8=invoke the (node, the depth, 8).
P9=invoke the (node, the depth, 9);
Createtree (p0);
Createtree (p1);
Createtree (p2);
Createtree (p3);
Createtree (p4);
Createtree (p5);
Createtree (p6);
Createtree (p7);
Createtree (p8);
Createtree (p9);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related