Skip to content

Fixed #1: Bug: remove() on a rootNode with one child #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions pyavltree.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ def remove_leaf (self, node):

def remove_branch (self, node):
parent = node.parent
leftChild = node.leftChild
rightChild = node.rightChild

if (parent):
if parent.leftChild == node:
parent.leftChild = node.rightChild or node.leftChild
Expand All @@ -386,10 +389,19 @@ def remove_branch (self, node):
del node
# rebalance
node = parent
while (node):
if not node.balance() in [-1, 0, 1]:
self.rebalance(node)
node = node.parent

if node:
while (node):
if not node.balance() in [-1, 0, 1]:
self.rebalance(node)
node = node.parent
else:
if leftChild:
self.rootNode = leftChild
else:
self.rootNode = rightChild

self.rootNode.parent = None

def swap_with_successor_and_remove (self, node):
successor = self.find_smallest(node.rightChild)
Expand Down