Пример #1
0
        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 BinNode <int> FirstLeft(BinNode <int> pos)
 {                                 // A: total 10pt
     while (pos.GetLeft() != null) // 4pt
     {
         pos = pos.GetLeft();      // 4pt
     }
     return(pos);                  // 2pt
 }
Пример #4
0
        public static bool TRG26 <T> (BinNode <T> tree)
        {
            if (tree == null)
            {
                return(true);
            }
            int left  = TRG27(tree.GetLeft());
            int right = TRG27(tree.GetRight());

            return(left == right && TRG26(tree.GetLeft()) && TRG26(tree.GetRight()));
        }
Пример #5
0
 public static bool TRG21(BinNode <int> tree) // O(n^2)
 {
     if (tree == null)
     {
         return(true);
     }
     if (Math.Abs(TRG21High(tree.GetLeft()) - TRG21High(tree.GetRight())) > 1)
     {
         return(false);
     }
     return(TRG21(tree.GetLeft()) && TRG21(tree.GetRight()));
 }
Пример #6
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);
 }
Пример #7
0
 public static void TRG10 <T>(BinNode <T> tree) // O(n)
 {
     if (tree != null)
     {
         if (!tree.HasLeft() && !tree.HasRight())
         {
             Console.WriteLine(tree);
         }
         else
         {
             TRG10(tree.GetLeft());
             TRG10(tree.GetLeft());
         }
     }
 }
Пример #8
0
 public static int DeadlyDistributor(BinNode <Patient> patient)
 {
     if (patient == null || IsLeaf(patient))
     {
         return(0);
     }
     if (DeadDescendants(patient))
     {
         return(1 + DeadlyDistributor(patient.GetLeft()) + DeadlyDistributor(patient.GetRight()));
     }
     else
     {
         return(DeadlyDistributor(patient.GetLeft()) + DeadlyDistributor(patient.GetRight()));
     }
 }
Пример #9
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);
        }
Пример #10
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);
                }
            }
        }
Пример #11
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());
     }
 }
Пример #12
0
 public static int CountNodes1 <T>(BinNode <T> tr)
 {
     if (tr == null)
     {
         return(0);
     }
     return(1 + CountNodes1(tr.GetLeft()) + CountNodes1(tr.GetRight()));
 }
Пример #13
0
 public static int TRG27 <T>(BinNode <T> tree) // O(n)
 {
     if (tree == null)
     {
         return(0);
     }
     return(1 + Math.Max(TRG27(tree.GetRight()), TRG27(tree.GetLeft())));
 }
Пример #14
0
 public static int TRG21High(BinNode <int> tree)
 {
     if (tree == null)
     {
         return(0);
     }
     return(1 + Math.Max(TRG21High(tree.GetLeft()), TRG21High(tree.GetRight())));
 }
Пример #15
0
        public static int Secret2(BinNode <int> t)
        {
            if (t == null)
            {
                return(0);
            }
            int z = 0;

            if (t.GetLeft() == t.GetRight())
            {
                z = 1;
            }
            int x = Secret2(t.GetLeft());
            int y = Secret2(t.GetRight());

            return(x + y + z);
        }
Пример #16
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);
 }
Пример #17
0
        public static int GetWidthBinTreeLevelSearch <T>(BinNode <T> t)
        {
            //תור עזר של צמתים
            Queue <BinNode <T> > qNodes = new Queue <BinNode <T> >();
            Queue <int>          levels = new Queue <int>();

            //מקסימום הכולל
            int max = 0;
            //מונה צמתים ברמה
            int currNodesInLevel = 0;
            //רמה נוכחית שכרגע נספרת
            int currLevel = 0;

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

            levels.Insert(currLevel);

            while (!qNodes.IsEmpty())
            {
                //נשלוף את הצומת
                BinNode <T> temp = qNodes.Remove();
                //נשלוף את הרמה של הצומת
                int level = levels.Remove();


                //קוד לביצוע
                //אם הרמה זהה
                if (currLevel == level)
                {
                    currNodesInLevel++;
                    if (currNodesInLevel > max)
                    {
                        max = currNodesInLevel;
                    }
                }
                //החלפת רמה

                {
                    currNodesInLevel = 0;
                    currLevel        = level;
                }

                if (temp.HasLeft())
                {
                    qNodes.Insert(t.GetLeft());
                    levels.Insert(currLevel + 1);
                }
                if (temp.HasRight())
                {
                    qNodes.Insert(t.GetRight());
                    levels.Insert(currLevel + 1);
                }
            }
            return(max);
        }
