Exemple #1
0
        public BinaryTreeItem FindItemBeforeItemToFind(int itemToFind)
        {
            if (itemToFind == 0)
            {
                return(null);
            }
            BinaryTreeItem actItem = this._root;

            while (this._root != null)
            {
                if (itemToFind > actItem.item)
                {
                    if (itemToFind.Equals(actItem.rightItem.item))
                    {
                        return(actItem);
                    }
                    actItem = actItem.rightItem;
                }
                else if (itemToFind < actItem.item)
                {
                    if (itemToFind.Equals(actItem.leftItem.item))
                    {
                        return(actItem);
                    }
                    actItem = actItem.leftItem;
                }
            }
            return(null);
        }
Exemple #2
0
        public BinaryTreeItem Find(int itemToFind)
        {
            if (itemToFind == 0)
            {
                return(null);
            }

            BinaryTreeItem actItem = this._root;

            while (actItem != null)
            {
                if (itemToFind > actItem.Item)
                {
                    actItem = actItem.RightItem;
                }
                else if (itemToFind < actItem.Item)
                {
                    actItem = actItem.LeftItem;
                }
                else
                {
                    return(actItem);
                }
            }
            return(null);
        }
Exemple #3
0
 public BinaryTreeItem(int item, int count, BinaryTreeItem rightItem, BinaryTreeItem leftItem)
 {
     this.Item      = item;
     this.Count     = count;
     this.RightItem = rightItem;
     this.LeftItem  = leftItem;
 }
Exemple #4
0
 public BinaryTreeItem(int item, BinaryTreeItem right, BinaryTreeItem left, int counter)
 {
     this.Item    = item;
     this.Right   = right;
     this.Left    = left;
     this.Counter = counter;
 }
Exemple #5
0
        public BinaryTreeItem FindMinRecursiv(int searchItem, BinaryTreeItem actItem = null)
        {
            if (this._root == null)
            {
                return(null);
            }

            if ((searchItem == 0) && (actItem == null))
            {
                actItem = this._root;
            }

            if (actItem == null)
            {
                actItem = Find(searchItem);
            }

            if (actItem.LeftItem == null)
            {
                return(actItem);
            }

            else
            {
                actItem = actItem.LeftItem;
            }
            return(FindMinRecursiv(searchItem, actItem));
        }
Exemple #6
0
        public BinaryTreeItem FindMax(int searchFrom = 0)
        {
            if (searchFrom == 0)
            {
                BinaryTreeItem actItem = _root;

                while (actItem != null)
                {
                    if (actItem.RightItem == null)
                    {
                        return(actItem);
                    }
                    actItem = actItem.RightItem;
                }
            }

            else
            {
                BinaryTreeItem actItem = Find(searchFrom);

                while (actItem != null)
                {
                    if (actItem.RightItem == null)
                    {
                        return(actItem);
                    }
                    actItem = actItem.RightItem;
                }
            }
            return(null);
        }
Exemple #7
0
        public BinaryTreeItem Maximum(int MaxItem, BinaryTreeItem actItem = null)
        {
            if ((MaxItem == 0) || (this._root == null))
            {
                return(null);
            }
            //Maxitem in der Liste finden
            BinaryTreeItem foundItem = Find(MaxItem);

            if (foundItem == null)
            {
                return(null);
            }
            //actItem zum weiterlaufen der Liste
            if (actItem == null)
            {
                actItem = foundItem;
            }
            //gibt es ein größeres Item
            if (actItem.Right != null)
            {
                actItem = actItem.Right;
            }
            //Überprüfen & Stop für Rekursion
            if (actItem.Right == null)
            {
                return(actItem);
            }
            else
            {
                return(Maximum(MaxItem, actItem));
            }
        }
Exemple #8
0
        public BinaryTreeItem Minimum(int MinItem, BinaryTreeItem actItem = null)
        {
            if ((MinItem == 0) || (this._root == null))
            {
                return(null);
            }
            //MinItem in der Liste finden
            BinaryTreeItem foundItem = Find(MinItem);

            if (foundItem == null)
            {
                return(null);
            }
            //actItem zum weiterlaufen der Liste
            if (actItem == null)
            {
                actItem = foundItem;
            }
            //wenn es etwas kleineres gibt
            if (actItem.Left != null)
            {
                actItem = actItem.Left;
            }
            //überprüfen & Stop für Rekursion
            if (actItem.Left == null)
            {
                return(actItem);
            }
            else
            {
                return(Minimum(MinItem, actItem));
            }
        }
