Deprecated: (6.006) preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /afs/athena.mit.edu/course/6/6.006/web_scripts/fall08/wiki/includes/Sanitizer.php on line 1470

Deprecated: (6.006) preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /afs/athena.mit.edu/course/6/6.006/web_scripts/fall08/wiki/includes/Sanitizer.php on line 1470
Recitation04 Notes - 6.006 Wiki

Recitation04 Notes

From 6.006 Wiki

Revision as of 19:41, 17 September 2008 by Mng 520 (Talk | contribs)
Jump to: navigation, search

BST / AVL implementation

    def left_rotate(self, x):
        y = x.right #Step 1
        y.parent = x.parent
        if y.parent is None:
            self.root = y
        else:
            if y.parent.left is x:
                y.parent.left = y
            elif y.parent.right is x:
                y.parent.right = y
        x.right = y.left #Step 2
        if x.right is not None:
            x.right.parent = x
        y.left = x #Step 3
        x.parent = y

    def right_rotate(self, x):
        y = x.left #Step 1
        y.parent = x.parent
        if y.parent is None:
            self.root = y
        else:
            if y.parent.left is x:
                y.parent.left = y
            elif y.parent.right is x:
                y.parent.right = y
        x.left = y.right #Step 2
        if x.left is not None:
            x.left.parent = x
        y.right = x #Step 3
        x.parent = y
Personal tools