Skip to content

Commit

Permalink
Add delete method for AVL tree
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Jun 20, 2023
1 parent d376d81 commit 021f57a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Please ask questions in [Issues](https://github.com/msambol/youtube/issues).
* [CS Tutorials](https://www.youtube.com/playlist?list=PL9xmBV_5YoZPKwb4XPB1sG7S6kNpN9JJo) – all of my computer science tutorials in one playlist
* [Big-O Notation](https://www.youtube.com/playlist?list=PL9xmBV_5YoZMxejjIyFHWa-4nKg6sdoIv)
* [Data Structures](https://www.youtube.com/playlist?list=PL9xmBV_5YoZO2D89q42-y8voxIJKpB4oR)
* [Maximum Flow Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZPxifnyXjQVU1bhU4b4_Ts2)
* [Minimum Spanning Trees](https://www.youtube.com/playlist?list=PL9xmBV_5YoZObEi3Hf6lmyW-CBfs7nkOV)
* [Search Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZMIAJn8M6At9CjZ0Wu0B31d)
* [Shortest Path Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZO-Y-H3xIC9DGSfVYJng9Yw)
* [Maximum Flow Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZPxifnyXjQVU1bhU4b4_Ts2)
* [Search Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZMIAJn8M6At9CjZ0Wu0B31d)
* [Sort Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZOZSbGAXAPIq1BeUf4j20pl)
* [Tree Traversal Algos](https://www.youtube.com/playlist?list=PL9xmBV_5YoZO1JC2RgEi04nLy6D-rKk6b)
* [Red-Black Trees](https://www.youtube.com/playlist?list=PL9xmBV_5YoZNqDI8qfOZgzbqahCUmUEin)
Expand Down
20 changes: 20 additions & 0 deletions trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* [Playlist]()
* Videos: [Intro & Search]() | [Insertions]() | [Deletions]()
* h/t **Shivali Bhadaniya**[Article](https://favtutor.com/blogs/avl-tree-python) | [LinkedIn](https://www.linkedin.com/in/shivali-bhadaniya-76932a192/)
* h/t **Programiz**[AVL Tree](https://www.programiz.com/dsa/avl-tree)

```
❯ python avl_tree.py
Expand All @@ -77,6 +78,25 @@ Level-order traversal with height and balance factor:
Search for 125: 125
Search for 1: not found
After deleting 120:
Level-order traversal:
50 25 75 15 35 60 90 10 68 83 125 100
Level-order traversal with height and balance factor:
50: h = 5, bf = -1
25: h = 3, bf = 1
75: h = 4, bf = -1
15: h = 2, bf = 1
35: h = 1, bf = 0
60: h = 2, bf = -1
90: h = 3, bf = -1
10: h = 1, bf = 0
68: h = 1, bf = 0
83: h = 1, bf = 0
125: h = 2, bf = 1
100: h = 1, bf = 0
```

### B-trees
Expand Down
56 changes: 52 additions & 4 deletions trees/avl_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def get_height(self, node):
def get_balance_factor(self, node):
return 0 if not node else (self.get_height(node.left) - self.get_height(node.right))

def get_min_node(self, node):
return node if not node or not node.left else self.get_min_node(node.left)

# O(logn)
def search(self, key):
x = self.root
Expand Down Expand Up @@ -60,8 +63,51 @@ def insert(self, root, key):

return root

def delete(self, key):
pass
# O(logn)
def delete(self, root, key):
if not root:
return root
elif key < root.key:
root.left = self.delete(root.left, key)
elif key > root.key:
root.right = self.delete(root.right, key)
else:
if not root.left:
temp = root.right
root = None
return temp
elif not root.right:
temp = root.left
root = None
return temp
# find inorder successor
temp = self.get_min_node(root.right)
root.key = temp.key
root.right = self.delete(root.right, temp.key)

if root is None:
return root

root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))

# Update the balance factor and balance the tree
bf = self.get_balance_factor(root)

if bf > 1 and self.get_balance_factor(root.left) >= 0:
return self.right_rotate(root)

if bf < -1 and self.get_balance_factor(root.right) <= 0:
return self.left_rotate(root)

if bf > 1 and self.get_balance_factor(root.left) < 0:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)

if bf < -1 and self.get_balance_factor(root.right) > 0:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)

return root

# O(1)
def left_rotate(self, node):
Expand Down Expand Up @@ -134,7 +180,9 @@ def main():

result = avl.search(1)
print(f'Search for 1: {print_search_result(result)}')

return avl

avl.root = avl.delete(avl.root, 120)
print('\nAfter deleting 120:')
avl.print_tree()

main()

0 comments on commit 021f57a

Please sign in to comment.