Exemple #9
0
        public BinaryTreeItem FindRecursiv(int itemToFind, BinaryTreeItem actItem = null)
        {
            if (itemToFind == 0)
            {
                return(null);
            }

            if (this._root == null)
            {
                return(null);
            }

            if (actItem == null)
            {
                actItem = this._root;
            }

            if (itemToFind > actItem.Item)
            {
                actItem = actItem.RightItem;
            }
            else if (itemToFind < actItem.Item)
            {
                actItem = actItem.LeftItem;
            }

            else
            {
                return(actItem);
            }

            return(FindRecursiv(itemToFind, actItem));
        }
Exemple #10
0
        public BinaryTreeItem FindItemBeforeItem(int itemToFind)
        {
            if (itemToFind == 0)
            {
                return(null);
            }

            BinaryTreeItem actItem = this._root;

            while (actItem != null)
            {
                if (itemToFind > actItem.Item)
                {
                    if (actItem.RightItem.Item.Equals(itemToFind))
                    {
                        return(actItem);
                    }
                    actItem = actItem.RightItem;
                }

                if (itemToFind < actItem.Item)
                {
                    if (actItem.LeftItem.Item.Equals(itemToFind))
                    {
                        return(actItem);
                    }
                    actItem = actItem.LeftItem;
                }
            }
            return(null);
        }
 public BinaryTreeItem(int Item, int count, BinaryTreeItem RightItem, BinaryTreeItem LeftItem)
 {
     this.item      = Item;
     this.Count     = count;
     this.rightItem = RightItem;
     this.leftItem  = LeftItem;
 }
Exemple #12
0
        public BinaryTree()
        {
            this._root = null;

            this._root2 = null;

            this._root2 = _root;
        }
Exemple #13
0
        public BinaryTreeItem Remove(int itemToRemove, BinaryTreeItem actItem = null)
        {
            if (actItem == null)
            {
                actItem = this._root;
            }

            if (itemToRemove == this._root.Item)
            {
                actItem          = actItem.Right;
                actItem          = MinimumRecursiv(actItem.Item);
                actItem.Left     = this._root.Left;
                this._root.Right = this._root;
                return(actItem);
            }
            else
            {
                while ((actItem.Left != null) && (actItem.Right != null))
                {
                    if (itemToRemove > actItem.Item)
                    {
                        actItem = actItem.Right;
                    }
                    else if (itemToRemove < actItem.Item)
                    {
                        actItem = actItem.Left;
                    }
                    //Fall a)
                    if ((actItem.Left.Item == itemToRemove) && (actItem.Left.Left == null))
                    {
                        actItem.Left = actItem.Left.Right;
                        return(actItem);
                    }
                    else if ((actItem.Right.Item == itemToRemove) && (actItem.Right.Left == null))
                    {
                        actItem.Right = actItem.Right.Right;
                        return(actItem);
                    }
                    //Fall b)
                    if ((actItem.Left.Item == itemToRemove) && (actItem.Left.Right == null))
                    {
                        actItem.Left = actItem.Left.Left;
                        return(actItem);
                    }
                    else if ((actItem.Right.Item == itemToRemove) && (actItem.Right.Right == null))
                    {
                        actItem.Right = actItem.Right.Left;
                        return(actItem);
                    }
                    //Fall c)
                    bool           isFirstItem;
                    BinaryTreeItem itemBeforeItem = this.ItemBeforeItem(itemToRemove, out isFirstItem);
                }
                return(actItem);
                //Dieser kommentar wurde am 20.11.2019 geschriebem
            }
        }
Exemple #14
0
        public BinaryTreeItem ItemBeforeItem(int itemToFind, out bool isStartItem, BinaryTreeItem actItem = null)
        {
            isStartItem = false;

            if (this._root == null)
            {
                return(null);
            }

            if (actItem == null)
            {
                actItem = this._root;
            }

            if (this._root.Item == itemToFind)
            {
                isStartItem = true;
                return(null);
            }

            while (actItem != null)
            {
                //Überprüfung um das itemBeforItem zu finden
                if (actItem.Left.Item == itemToFind)
                {
                    return(actItem);
                }
                else if (actItem.Right.Item == itemToFind)
                {
                    return(actItem);
                }
                //actItem auf das nächst kleinere setzen
                if (actItem.Item > itemToFind)
                {
                    actItem = actItem.Left;
                }
                //actItem auf das nächst größere setzen
                else if (actItem.Item < itemToFind)
                {
                    actItem = actItem.Right;
                }

                if (actItem == null)
                {
                    return(null);
                }
                else
                {
                    return(Find(itemToFind, actItem));
                }
            }

            return(null);
        }
