示例#1
0
文件: Q3.cs 项目: dzilbers/yarkon5779
        public static bool What(BinNode <int> pos)
        {
            BinNode <int> left  = FirstLeft(pos);
            BinNode <int> right = FirstRight(pos);

            int sum = left.GetValue() + right.GetValue();

            left  = left.GetRight();
            right = right.GetLeft();

            while ((left != right) && (left.GetRight() != right) &&
                   (left.GetValue() + right.GetValue() != sum))
            {
                left  = left.GetRight();
                right = right.GetLeft();
            }
            if (left == right)
            {
                return(right.GetValue() == sum);
            }

            if (left.GetRight() == right)
            {
                return(left.GetValue() + right.GetValue() == sum);
            }
            return(false);
            // return left.GetValue() + right.GetValue() == sum;
        }
示例#2
0
 public static bool IsBst(BinNode <int> t)
 {
     //אם עץ ריק
     if (t == null)
     {
         return(true);
     }
     //אם יש ילד שמאלי גדול מהשורש הנוכחי
     if (t.HasLeft())
     {
         if (t.GetValue() <= t.GetLeft().GetValue())
         {
             return(false);
         }
     }
     //אם יש ילד ימני קטן מהשורש הנוכחי
     if (t.HasRight())
     {
         if (t.GetValue() > t.GetRight().GetValue())
         {
             return(false);
         }
     }
     //נבדק שגם תת עץ שמאל וגם תת עץ ימין הם עץ חיפוש
     return(IsBst(t.GetLeft()) && IsBst(t.GetRight()));
 }
示例#3
0
        public static int Calc(BinNode <int> exp)
        {
            // 1+, 2-, 3*, 4/
            if (IsLeaf(exp))
            {
                return(exp.GetValue());
            }
            // כאן זה לא עלה אלא פעולה ולכן יש בודאות שני הבנים
            int left  = Calc(exp.GetLeft());
            int right = Calc(exp.GetRight());
            int oper  = exp.GetValue();

            if (oper == 1)
            {
                return(left + right);
            }
            if (oper == 2)
            {
                return(left - right);
            }
            if (oper == 3)
            {
                return(left * right);
            }
            // if (oper == 4) - זה מה שנשאר
            return(left / right);
        }
示例#4
0
 public static int MaxInTree(BinNode <int> t, int max)
 {
     if (t == null)
     {
         return(max);
     }
     if (t.GetValue() > max)
     {
         max = t.GetValue();
     }
     return(Math.Max(MaxInTree(t.GetLeft(), max), MaxInTree(t.GetRight(), max)));
 }
示例#5
0
 public static void PrintEvenNodes(BinNode <int> t)
 {
     if (t != null)
     {
         if (t.GetValue() % 2 == 0)
         {
             Console.WriteLine(t.GetValue());
         }
         PrintEvenNodes(t.GetLeft());
         PrintEvenNodes(t.GetRight());
     }
 }
示例#6
0
 public static double TRG23b(BinNode <double> tree, double Min)
 {
     if (tree == null)
     {
         return(Min);
     }
     if (tree.GetValue() < Min)
     {
         Min = tree.GetValue();
     }
     return(Math.Min(TRG23(tree.GetRight(), Min), TRG23(tree.GetLeft(), Min)));
 }
示例#7
0
 public static double TRG23(BinNode <double> tree, double Max)
 {
     if (tree == null)
     {
         return(Max);
     }
     if (tree.GetValue() > Max)
     {
         Max = tree.GetValue();
     }
     return(Math.Max(TRG23(tree.GetRight(), Max), TRG23(tree.GetLeft(), Max)));
 }
示例#8
0
 public static bool Branch(BinNode <int> t1, BinNode <int> t2, string ident)
 {
     if (t1 == null && t2 == null)
     {
         return(true);
     }
     Console.WriteLine(ident + t1.GetValue() + "/" + t2.GetValue());
     if ((t1 != null && t2 == null) || (t1 == null && t2 != null))
     {
         return(false);
     }
     return(t1.GetValue() == t2.GetValue() && Branch(t1.GetLeft(), t2.GetRight(), ident + " "));
 }
示例#9
0
        public static void PrintTreeWithLevels <T>(BinNode <T> t)
        {
            //תור עזר של צמתים
            Queue <BinNode <T> > qNodes = new Queue <BinNode <T> >();
            Queue <int>          levels = new Queue <int>();

            //הכנסת השורש
            qNodes.Insert(t);
            int currLevel = 0;

            levels.Insert(currLevel);
            while (!qNodes.IsEmpty())
            {
                //נשלוף את הצומת
                BinNode <T> temp = qNodes.Remove();
                currLevel = levels.Remove();
                //קוד לביצוע
                Console.WriteLine(temp.GetValue() + "-" + "level:" + currLevel);
                if (temp.HasLeft())
                {
                    qNodes.Insert(t.GetLeft());
                    levels.Insert(currLevel + 1);
                }
                if (temp.HasRight())
                {
                    qNodes.Insert(t.GetRight());
                    levels.Insert(currLevel + 1);
                }
            }
        }
