Example #1
0
 internal void Add(Node node1, string s)
 {
     if (SumOfCodes.GetSum(s) <= node1.SumCodes)
     {
         if (node1.LeftChild == null)
         {
             node1.LeftChild = new Node(s);
         }
         else
         {
             Add(node1.LeftChild, s);
         }
     }
     else
     {
         if (node1.RightChild == null)
         {
             node1.RightChild = new Node(s);
         }
         else
         {
             Add(node1.RightChild, s);
         }
     }
 }
Example #2
0
 public Node(string s)
 {
     LeftChild  = null;
     RightChild = null;
     Info       = s;
     _sum_codes = SumOfCodes.GetSum(s);
 }
Example #3
0
 public NodeOfList(string info)
 {
     Previous   = null;
     Next       = null;
     Info       = info;
     _sum_codes = SumOfCodes.GetSumOfCodes(info);
 }
Example #4
0
 internal void Push(Node node1)
 {
     if (SumOfCodes.EvenSum(node1.Info))
     {
         this.Push(node1.Info);
     }
 }
Example #5
0
        public static void AskForChanges(BinaryTree MyBinTree, MyList MyList1)
        {
            bool Change = true;

            Console.WriteLine("\nif you want to change the stack of binary tree enter \"true\" else enter \"false\" \n");
            Input.InputValidated <bool>(ref Change);

            while (Change)
            {
                Console.Clear();
                Console.WriteLine("Please, choose the operation\n1.Push an element to the stack\n2.Pop an element from the stack\n3.Change an element of the stack\n4.Delete an element from the stack\n5.Add an element to the tree\n6.Delete an element from the tree\n");
                int TypeOfOperation = 0;
                Input.MyIntParseInterval(ref TypeOfOperation, 1, 6);
                function operation = null;;
                switch (TypeOfOperation)
                {
                case 1:
                {
                    operation = MyList1.AskForAdding;
                    break;
                }

                case 2:
                {
                    operation = MyList1.Pop;
                    break;
                }

                case 3:
                {
                    operation = MyList1.AskForChanging;
                    break;
                }

                case 4:
                {
                    operation = MyList1.AskForDeleting;
                    break;
                }

                case 5:
                {
                    Console.WriteLine("\nenter the string you want to add\n");
                    string StringToAdd = Console.ReadLine();
                    MyBinTree.Add(StringToAdd);
                    if (SumOfCodes.EvenSum(StringToAdd))
                    {
                        MyList1.Push(StringToAdd);
                    }

                    break;
                }

                case 6:
                {
                    Console.WriteLine("\nenter the string you want to delete\n");
                    string StringToDelete = Console.ReadLine();
                    if (!MyBinTree.Delete(StringToDelete))
                    {
                        Console.WriteLine("\nthere is no such string in the tree\n");
                        break;
                    }
                    if (SumOfCodes.EvenSum(StringToDelete))
                    {
                        MyList1.Delete(StringToDelete);
                    }
                    break;
                }
                }

                if (TypeOfOperation != 5 && TypeOfOperation != 6)
                {
                    operation();
                }

                MyBinTree.PrintElements();
                Console.WriteLine("\n\n");
                MyList1.Print();
                Console.WriteLine("\n\n");
                Console.WriteLine("\nif you want to continue changing the stack or binary tree enter \"true\" else enter \"false\"\n");
                Input.InputValidated <bool>(ref Change);
            }
        }
Example #6
0
        public bool  Delete(string s)
        {
            if (_root == null)
            {
                return(false);
            }
            TypedReference NodePtr      = __makeref(_root);
            int            SumCodesForS = SumOfCodes.GetSumOfCodes(s);

            while (__refvalue(NodePtr, Node).Info != s)
            {
                if (SumCodesForS <= __refvalue(NodePtr, Node).SumCodes&& __refvalue(NodePtr, Node).Info != s)
                {
                    NodePtr = __makeref(__refvalue(NodePtr, Node).LeftChild);
                }
                else
                {
                    NodePtr = __makeref(__refvalue(NodePtr, Node).RightChild);
                }

                if (__refvalue(NodePtr, Node) == null)
                {
                    return(false);
                }
            }
            Node CopyOfNeededNode = __refvalue(NodePtr, Node);

            if (CopyOfNeededNode.IsLeaf)
            {
                __refvalue(NodePtr, Node) = null;
                return(true);
            }

            TypedReference ChangeToPtr;

            if (CopyOfNeededNode.LeftChild != null)
            {
                ChangeToPtr = __makeref(__refvalue(NodePtr, Node).LeftChild);
                while (__refvalue(ChangeToPtr, Node).RightChild != null)
                {
                    ChangeToPtr = __makeref(__refvalue(ChangeToPtr, Node).RightChild);
                }
            }

            else
            {
                ChangeToPtr = __makeref(__refvalue(NodePtr, Node).RightChild);
                while (__refvalue(ChangeToPtr, Node).LeftChild != null)
                {
                    ChangeToPtr = __makeref(__refvalue(ChangeToPtr, Node).LeftChild);
                }
            }

            string temp = __refvalue(ChangeToPtr, Node).Info;

            __refvalue(NodePtr, Node).Info     = __refvalue(ChangeToPtr, Node).Info;
            __refvalue(ChangeToPtr, Node).Info = temp;
            __refvalue(ChangeToPtr, Node)      = null;

            return(true);
        }