From 6862c198a263f7323b03a2e23cfb14dd02ea6b0d Mon Sep 17 00:00:00 2001
From: efaal85565 <efaal85565@th-nuernberg.de>
Date: Fri, 17 Jun 2022 13:34:46 +0200
Subject: [PATCH] =?UTF-8?q?fix=20root=20l=C3=B6schen?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Dictionaries/src/BinaryTree/AVLTree.cs |  14 +--
 Dictionaries/src/BinaryTree/Treap.cs   | 125 +++++++++++++------------
 2 files changed, 72 insertions(+), 67 deletions(-)

diff --git a/Dictionaries/src/BinaryTree/AVLTree.cs b/Dictionaries/src/BinaryTree/AVLTree.cs
index e7017b0..f186011 100644
--- a/Dictionaries/src/BinaryTree/AVLTree.cs
+++ b/Dictionaries/src/BinaryTree/AVLTree.cs
@@ -149,15 +149,15 @@ namespace Dictionaries.src.BinaryTree
                 //element is found 
                 else
                 {
-                    if (current.right != null)
+                    if (current.left != null)
                     {
-                        maxNode = current.right; // find sym. follower  
-                        while (maxNode.left != null)
+                        maxNode = current.left; // find sym. Vorgänger  
+                        while (maxNode.right != null)
                         {
-                            maxNode = maxNode.left;
+                            maxNode = maxNode.right;
                         }
-                        current.data = maxNode.data; // set new sym. follower Node  
-                        current.right = deleteHelper(current.right, maxNode.data); // rebalance tree 
+                        current.data = maxNode.data; // set new sym. Vorgänger Node  
+                        current.left = deleteHelper(current.left, maxNode.data); // rebalance tree 
                         if (balance_factor(current) == 2) // rebalance right sub tree 
                         {
                             if (balance_factor(current.left) >= 0)
@@ -168,7 +168,7 @@ namespace Dictionaries.src.BinaryTree
                     }
                     else
                     {   
-                        return current.left; // case: no sym. Follower (deleted biggest value) 
+                        return current.right; // case: no sym. Vorgänger (deleted biggest value) 
                     }
                 }
             }
diff --git a/Dictionaries/src/BinaryTree/Treap.cs b/Dictionaries/src/BinaryTree/Treap.cs
index 8a73d61..b105c77 100644
--- a/Dictionaries/src/BinaryTree/Treap.cs
+++ b/Dictionaries/src/BinaryTree/Treap.cs
@@ -24,7 +24,7 @@ namespace Dictionaries.src.BinaryTree
                     current = current.right;
                 else
                 {
-                    return false; 
+                    return false;
                 }
             }
 
@@ -36,7 +36,7 @@ namespace Dictionaries.src.BinaryTree
 
             if (this.root == null)//Tree is empty
                 this.root = newNode;
-            else 
+            else
             {
                 newNode.parent = parent;
                 if (elem < parent.data)
@@ -44,7 +44,7 @@ namespace Dictionaries.src.BinaryTree
                 else
                     parent.right = newNode;
             }
-            ensureHeapCondition(newNode); 
+            ensureHeapCondition(newNode);
 
             return true;
         }
@@ -53,7 +53,7 @@ namespace Dictionaries.src.BinaryTree
         /// rotations to ensure Heap Condition
         /// </summary>
         /// <param name="node"></param>
-        private void ensureHeapCondition(TreapNode node) //notizen 
+        private void ensureHeapCondition(TreapNode node)
         {
             while (node.parent != null && node.priority < node.parent.priority)
             {
@@ -92,7 +92,7 @@ namespace Dictionaries.src.BinaryTree
                     node.left = oldParent;
                     oldParent.parent = node;
                     oldParent.right = temp;
-                    if (temp != null)    
+                    if (temp != null)
                     {
                         temp.parent = oldParent;
                     }
@@ -101,7 +101,7 @@ namespace Dictionaries.src.BinaryTree
                 {
                     node.parent = null;
                     root = node;
-                }       
+                }
             }
         }
 
