Exemplo n.º 1
0
 public static void FillTree(NodeTree root, IEnumerable <int> values)
 {
     foreach (var value in values)
     {
         InsertValue(root, value);
     }
 }
Exemplo n.º 2
0
        /* 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);
        }
Exemplo n.º 3
0
 //@deepalgo
 public static int MyWeight(NodeTree root)
 {
     if (root == null)
     {
         return(0);
     }
     return(1 + Math.Max(MyWeight(root.Left), MyWeight(root.Right)));
 }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 5
0
 //@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));
 }
Exemplo n.º 6
0
        //@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);
        }
Exemplo n.º 7
0
 //@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);
 }
Exemplo n.º 8
0
 //@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);
     }
 }
Exemplo n.º 9
0
 //@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);
 }
Exemplo n.º 10
0
        /*
         * 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);
        }