On 2016-11-28,
xero...@gmail.com <
xero...@gmail.com> wrote:
> For some reason the below program in not inserting correctly and I
> have tried now for days to figure out why. Seems I am missing
> something very simple.
> This is the insert function:
> //Takes const reference of type T and inserts it into tree, returns iterator to inserted Node
> iterator insert(const T& data){
> Node* p = root_;
> for (;;){
> if (p->data_ > data){
> if (p->rightThread)
> break;
> p = p->right_;
> }
> else if (p->data_ < data){
> if (p->leftThread)
> break;
> p = p->left_;
> }
> }
> Node* tmp = new Node();
> tmp->data_ = data;
> tmp->rightThread = tmp->leftThread = true;
> if (p->data_ < data){
> tmp->right_ = p->right_;
> tmp->left_ = p;
> p->right_ = tmp;
> p->rightThread = false;
> return iterator(p->right_);
> }
> else{
> tmp->right_ = p;
> tmp->left_ = p->left_;
> p->left_ = tmp;
> p->leftThread = false;
> return iterator(p->left_);
> }
> }
> This is the complete code:
> [snip]
> template <class T>
> class ThreadedTree{
> //Constructor that creates new Tree, takes no arguments and sets tree to new state
> ThreadedTree(){
> root_ = new Node();
> root_->right_ = root_->left_ = root_;
> root_->leftThread = true;
> }
In the constructor for ThreadedTree, some members of the root node
are not initialized, such as root_->data and root->righttThread,
but the values of those members are used in insert().