0

I'm not sure if I can violate treap's heap-ordering or it's binary search tree like structure with left and/or right rotation methods.

This is the code for left rotation

typename BinarySearchTree<K, T>::BSTTreeNode* rightSon = (*node).getRightSon();
        if (rightSon != nullptr)
        {
            typename BinarySearchTree<K,T>::BSTTreeNode* leftGreatSon = (*rightSon).getLeftSon();
            (*node).setRightSon(leftGreatSon);
            (*rightSon).setLeftSon(node);
        }

and right rotation

typename BinarySearchTree<K,T>::BSTTreeNode* leftSon = (*node).getleftSon();
        if (leftSon != nullptr)
        {
            typename BinarySearchTree<K,T>::BSTTreeNode* rightGreatSon = (*leftSon).getRightSon();
            (*leftSon).setRightSon(node);
            (*node).setLeftSon(parent);
        }

I'd expect these rotations to not violate the heap-ordering and the binary search tree like structure of the treap.

dodekja
  • 537
  • 11
  • 24
  • just curious; Why are you doing `(*node).getRightSon()` instead of `node->getRightSon()`? – Leonid Apr 24 '19 at 23:49
  • @Leonid the project is provided by school and Intellisense is not working in it for some reason. It's easier to type (*node).getRightSon() than node->getRightSon() – dodekja Apr 25 '19 at 11:52

1 Answers1

0

Heap-ordering will be destroyed by rotation, since given a root node (X0, Y0), which a child (X1, Y1), after the rotation, (X1, Y1) will be root. Since the Y-value of the root has to be greater than that of the child, we know that Y0 > Y1 initially. After the rotation, Y1 being root requires that Y1 > Y0, which is not true.

Binary search tree properties are not destroyed by rotation, though.

Leonid
  • 708
  • 5
  • 12