Obrót drzewa w prawo i lewo w Pythonie

Aug 19 2020

Korzystam z klasy:

class Node:
    def __init__(self, value):
        self.key = value
        self.left = None
        self.right = None
        self.parent = None

i stworzyłem to drzewo:

n_12 = Node(12)
n_15 = Node(15)
n_3 = Node(3)
n_7 = Node(7)
n_1 = Node(1)
n_2 = Node(2)
n_not1 = Node(-1)

n_12.right = n_15
n_12.left = n_3
n_3.right = n_7
n_3.left = n_1
n_1.right = n_2
n_1.left = n_not1

n_12.parent = None
n_15.parent = n_12
n_3.parent = n_12
n_7.parent = n_3
n_1.parent = n_3
n_2.parent = n_1
n_not1.parent = n_1

Wypróbowałem ten kod:

def rightRotate(t): 
    if t == None or t.left == None:
        return None
    n = t
    l = t.left
    r = t.right
    lr = t.left.right
    ll = t.left.left
    t = t.left
    t.right = n
    if r != None:
        t.right.right = r
    if lr != None:
        t.right.left = lr
    if ll != None:
        t.left = ll

Ale to nie zadziałało, używając węzła głównego n_12usuwa niektóre węzły. Dlaczego to nie zadziałało i nie rozumiem, dlaczego nie mam wszystkich węzłów. Jeśli zadzwonię rightRotate(n_1), mam nieskończoną pętlę.

Odpowiedzi

trincot Aug 19 2020 at 18:59

Piszesz „Mam nieskończoną pętlę” , ale Twój kod nie ma pętli, więc musi się to dziać w innym miejscu w kodzie.

Widzę dwa problemy:

1) Cesja powinna być bezwarunkowa

if lr != None:
    t.right.left = lr

To zadanie jest również potrzebne, gdy lr is None. Jeśli nie, t.right.leftpozostaniesz równy temu, lktóry jest tw tym momencie, a więc rzeczywiście zostaniesz z pętlą w swoim drzewie.

2) Podwójne gwintowanie

Twoje drzewo jest dwuwątkowe, tzn. Zawiera również parentlinki. Ale te nie są aktualizowane w twojej rightRotatefunkcji. Zrezygnuj więc bez parentlinków (co jest preferowane) lub dostosuj kod tak, aby również parentlinki były aktualizowane zgodnie z rotacją.

Inna uwaga:

Następujący fragment kodu można uprościć:

if r != None:
    t.right.right = r   # was already equal to r
if lr != None:
    t.right.left = lr   # see above. should not be behind a condition
if ll != None:
    t.left = ll         # was already equal to ll

Można to więc sprowadzić do:

t.right.left = lr

lub nawet:

n.left = lr

Ostateczny kod

Dzięki powyższym zmianom Twoją funkcją może być:

class Node:
    def __init__(self, value):
        self.key = value
        self.left = None
        self.right = None
        self.parent = None

def rightRotate(node):
    if node is None or node.left is None:
        return node
    parent = node.parent
    left = node.left
    left_right = left.right

    # change edge 1
    if parent: # find out if node is a left or right child of node
        if parent.left == node:
            parent.left = left
        else:
            parent.right = left
    left.parent = parent

    # change edge 2
    left.right = node
    node.parent = left

    # change edge 3
    node.left = left_right
    if left_right:
        left_right.parent = node

    return left  # the node that took the position of node

# your code to build the tree
n_12 = Node(12)
n_15 = Node(15)
n_3 = Node(3)
n_7 = Node(7)
n_1 = Node(1)
n_2 = Node(2)
n_not1 = Node(-1)

n_12.right = n_15
n_12.left = n_3
n_3.right = n_7
n_3.left = n_1
n_1.right = n_2
n_1.left = n_not1

n_12.parent = None
n_15.parent = n_12
n_3.parent = n_12
n_7.parent = n_3
n_1.parent = n_3
n_2.parent = n_1
n_not1.parent = n_1

# rotate the root
root = n_12
root = rightRotate(root) # returns the node that took the place of n_12

Po prostu usuń linie za pomocą, parentaby uzyskać wersję jednowątkową.