Wednesday, 29 February 2012

suffix Tree using Treeview and linkedlist in c#.net


                                           Design form of suffix tree

Code of the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SuffixTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Build_Node_Click(object sender, EventArgs e)

        {
           
            // stores the string of input
            LinkedList<string> sentence = new LinkedList<string>();
            LinkedList<string> match_list = new LinkedList<string>();
            //string into chararray and reverse the char and convert into string
            char[] wordarr = txtinput_string.Text.ToCharArray();
            Array.Reverse(wordarr);
            string wordstring = new string(wordarr);
            for (int i = 0; i < wordstring.Length; i++)
            {
                if (i == 0)
                {
                    // re-reverse the string to original form
                    char[] wordarr2 = wordstring.ToCharArray();
                    Array.Reverse(wordarr2);
                    string wordstring2 = new string(wordarr2);
                    //insert or add to the linkedlist
                    sentence.AddFirst(wordstring2);
                }
                else
                {
                    //remove the char one by one
                    string wordtoadd = wordstring.Remove(wordstring.Length - i);
                    // re-reverse the string to original form
                    char[] wordarr2 = wordtoadd.ToCharArray();
                    Array.Reverse(wordarr2);
                    string wordstring2 = new string(wordarr2);
                    //insert or add the string
                    sentence.AddLast(wordstring2);
                }
                            }
                      //creating Tree of nodes in the linkedlist of sentence(stores the input string)
            for (int r = 0; r < sentence.Count; r++)
            {
                TreeNode node_1 = new TreeNode(sentence.ElementAt(r).ToString());
                treeView1.Nodes.Add(node_1);
            }
             for (int i = 0; i < sentence.Count; i++)
            {
               char[] list_one = sentence.ElementAt(i).ToCharArray();
               for (int j = i + 1; j < sentence.Count; j++)
                {
                    char[] list_two = sentence.ElementAt(j).ToCharArray();
                    int a = 0;
                    if ( a < 1)
                    {
                        if (list_one.ElementAt(a).ToString() != list_two.ElementAt(a).ToString())
                        {
                       }
                        else
                        {
                            if (match_list.Count == 0)
                            {
                                match_list.AddLast(sentence.ElementAt(i).ToString());
                                // sentence.Remove(sentence.ElementAt(i).ToString());
                            }
                            if(sentence.ElementAt(j).Length>0)
                            {
                            match_list.AddLast(sentence.ElementAt(j).ToString());
                            sentence.Remove(sentence.ElementAt(j).ToString());
                            j--;
                            }
                            }
                           }
                         }
                if(match_list.Count!=0)
                {
                    sentence.Remove(sentence.ElementAt(i).ToString());
                    i = -1;
                    int r = 0,s=1,t=0;
                    int flag = 0;
                    string char11 = null;
                    string char12 = null;
                      while(flag==0)
                    {
                        char[] char1 = match_list.ElementAt(r).ToCharArray();
                                                 char11 = char1.ElementAt(t).ToString();
                                              if (match_list.Count > s )
                        {
                            char[] char2 = match_list.ElementAt(s).ToCharArray();
                            if (char2.Length == 1)
                            {
                                char12 = char2.ElementAt(0).ToString();
                            }
                            else {
                                char12 = char2.ElementAt(t).ToString();
                            }
                            if (char11 == char12)
                            {
                                 s++;
                                 }
                            else
                            {
                                flag = 1;
                              }
                        }
                        else {
                            r = 0; s = 1; t++;
                        }
                                            }
                    int t_value = t;
                    for (int u = 0; u < match_list.Count; u++)
                    {
                        string char_string = null;
                        char[] char_new = match_list.ElementAt(u).ToCharArray();
                        int k = 0;
                        if(t!=0)
                        {
                            if (t == 1)
                            {
                                char_string = char_string + char_new.ElementAt(0);
                                t--; }
                            while(t>k)
                            {
                                  if (char_new.Length != 1)
                                {
                                     char_string = char_string + char_new.ElementAt(t);
                                        t--;
                                     }
                                                        }
                        TreeNode Tree_Nn = new TreeNode(char_string);
                        treeView1.Nodes.Add(Tree_Nn);
                        char_string=null;
                        k = t_value;
                             while (k < char_new.Length)
                                                                  {
                                                   char_string = char_string + char_new.ElementAt(k).ToString();
                                     k++;
                                 }
                                 TreeNode Tree_branch = new TreeNode(char_string);
                                 Tree_Nn.Nodes.Add(char_string);
                        }  else
                            {
                                if (u == 0)
                                {
                                    char_string = char_new.ElementAt(u).ToString();
                                    TreeNode Tree_Nn = new TreeNode(char_string);
                                    treeView1.Nodes.Add(Tree_Nn);
                                                                    }
                                         char_string=null;
                                 k = t_value;
                                 while (k < char_new.Length)
                                 {
                                     char_string = char_string + char_new.ElementAt(k).ToString();
                                     k++;
                                 }
                                 TreeNode Tree_branch = new TreeNode(char_string);
                                 int count = treeView1.Nodes.Count;
                                 treeView1.Nodes[count-1].Nodes.Add(Tree_branch);
                            }
                                  }
                    }
                match_list.Clear();
                    }
                        }
                                }
                              }
Output example:


           Example 2nd:


       

      
   

Suffix Tree Theory