Пример #18
0
 /// <summary>
 /// תרגיל פתרון עץ זיגזג
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="t"></param>
 /// <returns></returns>
 /// פתרון לטפל בשורש של העץ ולא עץ עלה
 public static bool IsZigZagTree <T>(BinNode <T> t)
 {
     //אם העץ ריק או עץ עלה
     if (t == null || IsLeaf(t))
     {
         return(false);
     }
     //בודקים תת עץ שמאל וגם תת עץ ימין שכל הצמתים חוץ מהשורש מקיימים את התנאים
     return(IsZigZagTree(t.GetLeft(), true) && IsZigZagTree(t.GetRight(), false));
 }
Пример #19
0
 public static void PrintPreorder <T>(BinNode <T> tr)
 { // סריקה תחילית
     if (tr == null)
     {
         return;
     }
     Console.WriteLine(tr.GetValue());
     PrintInorder(tr.GetLeft());
     PrintInorder(tr.GetRight());
 }
Пример #20
0
 public static void PrintPostorder <T>(BinNode <T> tr)
 { // סריקה סופית
     if (tr == null)
     {
         return;
     }
     PrintPostorder(tr.GetLeft());
     PrintPostorder(tr.GetRight());
     Console.WriteLine(tr.GetValue());
 }
Пример #21
0
        // Targil 2

        // Help function to check dead descendants
        private static bool DeadDescendants(BinNode <Patient> patient)
        {
            if (IsLeaf(patient))
            {
                return(false);
            }
            if (patient.HasLeft() &&
                (patient.GetLeft().GetValue().GetDead() || DeadDescendants(patient.GetLeft()))
                )
            {
                return(true);
            }
            if (patient.HasRight() &&
                (patient.GetRight().GetValue().GetDead() || DeadDescendants(patient.GetRight()))
                )
            {
                return(true);
            }
            return(true);
        }
Пример #22
0
 public static bool TRG22 <T>(BinNode <T> tree)
 {
     if (tree == null)
     {
         return(true);
     }
     if ((tree.HasLeft() && !tree.HasRight()) || tree.HasRight() && !tree.HasLeft()) // If there is only 1 child
     {
         return(false);
     }
     return(TRG22(tree.GetRight()) && TRG22(tree.GetLeft()));
 }
Пример #23
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());
     }
 }
Пример #24
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)));
 }
Пример #25
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)));
 }
Пример #26
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)));
 }
Пример #27
0
        public static void PrintInOrderTree(BinNode <int> tree)
        {
            if (tree != null)
            {
                PrintInOrderTree(tree.GetLeft());
                int val = tree.GetValue();
                ///יבוא קוד שאני רוצה לבצע על הצומת.
                Console.Write(val + "--->");

                PrintInOrderTree(tree.GetRight());
            }
        }
Пример #28
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 + " "));
 }
Пример #29
0
        public static int CountNodesInTree <T> (BinNode <T> t)
        {
            if (t == null)
            {
                return(0);
            }
            int current = 1;
            int left    = CountNodesInTree(t.GetLeft());
            int right   = CountNodesInTree(t.GetRight());

            return(current + left + right);
            //return 1+CountNodesInTree(t.GetLeft())+CountNodesInTree(t.GetRight());
        }
Пример #30
0
        public static int BinTreeHight <T> (BinNode <T> t)
        {
            if (t == null)
            {
                return(0);
            }
            if (!t.HasLeft() && !t.HasRight())
            {
                return(0);
            }

            return(1 + Math.Max(BinTreeHight(t.GetLeft()), BinTreeHight(t.GetRight())));
        }