示例#10
0
        public static void BuildTree(BinNode <double> tree) // Enter -1 To End
        {
            Console.WriteLine("Current Root Is: " + tree.GetValue());
            Console.Write("Enter Left Num: ");
            double leftValue = double.Parse(Console.ReadLine());

            if (leftValue == -1)
            {
                return;
            }
            BinNode <double> left = new BinNode <double>(leftValue);

            Console.Write("Enter Right Num: ");
            double rightValue = double.Parse(Console.ReadLine());

            if (rightValue == -1)
            {
                return;
            }
            BinNode <double> right = new BinNode <double>(rightValue);

            tree.SetRight(right);
            tree.SetLeft(left);
            BuildTree(left);
            BuildTree(right);
        }
示例#11
0
 public static void AddLeaves(BinNode <int> t, int n)
 {
     if (t == null)
     {
         return;
     }
     if (t.HasLeft() || t.HasRight())
     {
         AddLeaves(t.GetLeft(), n);
         AddLeaves(t.GetRight(), n);
     }
     else if (t.GetValue() > n)
     {
         Console.Write(t.GetValue() + ",");
         t.SetRight(new BinNode <int>(n));
     }
 }
示例#12
0
 public static bool TRG18(BinNode <int> t1, BinNode <int> t2) // O(n*m)
 {
     if (t2 != null)
     {
         int temp = t2.GetValue();
         return(TRG18(t1, temp) && TRG18(t1, t2.GetLeft()) && TRG18(t1, t2.GetRight()));
     }
     return(true);
 }
示例#13
0
 public static void PrintPostorder <T>(BinNode <T> tr)
 { // סריקה סופית
     if (tr == null)
     {
         return;
     }
     PrintPostorder(tr.GetLeft());
     PrintPostorder(tr.GetRight());
     Console.WriteLine(tr.GetValue());
 }
示例#14
0
 public static void PrintPreorder <T>(BinNode <T> tr)
 { // סריקה תחילית
     if (tr == null)
     {
         return;
     }
     Console.WriteLine(tr.GetValue());
     PrintInorder(tr.GetLeft());
     PrintInorder(tr.GetRight());
 }
示例#15
0
        public static void PrintInOrderTree(BinNode <int> tree)
        {
            if (tree != null)
            {
                PrintInOrderTree(tree.GetLeft());
                int val = tree.GetValue();
                ///יבוא קוד שאני רוצה לבצע על הצומת.
                Console.Write(val + "--->");

                PrintInOrderTree(tree.GetRight());
            }
        }
示例#16
0
        public static void InOrderTree <T>(BinNode <T> tree)
        {
            //option 2: if(tree==null)
            //     return;
            if (tree != null)
            {
                InOrderTree(tree.GetLeft());
                T val = tree.GetValue();
                ///יבוא קוד שאני רוצה לבצע על הצומת.
                ///{}

                InOrderTree(tree.GetRight());
            }
        }
示例#17
0
        public static bool SearchBinTree <T>(BinNode <T> t, T val)
        {
            if (t == null)
            {
                return(false);
            }

            if (t.GetValue().Equals(val))
            {
                return(true);
            }
            return(SearchBinTree(t.GetLeft(), val) || SearchBinTree(t.GetRight(), val));

            //(t.GetValue().Equals(val)||SearchBinTree(t.GetLeft(), val) || SearchBinTree(t.GetRight(), val);
        }
示例#18
0
 public static bool TRG18(BinNode <int> t1, int temp) // Returns True if temp is in t1.
 {
     if (t1 != null)
     {
         if (temp == t1.GetValue())
         {
             return(true);
         }
         else
         {
             return(TRG18(t1.GetLeft(), temp) || TRG18(t1.GetRight(), temp));
         }
     }
     return(false);
 }
示例#19
0
 public static int TRG20Counter(BinNode <int> tree, int temp)
 {
     if (tree != null)
     {
         if (tree.GetValue() == temp)
         {
             return(1 + TRG20Counter(tree.GetRight(), temp) + TRG20Counter(tree.GetLeft(), temp));
         }
         else
         {
             return(TRG20Counter(tree.GetRight(), temp) + TRG20Counter(tree.GetLeft(), temp));
         }
     }
     return(0);
 }
