Exemple #1
0
 static void Bottom_View(Node root)
 {
     var DistMap = new Dictionary<int, int>();
     int minKey = 0;
     int maxKey = 0;
     Bottom_View(root, ref DistMap, 0, ref minKey, ref maxKey);
     for (int i = minKey; i <= maxKey; i++)
     {
         var data = 0;
         if (DistMap.TryGetValue(i, out data))
         {
             Console.Write(" " + data);
         }
     }
 }
Exemple #2
0
 public void AddNode(Node node, Node root)
 {
     while (true)
     {
         if (node.Data >= root.Data)
         {
             if (root.Right == null)
             {
                 root.Right = node;
                 return;
             }
             root = root.Right;
         }
         else
         {
             if (root.Left == null)
             {
                 root.Left = node;
                 return;
             }
             root = root.Left;
         }
     }
 }
Exemple #3
0
 public void AddNode(int data)
 {
     Node node = new Node() { Data = data, Left = null, Right = null };
     this.AddNode(node, this.Root);
 }
Exemple #4
0
        static string TopView(Node root, int level, int right, int left, string a)
        {
            if (left == 1)
            {
                a = root.Data + " " + a;
                if (root.Left != null)
                {
                    return TopView(root.Left, level + 1, 0, 1, a);
                }
                return a;
            }

            if (right == 1)
            {
                a = a + " " + root.Data;
                if (root.Right != null)
                {
                    return TopView(root.Right, level + 1, 1, 0, a);
                }
                return a;
            }

            if (level == 0)
            {
                string left_string = a + " " + root.Data;
                string right_string = a;
                return (TopView(root.Left, level + 1, 0, 1, left_string) + " " + TopView(root.Right, level + 1, 1, 0, right_string));
            }

            return String.Empty;
        }
Exemple #5
0
 public BinaryTree(int data)
 {
     this.Root = new Node() { Data = data, Left = null, Right = null };
 }
Exemple #6
0
        static void Left_view(Node Root, int level, ref int maxLevel, ref string res)
        {
            if (Root == null)
                return;

            if (level > maxLevel)
            {
                res = Root.Data + " " + res;
                maxLevel = level;
            }

            Left_view(Root.Left, level + 1, ref maxLevel, ref res);
            Left_view(Root.Right, level + 1, ref maxLevel, ref res);
        }
Exemple #7
0
 static void Left_view(Node Root)
 {
     int maxLevel = 0;
     var res = string.Empty;
     ////var lst = new Stack<int>();
     Left_view(Root, 1, ref maxLevel, ref res);
     Console.Write(res);
 }
Exemple #8
0
        static void Bottom_View(Node root, ref Dictionary<int, int> DistMap, int dist, ref int minKey, ref int maxKey)
        {
            if (root == null)
                return;

            int val;
            if (DistMap.TryGetValue(dist, out val))
            {
                DistMap[dist] = root.Data;
            }
            else
            {
                DistMap.Add(dist, root.Data);
            }

            minKey = (dist < minKey) ? dist : minKey;
            maxKey = (dist > maxKey) ? dist : maxKey;

            Bottom_View(root.Left, ref DistMap, dist-1, ref minKey, ref maxKey);
            Bottom_View(root.Right, ref DistMap, dist+1, ref minKey, ref maxKey);
        }