title: C++ Implementation of Splay Tree
body: <p>So I was to implement Splay function after inserting a new tree. However, when I tried to insert multiple ints it says Segmentation Fault (core dump) then exit.</p>
  
  <p>Can anyone check where my problem is?</p>
  
  <pre><code>void SplayTree::splay(Node* node)
  {
      if (node == NULL)
  
          return;
     while (node!=NULL) {
              Node* parent = node-&gt;parent;
              if (parent != NULL) {
                  if (parent-&gt;left == node) {
                      rightRotate(parent);
                  } else {
                      leftRotate(parent);
                  }
              } else {
                  Node* gparent = parent-&gt;parent;
                  if (parent-&gt;left == node &amp;&amp; gparent-&gt;left == parent) {
                      rightRotate(gparent);
                      rightRotate(node-&gt;parent);
                  } else if (parent-&gt;right == node &amp;&amp;
                          gparent-&gt;right == parent) {
                      leftRotate(gparent);
                      leftRotate(node-&gt;parent);
                  } else if (parent-&gt;left == node &amp;&amp;
                          gparent-&gt;right == parent) {
                      rightRotate(parent);
                      leftRotate(node-&gt;parent);
                  } else {
                      leftRotate(parent);
                      rightRotate(node-&gt;parent);
                  }
              }
          }
  
  }
  </code></pre>