Ejemplo n.º 1
0
        //מקבלת עץ ביטוי
        //מחזירה את ערכו כפעולת חשבון
        public static int ExpretionTreeValue(BinTreeNode <char> t)
        {
            if (!IfLeaf(t))
            {
                switch (t.GetInfo())
                {
                case '+': return(ExpretionTreeValue(t.GetLeft()) + ExpretionTreeValue(t.GetRight()));

                    break;

                case '-': return(ExpretionTreeValue(t.GetLeft()) - ExpretionTreeValue(t.GetRight()));

                    break;

                case '*': return(ExpretionTreeValue(t.GetLeft()) * ExpretionTreeValue(t.GetRight()));

                    break;

                case '/': return(ExpretionTreeValue(t.GetLeft()) / ExpretionTreeValue(t.GetRight()));

                    break;

                default: break;
                }
            }
            return(t.GetInfo() - 48);
        }
Ejemplo n.º 2
0
        // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
        // טענת יציאה: הפעולה מחזירה את הערך הגבוה ביותר בעץ
        // סיבוכיות זמן ריצה: O(n)
        public static int MaxValue(BinTreeNode <int> bt)
        {
            if (IsLeaf(bt)) //!
            {
                return(bt.GetInfo());
            }
            int maxLeft  = MaxValue(bt.GetLeft());
            int maxRight = MaxValue(bt.GetRight());

            return(Math.Max(bt.GetInfo(), Math.Max(maxLeft, maxRight)));
        }
Ejemplo n.º 3
0
 //מקבלת עץ חיפוש ומספר
 //מחזירה מצביע למיקום
 //רקורסייה
 public static BinTreeNode <int> Position(BinTreeNode <int> t, int num)
 {
     //if (t != null)
     //{
     if (t.GetInfo() != num)
     {
         if (t.GetInfo() < num)
         {
             return(Position(t.GetRight(), num));
         }
         return(Position(t.GetLeft(), num));
     }
     return(t);
     //}
     //return t;
 }
Ejemplo n.º 4
0
 // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
 // טענת יציאה: הפעולה מחזירה "אמת" אם העץ הוא עץ חיפוש בינארי, אחרת מחזירה "שקר"
 // סיבוכיות זמן ריצה: O(n)
 public static bool IsBST(BinTreeNode <int> bt)
 {
     if (bt == null)
     {
         return(true);
     }
     if (bt.GetLeft() != null && bt.GetLeft().GetInfo() >= bt.GetInfo())
     {
         return(false);
     }
     if (bt.GetRight() != null && bt.GetRight().GetInfo() < bt.GetInfo())
     {
         return(false);
     }
     return(IsBST(bt.GetLeft()) && IsBST(bt.GetRight()));
 }
Ejemplo n.º 5
0
 //מקבלת עץ בינארי
 //מדפיסה את הצמתים שיש להם 2 בנים וערכם גדול מאחד מהם
 public static void BigFatherTwoSuns(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetLeft() != null && t.GetRight() != null)
         {
             if (t.GetInfo() > t.GetLeft().GetInfo() || t.GetInfo() > t.GetRight().GetInfo())
             {
                 Console.WriteLine(t.ToString());
                 //Console.WriteLine(t.GetInfo());
             }
         }
         BigFatherTwoSuns(t.GetLeft());
         BigFatherTwoSuns(t.GetRight());
     }
 }
Ejemplo n.º 6
0
 // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים ומספר שלם
 // טענת יציאה: הפעולה מחזירה "אמת" אם המספר שהתקבל נמצא בעץ, אחרת מחזירה "שקר"
 // סיבוכיות זמן ריצה: O(n)
 public static bool IsExistTree(BinTreeNode <int> bt, int n)
 {
     if (bt == null)
     {
         return(false);
     }
     return(bt.GetInfo() == n || IsExistTree(bt.GetLeft(), n) || IsExistTree(bt.GetRight(), n));
 }
Ejemplo n.º 7
0
 // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
 // טענת יציאה: הפעולה מחזירה את סכום ערכי כל הצמתים בעץ
 // סיבוכיות זמן ריצה: O(n)
 public static int SumTree(BinTreeNode <int> bt)
 {
     if (bt == null)
     {
         return(0);
     }
     return(bt.GetInfo() + SumTree(bt.GetLeft()) + SumTree(bt.GetRight()));
 }
Ejemplo n.º 8
0
 //מקבלת עץ חיפוש
 //מדפיסה אותו בסדר יורד
 public static void DownOrderPrint(BinTreeNode <int> t)
 {
     if (t != null)
     {
         DownOrderPrint(t.GetRight());
         Console.Write(t.GetInfo() + " ");
         DownOrderPrint(t.GetLeft());
     }
 }
