Inserting into Threaded Binary Tree in C++
<p>I'm having trouble inserting some values into a threaded binary tree. In my main function, if I try inserting these values
    a.Insert(10);
    a.Insert(27);
    a.Insert(20);
    a.Insert(20);
    a.Insert(5);
    a.Insert(18);
    a.Insert(4);
    a.Insert(19);</p>

<p>Everything works fine, but if I switch the 27 and the 20, I get a segmentation error. </p>

<p>Here's my code and the working output.</p>

<pre><code>    struct bstNode{  
    int data;
    bool lthread;
    bool rthread;
    bstNode *left;
    bstNode *right;
    bstNode(int value){
        lthread = false;
        rthread = false;
        data = value;
        left = NULL;
        right = NULL;
    };
    int GetData() {return data;}
    void SetLeft(bstNode *l){ left = l;}
    bstNode *GetLeft() {return left;}
    bstNode *GetRight() {return right;}
    void SetRight(bstNode *r){ right = r;}
    void SetLeftThread(bool value){lthread = value;}
    void SetRightThread(bool value){rthread = value;}
    bool GetLeftThread() {return lthread;}
    bool GetRightThread() {return rthread;}
    bool IsLeft(){
        if(left == NULL)
            return false;
        else
            return true;
    }
    bool IsRight(){
        if(right == NULL)
            return false;

        return true;
    }
};


    class BinarySearchTree{
    public:
    BinarySearchTree(){root = NULL;};
    void Insert(int);
    void Display();
    bstNode* search(int key);
    bstNode* root;
    bstNode* current;
};



        void BinarySearchTree::Insert(int value){
    bstNode *node = new bstNode(value);
    if (root == NULL){
        root = node;
        return;
    }

    bstNode *ptr = root, *parent = NULL;

    while (ptr !=NULL){
        if(value == ptr-&gt;GetData()){
            cout &lt;&lt; "Attempted to insert duplicate value: " &lt;&lt; value &lt;&lt;" -- Ignored." &lt;&lt; endl;
            delete node;
            return;
        }
        parent = ptr;

        if(value &lt; ptr-&gt;GetData()){
            if(ptr-&gt;GetLeftThread())
                break;
            else
                ptr = ptr-&gt;GetLeft();}
        else{
            if(ptr-&gt;GetRightThread())
                break;
            else{
                ptr = ptr-&gt;GetRight();}

        }
    }

    if (ptr == NULL)
    {
        if(value &lt; parent-&gt;GetData())
        {
            parent-&gt;SetLeft(node);
            node-&gt;SetRight(parent);
            node-&gt;SetRightThread(true);

        }
        else
        {
            parent-&gt;SetRight(node);
            node-&gt;SetLeft(parent);
            node-&gt;SetLeftThread(true);
        }
    }
    else
    {
        if(value &lt; ptr-&gt;GetData())
        {
            node-&gt;SetLeft(ptr-&gt;GetLeft());
            node-&gt;SetLeftThread(true);
            node-&gt;SetRight(ptr);
            node-&gt;SetRightThread(true);
            ptr-&gt;SetLeft(node);
            ptr-&gt;SetLeftThread(false);
        }

        else
        {
            node-&gt;SetRight(ptr-&gt;GetRight());
            node-&gt;SetRightThread(true);
            node-&gt;SetLeft(ptr);
            node-&gt;SetLeftThread(true);
            ptr-&gt;SetRight(node);
            ptr-&gt;SetRightThread(false);

        }
    }
return;
}

void BinarySearchTree::Display(){
    if(root == NULL){
        cout &lt;&lt; "Empty" &lt;&lt; endl;
        return;
    }
    bstNode *p, *q;

    p = root;
    do
    {
        while(p != NULL){
            q = p;
            if(p-&gt;GetLeftThread())
                break;
            else
                p = p-&gt;GetLeft();
        }
        cout &lt;&lt; q-&gt;data &lt;&lt; "'s right thread is "&lt;&lt; q-&gt;right-&gt;data &lt;&lt; "\n";

        p = q-&gt;GetRight();

        while(q-&gt;GetRightThread()){
            cout &lt;&lt; p-&gt;data &lt;&lt; "'s left thread is " &lt;&lt; p-&gt;left-&gt;data &lt;&lt; "\n";
            q = p;
            p = q-&gt;GetRight();
        }

    } while(p != NULL);
}


bstNode* BinarySearchTree::search(int value){
    bstNode* current = root;
    while (current) {
        if(current-&gt;data == value){
            cout &lt;&lt; current-&gt;data &lt;&lt; " does exist!" &lt;&lt; endl;
            return current;
        }

        else if (value &lt; current-&gt;data){
            current = current-&gt;left;

        } 

        else current = current-&gt;right;

    }
    cout &lt;&lt; "No "&lt;&lt; value &lt;&lt; endl;
    return NULL;
}



main(){
    BinarySearchTree a;
    a.Insert(10);
    a.Insert(20);
    a.Insert(27);
    a.Insert(20);
    a.Insert(5);
    a.Insert(18);
    a.Insert(4);
    a.Insert(19);
    a.Display();
    a.search(19);

    cout &lt;&lt; "\n" &lt;&lt;endl;


    return 0;
}
</code></pre>

<p>And output:</p>

<pre><code>Attempted to insert duplicate value: 20 -- Ignored.
4's right thread is 5
5's left thread is 4
10's left thread is 5
18's right thread is 19
19's right thread is 20
20's left thread is 18
27's left thread is 20
19 does exist!


[Finished in 0.3s]
</code></pre>

<p>Nonfunctioning output:</p>

<pre><code>    Attempted to insert duplicate value: 20 -- Ignored.
bash: line 1: 26586 Segmentation fault: 11  '/Users/Gnedelka/Desktop/Assignment 6/new/selfcopy'
[Finished in 0.5s with exit code 139]
</code></pre>
