无法将矩阵的值添加到节点三的 child
Not able to add value of the matrix to the child of the node three
我有一个矩阵,里面有随机值。值的范围从 1
到 Width-1
。我的决定是使用每个 parent 有 4 children 的树(up
、right
、down
、left
)。 right
函数检查矩阵单元格的值是否小于 Width+Value
,如果是,那么应该可以将单元格的值保存到持有节点 iş 的右侧 child ,然后递归检查下一个值。在递归过程的最后,return到down
、left
和up
的功能与up
的工作方式相同。最后的目的地是b[HEIGHT-1][WIDTH-1]
点。例如:
2 1 2
1 2 1
1 1 2
这个矩阵应该是一棵树:
我使用的代码已经编辑并取自二叉树。
struct node
{
int key_value;
node *left;
node *right;
node *down;
node *up;
};
class btree
{
public:
btree();
~btree();
void insert(int key); //edit
node *search(int key);
void destroy_tree();
void destroy_tree(node *leaf);
void insert(int key, node *leaf, int position);
node *root;
};
btree::btree()
{
root=NULL;
}
btree::~btree()
{
destroy_tree();
}
void btree::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
destroy_tree(leaf->up);
destroy_tree(leaf->down);
delete leaf;
}
}
void btree::insert(int key)//set root (start point b[0][0])
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
root->up=NULL;
root->down=NULL;
cout<<"root value"<<root->key_value;
}
void btree::insert(int key, node *leaf, int position) //up 1,right 2, down 3, left 4
{
if(position==1)
{
if(leaf->up!=NULL)
insert(key, leaf->up,1);
else
{
leaf->up=new node;
leaf->up->key_value=key;
leaf->up->up=NULL; //Sets the up child of the child node to null
leaf->up->down=NULL; //Sets the down child of the child node to null
leaf->up->left=NULL; //Sets the left child of the child node to null
leaf->up->right=NULL; //Sets the right child of the child node to null
}
}
else if(position==2)
{
if(leaf->right!=NULL)
insert(key, leaf->right,2);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->up=NULL; //Sets the up child of the child node to null
leaf->right->down=NULL; //Sets the down child of the child node to null
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
else if(position==3)
{
if(leaf->down!=NULL)
insert(key, leaf->down,3);
else
{
leaf->down=new node;
leaf->down->key_value=key;
leaf->down->up=NULL; //Sets the up child of the child node to null
leaf->down->down=NULL; //Sets the down child of the child node to null
leaf->down->left=NULL; //Sets the left child of the child node to null
leaf->down->right=NULL; //Sets the right child of the child node to null
}
}
else if(position==4)
{
if(leaf->left!=NULL)
insert(key, leaf->left,4);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->up=NULL; //Sets the up child of the child node to null
leaf->left->down=NULL; //Sets the down child of the child node to null
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else{
cout<<"ERROR insert node"<<endl;
}
}
void btree::destroy_tree()
{
destroy_tree(root);
}
signed short int check;
void up(int b[][WIDTH], int x, int y){
check=y-b[x][y];// check if there is space to go up
if (check<0){ // if yes
// go to right() function
}
else{ // if there is place recurse and save the pass to the up leaf
}
}
void down(int b[][WIDTH], int x, int y){}
void right(int b[][WIDTH], int x, int y){
check=y+b[x][y];// check if there is space to go right
if (check>WIDTH){ // if no go to down function
down(b,x,y);
}
else{ // if there is place recurse and save the pass to the up leaf
btree::insert(b[x][check], leaf->left, 2); // save the value of the node b[x][check] into the right child
right(b,x,y+check);
cout<<"value of the node:"<<endl;
}
}
void left(int b[][WIDTH], int x, int y);
int main(){
int b[HEIGHT][WIDTH], row, col;
for (row = 0; row <=HEIGHT-1; row++)
for (col = 0; col <=WIDTH-1; col++)
cin >> b[row][col];
btree start;
start.insert(b[0][0]);
return 0;
}
我收到错误 leaf was leaf was not declared
但 leaf->right 发送根的右叶(在开始时),最近 children 发送。
这是解决方案:
btree up;
up.insert(b[x][check], leaf->up, 2, x, y);
创建对象。
我有一个矩阵,里面有随机值。值的范围从 1
到 Width-1
。我的决定是使用每个 parent 有 4 children 的树(up
、right
、down
、left
)。 right
函数检查矩阵单元格的值是否小于 Width+Value
,如果是,那么应该可以将单元格的值保存到持有节点 iş 的右侧 child ,然后递归检查下一个值。在递归过程的最后,return到down
、left
和up
的功能与up
的工作方式相同。最后的目的地是b[HEIGHT-1][WIDTH-1]
点。例如:
2 1 2
1 2 1
1 1 2
这个矩阵应该是一棵树:
我使用的代码已经编辑并取自二叉树。
struct node
{
int key_value;
node *left;
node *right;
node *down;
node *up;
};
class btree
{
public:
btree();
~btree();
void insert(int key); //edit
node *search(int key);
void destroy_tree();
void destroy_tree(node *leaf);
void insert(int key, node *leaf, int position);
node *root;
};
btree::btree()
{
root=NULL;
}
btree::~btree()
{
destroy_tree();
}
void btree::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
destroy_tree(leaf->up);
destroy_tree(leaf->down);
delete leaf;
}
}
void btree::insert(int key)//set root (start point b[0][0])
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
root->up=NULL;
root->down=NULL;
cout<<"root value"<<root->key_value;
}
void btree::insert(int key, node *leaf, int position) //up 1,right 2, down 3, left 4
{
if(position==1)
{
if(leaf->up!=NULL)
insert(key, leaf->up,1);
else
{
leaf->up=new node;
leaf->up->key_value=key;
leaf->up->up=NULL; //Sets the up child of the child node to null
leaf->up->down=NULL; //Sets the down child of the child node to null
leaf->up->left=NULL; //Sets the left child of the child node to null
leaf->up->right=NULL; //Sets the right child of the child node to null
}
}
else if(position==2)
{
if(leaf->right!=NULL)
insert(key, leaf->right,2);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->up=NULL; //Sets the up child of the child node to null
leaf->right->down=NULL; //Sets the down child of the child node to null
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
else if(position==3)
{
if(leaf->down!=NULL)
insert(key, leaf->down,3);
else
{
leaf->down=new node;
leaf->down->key_value=key;
leaf->down->up=NULL; //Sets the up child of the child node to null
leaf->down->down=NULL; //Sets the down child of the child node to null
leaf->down->left=NULL; //Sets the left child of the child node to null
leaf->down->right=NULL; //Sets the right child of the child node to null
}
}
else if(position==4)
{
if(leaf->left!=NULL)
insert(key, leaf->left,4);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->up=NULL; //Sets the up child of the child node to null
leaf->left->down=NULL; //Sets the down child of the child node to null
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else{
cout<<"ERROR insert node"<<endl;
}
}
void btree::destroy_tree()
{
destroy_tree(root);
}
signed short int check;
void up(int b[][WIDTH], int x, int y){
check=y-b[x][y];// check if there is space to go up
if (check<0){ // if yes
// go to right() function
}
else{ // if there is place recurse and save the pass to the up leaf
}
}
void down(int b[][WIDTH], int x, int y){}
void right(int b[][WIDTH], int x, int y){
check=y+b[x][y];// check if there is space to go right
if (check>WIDTH){ // if no go to down function
down(b,x,y);
}
else{ // if there is place recurse and save the pass to the up leaf
btree::insert(b[x][check], leaf->left, 2); // save the value of the node b[x][check] into the right child
right(b,x,y+check);
cout<<"value of the node:"<<endl;
}
}
void left(int b[][WIDTH], int x, int y);
int main(){
int b[HEIGHT][WIDTH], row, col;
for (row = 0; row <=HEIGHT-1; row++)
for (col = 0; col <=WIDTH-1; col++)
cin >> b[row][col];
btree start;
start.insert(b[0][0]);
return 0;
}
我收到错误 leaf was leaf was not declared
但 leaf->right 发送根的右叶(在开始时),最近 children 发送。
这是解决方案:
btree up;
up.insert(b[x][check], leaf->up, 2, x, y);
创建对象。