@@ -126,7 +126,7 @@ namespace Dictionaries.src.BinaryTree
         /// <returns></returns>
         private TreapNode deleteHelper(TreapNode current, int elem)
         {
-            TreapNode temp = null;
+            TreapNode temp;
             if (current == null)
                 return null;
             else
@@ -141,83 +141,88 @@ namespace Dictionaries.src.BinaryTree
                 }
                 else // found elem
                 {
-                    temp.data = current.data;
-                    temp.priority = current.priority;
-
-                    while (current.left != null || current.right != null)
+                    if (current.right != null) // same like in AVL Tree 
                     {
-                        if (current.right != null) // rechts runter solange es geht 
+                        // find max()
+                        temp = current.right;
+                        while (temp.left != null)
                         {
-                            current.right.data = current.data;
-                            current.right.priority = current.priority;
-                            current = current.right;
+                            temp = temp.left;
                         }
-                        else // links runter 
+
+                        current.data = temp.data;
+                        
+                        current.priority = temp.priority;
+                        if(temp.parent.right == temp)
                         {
-                            current.left.data = current.data;
-                            current.left.priority = current.priority;
-                            current = current.left;
+                            temp.parent.right = temp.right;
                         }
+                        if(temp.parent.left == temp)
+                        {
+                            temp.parent.left = temp.left;
+                        }
+
+                        
+
+                        ensureHeapCondition(current);
                     }
-                    if (current.left.data == current.data){
-                        current.left = temp;
-                    }
-                    else{
-                        current.right = temp;
+                    else
+                    {
+                            return current.left;
+                        
                     }
 
-
-                    ensureHeapCondition(temp); // rotate the Tree 
+                   
                 }
-
-                return current;
             }
+            return current;
         }
 
         public bool search(int elem)
-        {
-            TreapNode current;
-            if (root != null) //case: empty tree 
             {
-
-                if (elem == root.data)// case: root to be found
+                TreapNode current;
+                if (root != null) //case: empty tree 
                 {
-                    return true;
-                }
-                current = root;
-                while (current.left != null || current.right != null)
-                {
-                    if (elem < current.data)//case: left subtree
-                        current = current.left;
-                    else
-                        current = current.right; // case: right subtree
 
-                    if (elem == current.data) // elem found 
+                    if (elem == root.data)// case: root to be found
+                    {
                         return true;
+                    }
+                    current = root;
+                    while (current.left != null || current.right != null)
+                    {
+                        if (elem < current.data)//case: left subtree
+                            current = current.left;
+                        else
+                            current = current.right; // case: right subtree
+
+                        if (current != null && elem == current.data) // elem found 
+                            return true;
+                    }
                 }
+                return false;
             }
-            return false;
-        }
         /// <summary>
-        /// print with priority 
-        /// </summary>
-        /// <param name="node"></param>
-        /// <param name="level"></param>
+            /// print with priority 
+            /// </summary>
+            /// <param name="node"></param>
+            /// <param name="level"></param>
         public void print()
-        {
-            printInOrder(this.root);
-        }
+            {
+                printInOrder(this.root);
+            }
         public void printInOrder(TreapNode node, int level = 0)
-        {
-            if (node != null)
             {
-                printInOrder(node.right, level+1);
-                string tabstop = new string('\t', level);
-                Console.WriteLine("{0}{1} ({2}, {3}){4}", tabstop, node == root ? "" : "--", node.data, node.priority, Environment.NewLine);
-                printInOrder(node.left, level+1);
+                if (node != null)
+                {
+                    printInOrder(node.right, level + 1);
+                    string tabstop = new string('\t', level);
+                    Console.WriteLine("{0}{1} ({2}, {3}){4}", tabstop, node == root ? "" : "--", node.data, node.priority, Environment.NewLine);
+                    printInOrder(node.left, level + 1);
+                }
             }
-        }
 
+        }
     }
-}
+        
 
-- 
GitLab