Wednesday, January 9, 2019

Save all leaf nodes of a Binary tree in a Doubly Linked List by using Right node as Next node and Left Node as Previous Node.

Sample tree

1
/ \
      2   3
     /\    /\
   4  5  6 7

Sample output 4 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7

c# code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BinaryTreeNode b = new BinaryTreeNode();
            b.Data = 1;
            b.Left = new BinaryTreeNode(){Data = 2, Left = new BinaryTreeNode(){Data = 4}, Right = new BinaryTreeNode(){Data = 5}};
            b.Right = new BinaryTreeNode(){Data = 3, Left = new BinaryTreeNode(){Data = 6}, Right = new BinaryTreeNode(){Data = 7}};
            LinkedListNode ll = TreeToLinkedList(b);
            var temp = ll;
            while (temp.Previous != null)
            {
                temp = temp.Previous;
            }

            while (temp != null)
            {
                Console.Write(temp.Data);
                Console.Write(" -> ");
                temp = temp.Next;
            }
        }

        static LinkedListNode TreeToLinkedList(BinaryTreeNode node)
        {
            if (node == null) return null;
            var linkedListNode = new LinkedListNode {Data = node.Data};
            var leftLinkedList = TreeToLinkedList(node.Left);
            var rightLinkedList = TreeToLinkedList(node.Right);
            //Travel to left most node
            var temp = leftLinkedList;
            if (temp?.Next != null)
            {
                temp = temp.Next;
            }

            linkedListNode.Previous = temp;
            //Travel to right most node
            temp = rightLinkedList;
            if (temp?.Previous != null)
            {
                temp = temp.Previous;
            }

            linkedListNode.Next = temp;
            if (linkedListNode.Previous != null)
            {
                linkedListNode.Previous.Next = linkedListNode;
            }

            if (linkedListNode.Next != null)
            {
                linkedListNode.Next.Previous = linkedListNode;
            }
            return linkedListNode;
        }
    }

    class BinaryTreeNode
    {
        public int Data { get; set; }
        public BinaryTreeNode Left { get; set; }
        public BinaryTreeNode Right { get; set; }
    }

    class LinkedListNode
    {
        public int Data;
        public LinkedListNode Previous { get; set; }
        public LinkedListNode Next { get; set; }
    }
}

Thursday, October 22, 2015

Escape & in ribbon.xml vsto

use
"&&"
"&&"
to show & in ribbon.xml

Tuesday, February 5, 2013

Thursday, January 21, 2010

Writing a string into text file - java

PrintWriter out = new PrintWriter(new FileWriter(outFile));
out.print(txt);
out.close();

Monday, December 28, 2009

Eclipse run configuration for appengine python project



Main Module: C:\Program Files\Google\google_appengine\dev_appserver.py
Arguments: "${project_loc}/src"
--port=8080

Saturday, June 13, 2009

FTP User Isolation in IIS 6.0

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/b63de8ef-e3c5-456d-a8ca-7af4198819d4.mspx?mfr=true

Zipping large file with winrar

It took around 6 hours to zip 21.5 GB file and resulting rar file size is 4GB. It took around 18 minutes to extract zipped file. The file is a SQL server database backup.

Thursday, May 7, 2009

Thursday, February 19, 2009

Converting file length into User friendly format (GB, MB, KB, Bytes)

Use following function to convert file length in bytes to user friendly format


private string FormatByteSize(long length)
{
const int KB = 1024;
const int MB = 1024 * KB;
const int GB = 1024 * MB;
if (length > GB)
{
return (length / GB).ToString("0.00") + " GB";
}
else if (length > MB)
{
return (length / MB).ToString("0.00") + " MB";
}
else if (length > KB)
{
return (length / KB).ToString("0.00") + " KB";
}
return length.ToString()+" Bytes";
}

Thursday, February 12, 2009

Silverlight Calculator

Calculator built using silverlight


Source Code can be found here

Silverlight Error handling at user control level

When you are developing user control in Silverlight you might want to handle all errors for that user control at one place. For handling all errors at one place you have to subscribe to event
UnhandledException of the App class
Code
  public partial class Page : UserControl
{

public Page()

{

InitializeComponent();

App.Current.UnhandledException += new EventHandler(Current_UnhandledException);



}



void Current_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

{

//handle exception here. like showing a message box

}



}


Tuesday, January 20, 2009

RAM vs ROM

A good article explaining difference between RAM & ROM http://blogs.msdn.com/windowsmobile/archive/2005/08/19/453784.aspx

Main differences are (taken from article)
RAM: very fast, but burns a lot of power.
ROM: much slower, but burns very little power.
Importantly, RAM needs a constant supply of power for it to remember its data. ROM doesn't. In other words, if your batteries die, you'll lose the stuff in RAM, but the stuff in ROM will still be there.

Monday, December 22, 2008

Write C Program using Visual Studio .net

Please see below link to write a Hello World application in C using visual studio
http://support.microsoft.com/kb/829488

Friday, October 31, 2008

Force a website to use WWW using IIS

Some times you need to redirect all request to http://example.com/ to http://www.example.com/ . To achieve this you can write code to see if request is for http://example.com/ and redirect to http://www.example.com/ . We can also achieve this using just IIS (i think it will be faster than Code base solution) taking advantage of host headers. Add hostheader http://www.example.com/ to your website (see how to this in this link http://www.visualwin.com/host-header/ ) . Now create another new website with exact same settings as your first website exept host header value it should be example.com. Now right click your new website in IIS select properites and goto Home directory tab. select "A redirection to URL" radio button, in the "Redirect to" text box enter http://www.example.com/ . See the screen shot below for more details

Monday, September 29, 2008

Embedding Javascript file in custom control

  1. Add javascript file to your project
  2. Set Build Action of this file Embeded Resource
  3. Add the following entry into your AssemblyInfo.cs
    [assembly: WebResource("YourNameSpace.Filename.js", "text/javascript")]
  4. Add following code in your OnInit event

    string webresourceURL = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourNameSpace.Filename.js");
    this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "cctextbox",webresourceURL);
  5. Please remember YourNameSpace is base name space in your project