使用 Level Order Traversal 将节点插入二叉树
Inserting a node into a Binary Tree using Level Order Traversal
我正在尝试编写一个函数,使用层序遍历将一个元素插入到二叉树中。我在代码中遇到的问题是,当我在将新节点插入树后打印层级顺序遍历时,它会在无限循环中打印元素。数字 1 2 3 4 5 6 7 8 不停地跑过航站楼。对于如何解决这种情况的任何指示和建议,我将不胜感激。
typedef struct BinaryTreeNode {
int data;
BinaryTreeNode * left;
BinaryTreeNode * right;
} BinaryTreeNode;
这是打印元素的层序遍历:
void LevelOrder(BinaryTreeNode *root) {
BinaryTreeNode *temp;
std::queue<BinaryTreeNode*> Q {};
if(!root) return;
Q.push(root);
while(!Q.empty()) {
temp = Q.front();
Q.pop();
//process current node
printf("%d ", temp -> data);
if(temp -> left) Q.push(temp -> left);
if(temp -> right) Q.push(temp -> right);
}
}
这是我通过修改层序遍历技术向树中插入一个元素的地方
void insertElementInBinaryTree(BinaryTreeNode *root, int element) {
BinaryTreeNode new_node = {element, NULL, NULL};
BinaryTreeNode *temp;
std::queue<BinaryTreeNode*> Q {};
if(!root) {
root = &new_node;
return;
}
Q.push(root);
while(!Q.empty()) {
temp = Q.front();
Q.pop();
//process current node
if(temp -> left) Q.push(temp -> left);
else {
temp -> left = &new_node;
Q.pop();
return;
}
if(temp -> right) Q.push(temp -> right);
else {
temp -> right = &new_node;
Q.pop();
return;
}
}
}
主要
int main() {
BinaryTreeNode one = {1, NULL, NULL}; // root of the binary tree
BinaryTreeNode two = {2, NULL, NULL};
BinaryTreeNode three = {3, NULL, NULL};
BinaryTreeNode four = {4, NULL, NULL};
BinaryTreeNode five = {5, NULL, NULL};
BinaryTreeNode six = {6, NULL, NULL};
BinaryTreeNode seven = {7, NULL, NULL};
one.left = &two;
one.right = &three;
two.left = &four;
two.right = &five;
three.left = &six;
three.right = &seven;
insertElementInBinaryTree(&one, 8);
LevelOrder(&one);
printf("\n");
return 0;
}
这条线
temp -> left = &new_node;
您正在使 temp->left
指向一个局部变量,该变量在函数 returns 之后将不再存在。任何访问它的尝试都是未定义的行为。
我正在尝试编写一个函数,使用层序遍历将一个元素插入到二叉树中。我在代码中遇到的问题是,当我在将新节点插入树后打印层级顺序遍历时,它会在无限循环中打印元素。数字 1 2 3 4 5 6 7 8 不停地跑过航站楼。对于如何解决这种情况的任何指示和建议,我将不胜感激。
typedef struct BinaryTreeNode {
int data;
BinaryTreeNode * left;
BinaryTreeNode * right;
} BinaryTreeNode;
这是打印元素的层序遍历:
void LevelOrder(BinaryTreeNode *root) {
BinaryTreeNode *temp;
std::queue<BinaryTreeNode*> Q {};
if(!root) return;
Q.push(root);
while(!Q.empty()) {
temp = Q.front();
Q.pop();
//process current node
printf("%d ", temp -> data);
if(temp -> left) Q.push(temp -> left);
if(temp -> right) Q.push(temp -> right);
}
}
这是我通过修改层序遍历技术向树中插入一个元素的地方
void insertElementInBinaryTree(BinaryTreeNode *root, int element) {
BinaryTreeNode new_node = {element, NULL, NULL};
BinaryTreeNode *temp;
std::queue<BinaryTreeNode*> Q {};
if(!root) {
root = &new_node;
return;
}
Q.push(root);
while(!Q.empty()) {
temp = Q.front();
Q.pop();
//process current node
if(temp -> left) Q.push(temp -> left);
else {
temp -> left = &new_node;
Q.pop();
return;
}
if(temp -> right) Q.push(temp -> right);
else {
temp -> right = &new_node;
Q.pop();
return;
}
}
}
主要
int main() {
BinaryTreeNode one = {1, NULL, NULL}; // root of the binary tree
BinaryTreeNode two = {2, NULL, NULL};
BinaryTreeNode three = {3, NULL, NULL};
BinaryTreeNode four = {4, NULL, NULL};
BinaryTreeNode five = {5, NULL, NULL};
BinaryTreeNode six = {6, NULL, NULL};
BinaryTreeNode seven = {7, NULL, NULL};
one.left = &two;
one.right = &three;
two.left = &four;
two.right = &five;
three.left = &six;
three.right = &seven;
insertElementInBinaryTree(&one, 8);
LevelOrder(&one);
printf("\n");
return 0;
}
这条线
temp -> left = &new_node;
您正在使 temp->left
指向一个局部变量,该变量在函数 returns 之后将不再存在。任何访问它的尝试都是未定义的行为。