Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

print the top view of the binary tree

void TopView(Node root)
{
    Queue q = new Queue();
    SortedDictionary<int, Node> topViewMap
      = new SortedDictionary<int, Node>();

    if (root == null) {
      return;
    }
    else {
      q.Enqueue(new QueueObj(root, 0));
    }

    // count function returns 1 if the container
    // contains an element whose key is equivalent
    // to hd, or returns zero otherwise.
    while (q.Count != 0) {
      QueueObj tmpNode = (QueueObj)q.Dequeue();

      if (!topViewMap.ContainsKey(tmpNode.hd)) {
        topViewMap[tmpNode.hd] = tmpNode.node;
      }

      if (tmpNode.node.left != null) {
        q.Enqueue(new QueueObj(tmpNode.node.left,
                               tmpNode.hd - 1));
      }
      if (tmpNode.node.right != null) {
        q.Enqueue(new QueueObj(tmpNode.node.right,
                               tmpNode.hd + 1));
      }
    }

    foreach(var entry in topViewMap.Values)
    {
      Console.Write(entry.data);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: IOS app crashing on ios 15 unity 
Csharp :: string extentions not working 
Csharp :: Unity search all chidren of parent object 
Csharp :: Initalize C# project in VS Code 
Csharp :: design pattern for so many conditions c# 
Csharp :: c# read key without writing 
Csharp :: Delegate parameter no return 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: what is vector3.one c# 
Csharp :: closing main window after clicking on a button that opens another window in wpf 
Csharp :: custom convert list object to other object c# 
Csharp :: c# custom comment tags 
Csharp :: replace bar c# 
Csharp :: card caption 
Csharp :: c# download to string 
Csharp :: Here we create a MigraDoc Document object to draw the content of this page 
Csharp :: how to get user control dropdown value in asp net 
Csharp :: how to make dobuble jump unity 2d 
Csharp :: c# place all keys in dictionary into array 
Csharp :: how can i only show just a part of alist in datagridview in c# 
Csharp :: create shortcut C# WPF 
Csharp :: c# ensure static constructor is called 
Csharp :: subarray c# 
Csharp :: boucle C# 
Csharp :: grass download for unityh 
Csharp :: .net core get exe path 
Csharp :: how to define a static color resource in xaml wpf 
Csharp :: C# .net JwtSecurityTokenHandler jwttoken claims to object 
Csharp :: button Previous for picturebox c# 
Csharp :: unity bool to int 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =