示例#1
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 public static void FillTree(NodeTree root, IEnumerable <int> values)
 {
     foreach (var value in values)
     {
         InsertValue(root, value);
     }
 }
示例#2
0
文件: Algo.cs 项目: Leymariv/AlgosCS
        /* Breadth First Search */
        //@deepalgo
        public static IEnumerable <int> RootToHorizontalList(NodeTree root)
        {
            if (root == null)
            {
                return(null);
            }

            var list = new List <int>();

            list.Add(root.Value);

            var queue = new Queue <NodeTree>();

            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                if (current.Left != null)
                {
                    list.Add(current.Left.Value);
                    queue.Enqueue(current.Left);
                }
                if (current.Right != null)
                {
                    list.Add(current.Right.Value);
                    queue.Enqueue(current.Right);
                }
            }
            return(list);
        }
示例#3
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 //@deepalgo
 public static int MyWeight(NodeTree root)
 {
     if (root == null)
     {
         return(0);
     }
     return(1 + Math.Max(MyWeight(root.Left), MyWeight(root.Right)));
 }
示例#4
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 public static void RootToList2(NodeTree root, Collection <int> list)
 {
     if (root == null)
     {
         return;
     }
     RootToList2(root.Left, list);
     RootToList2(root.Right, list);
     list.Add(root.Value);
 }
示例#5
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 //@deepalgo
 public static bool TreeContainsRec(NodeTree root, int value)
 {
     if (root == null)
     {
         return(false);
     }
     if (root.Value == value)
     {
         return(true);
     }
     return(TreeContainsRec(value > root.Value ? root.Right : root.Left, value));
 }
示例#6
0
文件: Algo.cs 项目: Leymariv/AlgosCS
        //@deepalgo
        public static bool TreeContainsIt(NodeTree root, int value)
        {
            var current = root;

            while (current != null)
            {
                if (value == current.Value)
                {
                    return(true);
                }
                current = value > current.Value ? current.Right : current.Left;
            }
            return(false);
        }
示例#7
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 //@deepalgo
 public static bool IsBalancedRec(NodeTree root)
 {
     if (root != null)
     {
         var left  = MyWeight(root.Left);
         var right = MyWeight(root.Right);
         if (Math.Abs(left - right) > 1)
         {
             return(false);
         }
         IsBalancedRec(root.Left);
         IsBalancedRec(root.Right);
     }
     return(true);
 }
示例#8
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 //@deepalgo
 public static IEnumerable <int> RootToList(NodeTree root)
 {
     if (root != null)
     {
         foreach (var i in RootToList(root.Left))
         {
             yield return(i);
         }
         foreach (var i in RootToList(root.Right))
         {
             yield return(i);
         }
         yield return(root.Value);
     }
 }
示例#9
0
文件: Algo.cs 项目: Leymariv/AlgosCS
 //@deepalgo
 public static NodeTree InsertValue(NodeTree root, int value)
 {
     if (root == null)
     {
         return(new NodeTree(value, null, null));
     }
     if (value < root.Value)
     {
         root.Left = InsertValue(root.Left, value);
     }
     else
     {
         root.Right = InsertValue(root.Right, value);
     }
     return(root);
 }
示例#10
0
文件: Algo.cs 项目: Leymariv/AlgosCS
        /*
         * 1 2 3 4 5 6 7 8
         * ======>
         *       5
         *    3    7
         *   2 4  6 8
         *  1
         *
         */

        //@deepalgo
        public static NodeTree BuildBalancedTree(IEnumerable <int> list)
        {
            if (list == null || !list.Any())
            {
                return(null);
            }
            int        two         = 2;
            var        indexMid    = list.Count() / two;
            var        newRoot     = new NodeTree(list.ElementAt(indexMid), null, null);
            List <int> subListLeft = list.Take(indexMid).ToList();

            newRoot.Left = BuildBalancedTree(subListLeft);
            var        lastindex         = list.Count();
            var        countFromIndexMid = lastindex - (indexMid + 1);
            List <int> subListRight      = list.Skip(indexMid + 1).Take(countFromIndexMid).ToList();

            newRoot.Right = BuildBalancedTree(subListRight);
            return(newRoot);
        }