示例#20
0
 public static void TRG11(BinNode <int> tree) // O(n)
 {
     if (tree != null)
     {
         if (tree.GetValue() % 2 == 0)
         {
             if ((!tree.HasLeft() || tree.GetLeft().GetValue() % 2 == 0) &&
                 (!tree.HasRight() || tree.GetRight().GetValue() % 2 == 0))
             {
                 Console.WriteLine(tree);
             }
         }
         TRG11(tree.GetLeft());
         TRG11(tree.GetRight());
     }
 }
示例#21
0
        //static void Main(string[] args)
        //{
        //    //double[,] arr = { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 } };
        //    //Console.WriteLine(Average2D(arr, 4));
        //    Stack<int> stk = new Stack<int>();
        //    for (int i = 1; i <= 5; ++i)
        //        stk.Push(i);
        //    Page120Ex3(stk);
        //}

        public static void TRG9(BinNode <char> tree) // O(n)
        {
            if (tree != null)
            {
                char CharToChange = tree.GetValue();
                if (CharToChange == 'z')
                {
                    CharToChange = 'a';
                }
                else
                {
                    CharToChange++;
                }
                tree.SetValue(CharToChange);
                TRG9(tree.GetLeft());
                TRG9(tree.GetRight());
            }
        }
示例#22
0
 /// <summary>
 /// פעולה המקבלת ערך ומוסיפה אותו לעץ החיפוש
 /// </summary>
 /// <param name="t">שורש העץ</param>
 /// <param name="val">ערך להכנסה</param>
 /// <returns></returns>
 public static BinNode <int> CreateSearchTree(BinNode <int> t, int val)
 {
     //אם העץ ריק
     if (t == null)
     {
         return(new BinNode <int>(val));
     }
     //אם הערך קטן מהשורש= הכנס לתת עץ שמאל
     if (t.GetValue() > val)
     {
         t.SetLeft(CreateSearchTree(t.GetLeft(), val));
     }
     //אחרת הכנס לתת עץ ימין
     else
     {
         t.SetRight(CreateSearchTree(t.GetRight(), val));
     }
     return(t);
 }
示例#23
0
        public static int Targil3(BinNode <Patient> patient, BinNode <int> parent)
        {
            if (parent == null || IsLeaf(parent))
            {
                return(0);
            }
            BinNode <Patient> parentPatient = Find(patient, parent.GetValue());

            if (parentPatient == null)
            {
                return(Targil3(patient, parent.GetLeft()) + Targil3(patient, parent.GetRight()));
            }

            if ((parent.HasLeft() && CheckInfected(parentPatient, parent.GetLeft().GetValue())) ||
                (parent.HasRight() && CheckInfected(parentPatient, parent.GetRight().GetValue())))
            {
                return(1 + Targil3(patient, parent.GetLeft()) + Targil3(patient, parent.GetRight()));
            }
            else
            {
                return(Targil3(patient, parent.GetLeft()) + Targil3(patient, parent.GetRight()));
            }
        }
示例#24
0
        public static void PrintTreeLevels <T>(BinNode <T> t)
        {
            //תור עזר של צמתים
            Queue <BinNode <T> > qNodes = new Queue <BinNode <T> >();

            //הכנסת השורש
            qNodes.Insert(t);
            while (!qNodes.IsEmpty())
            {
                //נשלוף את הצומת
                BinNode <T> temp = qNodes.Remove();
                //קוד לביצוע
                Console.WriteLine(temp.GetValue());
                if (temp.HasLeft())
                {
                    qNodes.Insert(t.GetLeft());
                }
                if (temp.HasRight())
                {
                    qNodes.Insert(t.GetRight());
                }
            }
        }
示例#25
0
        private static BinNode <Patient> Find(BinNode <Patient> patient, int id)
        {
            BinNode <Patient> temp;

            if (patient == null)
            {
                return(null);
            }
            if (patient.GetValue().GetId() == id)
            {
                return(patient);
            }
            temp = Find(patient.GetLeft(), id);
            if (temp != null)
            {
                return(temp);
            }
            temp = Find(patient.GetRight(), id);
            if (temp != null)
            {
                return(temp);
            }
            return(null);
        }
示例#26
0
        public static double TRG23(BinNode <double> tree) // O(n)
        {
            double Max = tree.GetValue();

            return(Math.Max(TRG23(tree.GetRight(), Max), TRG23(tree.GetLeft(), Max)));
        }
示例#27
0
        public static double TRG23b(BinNode <double> tree) // O(n)
        {
            double Min = tree.GetValue();

            return(Math.Min(TRG23b(tree.GetRight(), Min), TRG23b(tree.GetLeft(), Min)));
        }
示例#28
0
 public static int MaxInTree(BinNode <int> t)
 {
     return(MaxInTree(t, t.GetValue()));
 }