Inserting into Binary search trees
The program that i have to create asks us to insert words from a file.txt into the tree and to keep track of the line where that word is. for example:
1 2 3
|
apple
bannana pie
beans pie
|
apple is on line1
pie is on line 2,3 |
i can create a binary tree that inserts the words but i am unsure how to make it insert the word and the line number on the same leaf/branch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void BinarySearchTree::insert(string d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
|
bump
Use a map.
what do you mean?
Topic archived. No new replies allowed.