algorithms(avl): refactor code

Signed-off-by: Matej Focko <me@mfocko.xyz>

diff --git a/static/files/algorithms/rb-trees/avl/src/AVL.cs b/static/files/algorithms/rb-trees/avl/src/AVL.cs
index 757d8d4..9b3e35e 100644
--- a/static/files/algorithms/rb-trees/avl/src/AVL.cs
+++ b/static/files/algorithms/rb-trees/avl/src/AVL.cs
@@ -12,32 +12,29 @@ class AVL<T>(IComparer<T> comparator) : IEnumerable<T> {

     #region AVLSpecific

-    private static (bool correct, int depth) Check(Node<T>? node, int depth) {
+    private static (bool Correct, int Depth) Check(Node<T>? node, int depth) {
         if (node == null) {
             return (true, depth);
         }

-        var (leftCorrect, leftDepth) = Check(node.Left, 1 + depth);
-        if (!leftCorrect) {
-            return (false, leftDepth);
+        var left = Check(node.Left, 1 + depth);
+        if (!left.Correct) {
+            return (false, left.Depth);
         }

-        var (rightCorrect, rightDepth) = Check(node.Right, 1 + depth);
+        var right = Check(node.Right, 1 + depth);

-        var foundDepth = Math.Max(leftDepth, rightDepth);
-        return (rightCorrect && Math.Abs(leftDepth - rightDepth) <= 1, foundDepth);
+        var foundDepth = Math.Max(left.Depth, right.Depth);
+        return (right.Correct && Math.Abs(left.Depth - right.Depth) <= 1, foundDepth);
     }

-    public bool IsCorrect() {
-        var (correct, _) = Check(_root, 0);
-        return correct;
-    }
+    public bool IsCorrect() => Check(_root, 0).Correct;

-    private void InsertRebalance(List<Node<T>> nodes) {
+    private void InsertRebalance(List<Node<T>> nodes, T item) {
         // TODO
     }

-    private void DeleteRebalance(List<Node<T>> nodes) {
+    private void DeleteRebalance(List<Node<T>> nodes, T item) {
         // TODO
     }

@@ -79,10 +76,9 @@ class AVL<T>(IComparer<T> comparator) : IEnumerable<T> {
         } else {
             path[lastIdx].Left = newItem;
         }
-        path.Add(newItem);

         // rebalance
-        InsertRebalance(path);
+        InsertRebalance(path, item);
         return true;
     }
This commit is contained in:
Matej Focko 2024-02-04 13:48:10 +01:00
parent aa6f33feb5
commit 957962665d
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -12,32 +12,29 @@ class AVL<T>(IComparer<T> comparator) : IEnumerable<T> {
#region AVLSpecific #region AVLSpecific
private static (bool correct, int depth) Check(Node<T>? node, int depth) { private static (bool Correct, int Depth) Check(Node<T>? node, int depth) {
if (node == null) { if (node == null) {
return (true, depth); return (true, depth);
} }
var (leftCorrect, leftDepth) = Check(node.Left, 1 + depth); var left = Check(node.Left, 1 + depth);
if (!leftCorrect) { if (!left.Correct) {
return (false, leftDepth); return (false, left.Depth);
} }
var (rightCorrect, rightDepth) = Check(node.Right, 1 + depth); var right = Check(node.Right, 1 + depth);
var foundDepth = Math.Max(leftDepth, rightDepth); var foundDepth = Math.Max(left.Depth, right.Depth);
return (rightCorrect && Math.Abs(leftDepth - rightDepth) <= 1, foundDepth); return (right.Correct && Math.Abs(left.Depth - right.Depth) <= 1, foundDepth);
} }
public bool IsCorrect() { public bool IsCorrect() => Check(_root, 0).Correct;
var (correct, _) = Check(_root, 0);
return correct;
}
private void InsertRebalance(List<Node<T>> nodes) { private void InsertRebalance(List<Node<T>> nodes, T item) {
// TODO // TODO
} }
private void DeleteRebalance(List<Node<T>> nodes) { private void DeleteRebalance(List<Node<T>> nodes, T item) {
// TODO // TODO
} }
@ -79,10 +76,9 @@ class AVL<T>(IComparer<T> comparator) : IEnumerable<T> {
} else { } else {
path[lastIdx].Left = newItem; path[lastIdx].Left = newItem;
} }
path.Add(newItem);
// rebalance // rebalance
InsertRebalance(path); InsertRebalance(path, item);
return true; return true;
} }