Ejemplo n.º 9
0
 // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
 // טענת יציאה: הפעולה מדפיסה את ערכי הצמתים בעץ לפי סדר סופי - שמאל, ימין, שורש
 // סיבוכיות זמן ריצה: O(n)
 public static void PrintPostOrder(BinTreeNode <int> bt)
 {
     if (bt != null)
     {
         PrintPostOrder(bt.GetLeft());
         PrintPostOrder(bt.GetRight());
         Console.WriteLine(bt.GetInfo());
     }
 }
Ejemplo n.º 10
0
 //מקבלת עץ בינארי
 //מחזירה את הצמתים שיש שגדולים לפחות מאחד מבניהם
 public static int SmallFatherOneSuns(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetLeft() != null && t.GetLeft().GetInfo() > t.GetInfo())
         {
             return(1 + SmallFatherOneSuns(t.GetLeft()) + SmallFatherOneSuns(t.GetRight()));
         }
         else
         {
             if (t.GetRight() != null && t.GetRight().GetInfo() > t.GetInfo())
             {
                 return(1 + SmallFatherOneSuns(t.GetLeft()) + SmallFatherOneSuns(t.GetRight()));
             }
         }
         return(SmallFatherOneSuns(t.GetLeft()) + SmallFatherOneSuns(t.GetRight()));
     }
     return(0);
 }
Ejemplo n.º 11
0
        ///

        ///
        /// עץ בינארי
        ///
        // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
        // טענת יציאה: הפעולה מחזירה עץ חדש, זהה בדיוק בצמתיו ובמיקומים שלהם לעץ שהתקבל
        // סיבוכיות זמן ריצה: O(n)
        public static BinTreeNode <int> CloneTree(BinTreeNode <int> bt)
        {
            if (bt == null)
            {
                return(null);
            }
            BinTreeNode <int> left  = CloneTree(bt.GetLeft());
            BinTreeNode <int> right = CloneTree(bt.GetRight());

            return(new BinTreeNode <int>(left, bt.GetInfo(), right)); //להחליף ימין ושמאל לקבלת עץ מראה
        }
Ejemplo n.º 12
0
 //מקבלת עץ
 //מחזירה את סכום ערכי הצמתים
 public static int SumTsomet(BinTreeNode <int> t)
 {
     if (t == null)
     {
         return(0);
     }
     else
     {
         return(t.GetInfo() + SumTsomet(t.GetLeft()) + SumTsomet(t.GetRight()));
     }
 }
Ejemplo n.º 13
0
 //ex 26
 public static bool If8NotExsist(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetInfo() == 8)
         {
             return(false);
         }
         return(If8NotExsist(t.GetLeft()) && If8NotExsist(t.GetRight()));
     }
     return(true);
 }
Ejemplo n.º 14
0
 //מקבל עץ בינארי
 //מחזיר את כמות הצמתים שערכם זוגי
 public static int MoneTsometZugi(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetInfo() % 2 == 0)
         {
             return(1 + MoneTsometZugi(t.GetLeft()) + MoneTsometZugi(t.GetRight()));
         }
         return(MoneTsometZugi(t.GetLeft()) + MoneTsometZugi(t.GetRight()));
     }
     return(0);
 }
Ejemplo n.º 15
0
 //מקבל עץ בינארי ומספר
 //מחזירה את כמות הצמתים שערכם כשל המספר
 public static int MoneNumInTree(BinTreeNode <int> t, int n)
 {
     if (t != null)
     {
         if (t.GetInfo() == n)
         {
             return(1 + MoneNumInTree(t.GetLeft(), n) + MoneNumInTree(t.GetRight(), n));
         }
         return(MoneNumInTree(t.GetLeft(), n) + MoneNumInTree(t.GetRight(), n));
     }
     return(0);
 }
Ejemplo n.º 16
0
 //ex 23
 public static bool IfAllIzugi(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetInfo() % 2 == 0)
         {
             return(false);
         }
         return(IfAllIzugi(t.GetLeft()) && IfAllIzugi(t.GetRight()));
     }
     return(true);
 }
Ejemplo n.º 17
0
 // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים ומספר שלם
 // טענת יציאה: הפעולה מחזירה את מספר הצמתים בהם מופיע הערך שהתקבל
 // סיבוכיות זמן ריצה: O(n)
 public static int HowMany(BinTreeNode <int> bt, int n)
 {
     if (bt == null)
     {
         return(0);
     }
     if (bt.GetInfo() == n)
     {
         return(1 + HowMany(bt.GetLeft(), n) + HowMany(bt.GetRight(), n));
     }
     return(HowMany(bt.GetLeft(), n) + HowMany(bt.GetRight(), n));
 }
Ejemplo n.º 18
0
 //מקבלת עץ בינארי
 //תדפיס את כל הצמתים שערכם נמוך מערך אביהם
 //מדפיסה בסדר סופי: postOrder
 public static void SunLowerThanFather(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetLeft() != null)
         {
             if (t.GetLeft().GetInfo() < t.GetInfo())
             {
                 Console.Write(t.GetLeft().GetInfo() + " ");
             }
             SunLowerThanFather(t.GetLeft());;
         }
         if (t.GetRight() != null)
         {
             if (t.GetRight().GetInfo() < t.GetInfo())
             {
                 Console.Write(t.GetRight().GetInfo() + " ");
             }
             SunLowerThanFather(t.GetRight());
         }
     }
 }
Ejemplo n.º 19
0
 //ex 25
 public static bool IfLeafLikeFather(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (IfLeaf(t.GetLeft()))
         {
             if (t.GetInfo() != t.GetLeft().GetInfo())
             {
                 return(false);
             }
         }
         if (IfLeaf(t.GetRight()))
         {
             if (t.GetInfo() != t.GetLeft().GetInfo())
             {
                 return(false);
             }
         }
         return(IfLeafLikeFather(t.GetRight()) && IfLeafLikeFather(t.GetRight()));
     }
     return(true);
 }
Ejemplo n.º 20
0
        //מקבלת עץ חיפוש ומספר
        //מחזירה מצביע למיקום
        //While
        public static BinTreeNode <int> Position2(BinTreeNode <int> t, int num)
        {
            BinTreeNode <int> temp = t;

            while (temp.GetInfo() != num)
            {
                if (t.GetInfo() < num)
                {
                    temp = t.GetRight();
                }
                temp = t.GetLeft();
            }
            return(temp);
        }
Ejemplo n.º 21
0
 //מקבלת עץ בינארי
 //מחזירה את כמות הצמתים שערכם גבוה מערך אביהם
 public static int SunHigherThanFather(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetLeft() != null && t.GetLeft().GetInfo() > t.GetInfo())
         {
             if (t.GetRight() != null && t.GetRight().GetInfo() > t.GetInfo())
             {
                 return(2 + SunHigherThanFather(t.GetLeft()) + SunHigherThanFather(t.GetRight()));
             }
             return(1 + SunHigherThanFather(t.GetLeft()));
         }
         else
         {
             if (t.GetRight() != null && t.GetRight().GetInfo() > t.GetInfo())
             {
                 return(1 + SunHigherThanFather(t.GetLeft()) + SunHigherThanFather(t.GetRight()));
             }
         }
         return(SunHigherThanFather(t.GetLeft()) + SunHigherThanFather(t.GetRight()));
     }
     return(0);
 }
Ejemplo n.º 22
0
 //מקבלת עץ בינארי
 //מחזירה את סכום ערכי עליו
 public static int SumLeafs(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (IfLeaf(t))
         {
             return(t.GetInfo());
         }
         else
         {
             return(SumLeafs(t.GetLeft()) + SumLeafs(t.GetRight()));
         }
     }
     return(0);
 }
Ejemplo n.º 23
0
 //ex 28
 public static bool LeftBigRightSmall(BinTreeNode <int> t)
 {
     if (t != null)
     {
         if (t.GetLeft() != null && t.GetRight() != null)
         {
             if (t.GetLeft().GetInfo() <= t.GetInfo() || t.GetRight().GetInfo() >= t.GetInfo())
             {
                 return(false);
             }
         }
         return(LeftBigRightSmall(t.GetLeft()) && LeftBigRightSmall(t.GetRight()));
     }
     return(true);
 }
Ejemplo n.º 24
0
        // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
        // טענת יציאה: הפעולה מדפיסה את ערכי הצמתים בעץ לפי הרמה שלהם - כל צמתי העץ ברמה כלשהי יודפסו אחד אחרי השני
        // סיבוכיות זמן ריצה: O(n)
        public static void PrintByLevels(BinTreeNode <int> bt)
        {
            Queue <BinTreeNode <int> > q = new Queue <BinTreeNode <int> >();

            q.Insert(bt);
            while (!q.IsEmpty())
            {
                BinTreeNode <int> current = q.Remove();
                Console.WriteLine(current.GetInfo());
                if (current.GetLeft() != null)
                {
                    q.Insert(current.GetLeft());
                }
                if (current.GetRight() != null)
                {
                    q.Insert(current.GetRight());
                }
            }
        }
Ejemplo n.º 25
0
        //מקבלת עץ בינארי
        //מדפיסה את העץ בצורת רמות
        public static void LevelOrderPrint(BinTreeNode <int> t)
        {
            Queue <BinTreeNode <int> > q = new Queue <BinTreeNode <int> >();

            q.Insert(t);
            while (!q.IsEmpty())
            {
                BinTreeNode <int> temp = q.Remove();
                Console.Write(temp.GetInfo() + " ");
                if (temp.GetLeft() != null)
                {
                    q.Insert(temp.GetLeft());
                }

                if (temp.GetRight() != null)
                {
                    q.Insert(temp.GetRight());
                }
            }
        }