Exemple #15
0
        //Methode Add
        public bool Add(int itemToAdd)
        {
            if (itemToAdd == 0)
            {
                return(false);
            }
            //Liste gibt es nicht
            if (this._root == null)
            {
                this._root = new BinaryTreeItem(itemToAdd, null, null, 1);
                return(true);
            }

            //größer oder kleiner
            BinaryTreeItem actItem = new BinaryTreeItem();

            actItem = _root;

            while (actItem != null)
            {
                if (itemToAdd < actItem.Item)
                {
                    if (actItem.Left == null)
                    {
                        actItem.Left = new BinaryTreeItem(itemToAdd, null, null, 1);
                        return(true);
                    }
                    else
                    {
                        actItem = actItem.Left;
                    }
                }
                else if (itemToAdd > actItem.Item)
                {
                    if (actItem.Right == null)
                    {
                        actItem.Right = new BinaryTreeItem(itemToAdd, null, null, 1);
                        return(true);
                    }
                    else
                    {
                        actItem = actItem.Right;
                    }
                }
                else
                {
                    actItem.Counter++;
                    return(true);
                }
            }
            //Hier war dei falsche Methode
            return(false);
        }
Exemple #16
0
        public bool AddRek(int itemToAdd, BinaryTreeItem actItem = null)
        {
            //Liste gibt es nicht
            if (this._root == null)
            {
                this._root = new BinaryTreeItem(itemToAdd, null, null, 1);
                return(true);
            }
            //größer, kleiner, gleich vergleich
            if (actItem == null)
            {
                actItem = this._root;
            }

            if ((itemToAdd < actItem.Item) && (actItem.Left != null))
            {
                actItem = actItem.Left;
            }
            else if ((itemToAdd > actItem.Item) && (actItem.Right != null))
            {
                actItem = actItem.Right;
            }


            if ((actItem.Right == null) && (itemToAdd > actItem.Item))
            {
                actItem.Right = new BinaryTreeItem(itemToAdd, null, null, 1);
                return(true);
            }
            else if ((actItem.Left == null) && (itemToAdd < actItem.Item))
            {
                actItem.Left = new BinaryTreeItem(itemToAdd, null, null, 1);
                return(true);
            }
            else if (actItem.Item.Equals(itemToAdd))
            {
                actItem.Counter++;
                return(true);
            }
            else
            {
                return(AddRek(itemToAdd, actItem));
            }
        }
Exemple #17
0
 public bool Add(int itemToAdd)
 {
     if (this._root == null)
     {
         this._root = new BinaryTreeItem(itemToAdd, 1, null, null);
         increaseCounter();
         return(true);
     }
     else
     {
         BinaryTreeItem actItem = this._root;
         while (actItem != null)
         {
             if (itemToAdd > actItem.item)
             {
                 if (actItem.rightItem == null)
                 {
                     actItem.rightItem = new BinaryTreeItem(itemToAdd, 0, null, null);
                     increaseCounter();
                     return(true);
                 }
                 actItem = actItem.rightItem;
             }
             else if (itemToAdd < actItem.item)
             {
                 if (actItem.leftItem == null)
                 {
                     actItem.leftItem = new BinaryTreeItem(itemToAdd, 0, null, null);
                     increaseCounter();
                     return(true);
                 }
                 actItem = actItem.leftItem;
             }
             else
             {
                 increaseCounter();
                 actItem.Count = actItem.Count + 1;
                 return(true);
             }
         }
         return(false);
     }
 }
Exemple #18
0
        public bool AddRekursiv(int itemToAdd, BinaryTreeItem actItem = null)
        {
            if (_root == null)
            {
                this._root = new BinaryTreeItem(itemToAdd, 1, null, null);
                IncreaseCounter();
                return(true);
            }

            if (actItem == null)
            {
                actItem = _root;
            }

            if (itemToAdd > actItem.Item)
            {
                if (actItem.RightItem == null)
                {
                    actItem.RightItem = new BinaryTreeItem(itemToAdd, 1, null, null);
                    IncreaseCounter();
                    return(true);
                }
                actItem = actItem.RightItem;
            }
            else if (itemToAdd < actItem.Item)
            {
                if (actItem.LeftItem == null)
                {
                    actItem.LeftItem = new BinaryTreeItem(itemToAdd, 1, null, null);
                    IncreaseCounter();
                    return(true);
                }
                actItem = actItem.LeftItem;
            }
            else
            {
                actItem.Count = actItem.Count + 1;
                IncreaseCounter();
                return(true);
            }
            return(AddRekursiv(itemToAdd, actItem));
        }
Exemple #19
0
        public BinaryTreeItem Find(int itemToFind, BinaryTreeItem actItem = null)
        {
            if ((itemToFind == 0) || (this._root == null))
            {
                return(null);
            }

            if (actItem == null)
            {
                actItem = this._root;
            }

            if (actItem.Item > itemToFind)
            {
                actItem = actItem.Left;
            }
            else if (actItem.Item < itemToFind)
            {
                actItem = actItem.Right;
            }
            else
            {
                return(actItem);
            }

            if (actItem == null)
            {
                return(null);
            }
            else if (actItem.Item.Equals(itemToFind))
            {
                return(actItem);
            }
            else
            {
                return(Find(itemToFind, actItem));
            }
        }
