[英]Binary Search Trees C++
I was trying to implement a simple Binary Search Tree for practice. 我正在尝试实现一个简单的二进制搜索树进行练习。 I tried to just add values and print the values in the nodes.
我试图只添加值并在节点中打印值。 However, I am not getting the proper ascending order of values in the nodes.
但是,我没有在节点中获得正确的值升序。 Here is what I have:
这是我所拥有的:
struct Node
{
int data;
Node* leftN;
Node* rightN;
};
typedef Node* Node_ptr;
Node_ptr head;
//INSERT_VALUE FUNCTION
Node* insert_value(Node_ptr leaf, int key)
{
//Root case when there is no set value yet
if(leaf == NULL)
{
leaf = new Node;
head = leaf;
cout << "Make the first node" << endl;
leaf->data = key;
leaf->leftN = NULL;
leaf->rightN = NULL;
return leaf;
}
//Left child Node
if(key < leaf->data)
{
//Search for a spot in the tree to add a Node (left value < root value < right value)
//This is only true for the left child Node
if(leaf->leftN != NULL)
insert_value(leaf, key);
//We have found a spot in the tree to add a new Node and add the value of key
else
{
cout << "Insert-left" << endl;
leaf->leftN = new Node;
leaf = leaf->leftN;
leaf->data = key;
leaf->leftN = NULL;
leaf->rightN = NULL;
return leaf;
}
}
//Right child Node
else if(key >= leaf->data)
{
//Search for a spot to add a new Node in the tree (only amongst the right child Nodes)
if(leaf->rightN != NULL)
insert_value(leaf, key);
//Once we have found a spot to add a new Node, append the new Node
else
{
cout << "Insert-right" << endl;
leaf->rightN = new Node;
leaf = leaf->rightN;
leaf->data = key;
leaf->leftN = NULL;
leaf->rightN = NULL;
return leaf;
}
}
}
//PRINT FUNCTION
void printTree(Node_ptr leaf)
{
if(leaf == NULL)
return;
printTree(leaf->leftN);
cout << "Data element: " << leaf->data << endl;
printTree(leaf->rightN);
}
//MAIN
int main()
{
Node_ptr root = NULL;
int i;
//initialize values
for(i = 1; i < 12; i+=2)
root = insert_value(root, i);
root = head;
for(i = 0; i < 11; i+=2)
root = insert_value(root, i);
root = head;
printTree(root);
root = head;
cout << "Head Node: " << root->data << endl;
return 0;
}
When I printed the results, this is what I got: 0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 11 and the value of the head node is 1 当我打印结果时,我得到的是:0、2、4、6、8、10、1、3、5、7、9、11,头节点的值为1
Because you are calling the insertion as: 因为您将插入称为:
root = insert_value(root, i);
the location at which you insert is always using a subtree starting at the last insertion. 您插入的位置始终使用从最后一次插入开始的子树。 Except the time that you re-start to add the odd numbers, when you start inserting at the head.
当您开始插入开头时,除了重新开始添加奇数的时间。
If you create a class BinarySearchTree
that contains a head pointer, and an insert method taking an int value
that calls Node::insert( head, value )
, then you can just call insert on that class, without passing it a node, and it can always see to it that the insertions use the root of the tree for the start of the recursion. 如果创建一个包含头指针的
class BinarySearchTree
和一个采用int value
的插入方法来调用Node::insert( head, value )
,则可以在该类上调用insert,而无需将其传递给节点,并且可以始终看到插入使用树的根作为递归的开始。
Just me, but I would have a constructor for Node that takes an int
and initializes the pointers to NULL. 仅我一个人,但我有一个Node的构造函数,该构造函数采用
int
并将指针初始化为NULL。 That way you don't have to do that in the insert method. 这样,您不必在insert方法中执行此操作。
In the leaf->node? 在叶子->节点? != NULL case, I think instead of calling
!= NULL的情况下,我认为不是调用
insert_value(leaf, key);
you want to say 你想说
leaf->node? = insert_value(leaf->node?, key)
where ? 在哪 is either L or R, of course.
当然是L或R。
Something you might consider is adding a comment to the method like so: 您可能会考虑在方法中添加注释,如下所示:
// Adds the given key to the (sub-)tree rooted at node* then returns the new root
// of that (sub-)tree.
node *insert_value_and_return_root(node *root, int value) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.