Exemple #20
0
        public BinaryTreeItem MaximumRecursiv(int?startValue = null, BinaryTreeItem actItem = null)
        {
            //bei "ersten" Aufruf ist actItem null
            if (actItem == null)
            {
                //falls startValue != null ist, suchen wir das Element im Baum und setzen das actItem
                if (startValue != null)
                {
                    actItem = Find(startValue.Value);
                }
                //ansonsten starten wir bei _root
                else
                {
                    actItem = this._root;
                }
            }
            //bei allen weiteren Aufrufen, setzen wir den Zeiger auf den RightItem
            else
            {
                actItem = actItem.Right;
            }

            if (actItem == null)
            {
                return(null);
            }

            //Minimum wurde gefunden
            if (actItem.Right == null)
            {
                return(actItem);
            }
            else
            {
                return(MaximumRecursiv(startValue, actItem));
            }
        }
Exemple #21
0
        public bool Remove(int itemToRemove)
        {
            //Fall a
            BinaryTreeItem actItem = Find(itemToRemove);

            if (actItem.Equals(_root))
            {
                return(false);
            }
            BinaryTreeItem BeforeActItem = FindItemBeforeItem(itemToRemove);

            if (actItem.LeftItem == null)
            {
                if (actItem.Item > BeforeActItem.Item)
                {
                    BeforeActItem.RightItem = actItem.RightItem;
                    return(true);
                }
                if (actItem.Item < BeforeActItem.Item)
                {
                    BeforeActItem.LeftItem = actItem.RightItem;
                    return(true);
                }
            }

            // Fall 2:
            else if (actItem.RightItem == null)
            {
                if (actItem.Item > BeforeActItem.Item)
                {
                    BeforeActItem.RightItem = actItem.LeftItem;
                    return(true);
                }
                if (actItem.Item < BeforeActItem.Item)
                {
                    BeforeActItem.LeftItem = actItem.LeftItem;
                    return(true);
                }
            }

            // Fall 3:
            else if (actItem.RightItem.LeftItem == null)
            {
                if (actItem.Item > BeforeActItem.Item)
                {
                    BeforeActItem.RightItem    = actItem.RightItem;
                    actItem.RightItem.LeftItem = actItem.LeftItem;
                    return(true);
                }
                if (actItem.Item < BeforeActItem.Item)
                {
                    BeforeActItem.LeftItem     = actItem.RightItem;
                    actItem.RightItem.LeftItem = actItem.LeftItem;
                    return(true);
                }
            }

            else if (actItem.LeftItem.RightItem == null)
            {
                if (actItem.Item > BeforeActItem.Item)
                {
                    BeforeActItem.RightItem    = actItem.LeftItem;
                    actItem.LeftItem.RightItem = actItem.RightItem;
                    return(true);
                }
                if (actItem.Item < BeforeActItem.Item)
                {
                    BeforeActItem.LeftItem     = actItem.LeftItem;
                    actItem.LeftItem.RightItem = actItem.RightItem;
                    return(true);
                }
            }

            //Fall 4:
            else
            {
                BinaryTreeItem minItem       = FindMin(actItem.RightItem.Item);
                BinaryTreeItem beforeMinItem = FindItemBeforeItem(minItem.Item);

                if (actItem.Item > BeforeActItem.Item)
                {
                    minItem.LeftItem        = actItem.LeftItem;
                    beforeMinItem.LeftItem  = minItem.RightItem;
                    minItem.RightItem       = actItem.RightItem;
                    BeforeActItem.RightItem = minItem;

                    return(true);
                }
                if (actItem.Item < BeforeActItem.Item)
                {
                    minItem.LeftItem       = actItem.LeftItem;
                    beforeMinItem.LeftItem = minItem.RightItem;
                    minItem.RightItem      = actItem.RightItem;
                    BeforeActItem.LeftItem = minItem;
                    return(true);
                }
            }

            return(false);
        }
Exemple #22
0
 public BinaryTree()
 {
     _root = null;
 }
Exemple #23
0
 public BinaryTree(BinaryTree bt)
 {
     this._root = bt._root;
 }
Exemple #24
0
 public BinaryTree(int item)
 {
     this._root = new BinaryTreeItem(item, 0, null, null);
 }
Exemple #25
0
 public BinaryTree(int Item)
 {
     this._root = new BinaryTreeItem(Item, null, null, 1);
 }
Exemple #26
0
 public BinaryTree()
 {
     this._root = null;
 }