Search


Monday, January 18, 2010

CountWords C# using Regular Expressions (Regex)

   1: protected int WordsCount()
   2: {
   3:     Regex r = new Regex(@"\b\w+\b");
   4:     return r.Matches(textBox1.Text).Count;
   5: }

don’t forget to include using System.Text.RegularExpressions;

Angle between the hr hand and min hand in the clock

 

C# Function that would tell the angle between the hr hand and min hand in the clock

   1: // assume we always have valid numbers for hour and minute
   2: private double CalculateAngle(int h, int m)
   3: {
   4:    // position for minutes
   5:    double totalHourPerDay = 12;
   6:    double totalMinutesPerHour = 60;
   7:    double totalMinutesPerDay = 360;
   8:    double m_pos = m / totalMinutesPerHour * totalMinutesPerDay;
   9:    double h_pos = (h / totalHourPerDay * totalMinutesPerDay) + (m / totalMinutesPerHour * totalMinutesPerDay / totalHourPerDay);
  10:  
  11:    double gap = Math.Abs(m_pos - h_pos);
  12:    
  13:  
  14:    return gap;
  15: }

Design and implement a C# class for a binary tree

 

class Tree : to manage the binary tree. (Insert, delete, and traversing the tree).

   1: public class TreeNode
   2: {
   3:    private TreeNode leftNode; // link to left child
   4:    private int data; // data stored in node
   5:    private TreeNode rightNode; // link to right child
   6:  
   7:    // initialize data and make this a leaf node
   8:    public TreeNode(int nodeData)
   9:    {
  10:        data = nodeData;
  11:        leftNode = rightNode = null; // node has no children
  12:    } // end constructor
  13:  
  14:    // LeftNode property
  15:    public TreeNode LeftNode
  16:    {
  17:        get
  18:        {
  19:            return leftNode;
  20:        } // end get
  21:        set
  22:        {
  23:            leftNode = value;
  24:        } // end set
  25:    } // end property LeftNode
  26:  
  27:    // Data property
  28:    public int Data
  29:    {
  30:        get
  31:        {
  32:            return data;
  33:        } // end get
  34:        set
  35:        {
  36:            data = value;
  37:        } // end set
  38:    } // end property Data
  39:  
  40:    // RightNode property
  41:    public TreeNode RightNode
  42:    {
  43:        get
  44:        {
  45:            return rightNode;
  46:        } // end get
  47:        set
  48:        {
  49:            rightNode = value;
  50:        } // end set
  51:    } // end property RightNode
  52:  
  53:    // insert TreeNode into Tree that contains nodes;
  54:    // ignore duplicate values
  55:    public void Insert(int insertValue)
  56:    {
  57:        if (insertValue < data) // insert in left subtree
  58:        {
  59:            // insert new TreeNode
  60:            if (leftNode == null)
  61:                leftNode = new TreeNode(insertValue);
  62:            else // continue traversing left subtree
  63:                leftNode.Insert(insertValue);
  64:        } // end if
  65:        else if (insertValue > data) // insert in right subtree
  66:        {
  67:            // insert new TreeNode
  68:            if (rightNode == null)
  69:                rightNode = new TreeNode(insertValue);
  70:            else // continue traversing right subtree
  71:                rightNode.Insert(insertValue);
  72:        } // end else if
  73:    } // end method Insert 
  74:  
  75: } 

class TreeNode : to create and assign tree properties: data, Right Node and Left Node

   1: // class Tree declaration
   2: public class Tree
   3: {
   4:    private TreeNode root;
   5:    
   6:    // construct an empty Tree of integers
   7:    public Tree()
   8:    {
   9:        root = null;
  10:        
  11:  
  12:    } // end constructor
  13:  
  14:    // Insert a new node in the binary search tree.
  15:    // If the root node is null, create the root node here.
  16:    // Otherwise, call the insert method of class TreeNode.
  17:    public void InsertNode(int insertValue)
  18:    {
  19:        if (root == null)
  20:            root = new TreeNode(insertValue);
  21:        else
  22:            root.Insert(insertValue);
  23:    } // end method InsertNode
  24:  
  25:    // begin preorder traversal
  26:    public void PreorderTraversal()
  27:    {
  28:        PreorderHelper(root);
  29:    } // end method PreorderTraversal
  30:  
  31:    // recursive method to perform preorder traversal
  32:    private void PreorderHelper(TreeNode node)
  33:    {
  34:        if (node == null)
  35:            return;
  36:  
  37:        // output node data
  38:        Console.Write(node.Data + " ");
  39:  
  40:        // traverse left subtree
  41:        PreorderHelper(node.LeftNode);
  42:  
  43:        // traverse right subtree
  44:        PreorderHelper(node.RightNode);
  45:    } // end method PreorderHelper
  46:  
  47:    // begin inorder traversal
  48:    public void InorderTraversal()
  49:    {
  50:        InorderHelper(root);
  51:    } // end method InorderTraversal
  52:  
  53:    // recursive method to perform inorder traversal
  54:    private void InorderHelper(TreeNode node)
  55:    {
  56:        if (node == null)
  57:            return;
  58:  
  59:        // traverse left subtree
  60:        InorderHelper(node.LeftNode);
  61:  
  62:        // output node data
  63:        Console.Write(node.Data + " ");
  64:  
  65:        // traverse right subtree
  66:        InorderHelper(node.RightNode);
  67:    } // end method InorderHelper
  68:  
  69:    // begin postorder traversal
  70:    public void PostorderTraversal()
  71:    {
  72:        PostorderHelper(root);
  73:    } // end method PostorderTraversal
  74:  
  75:    // recursive method to perform postorder traversal
  76:    private void PostorderHelper(TreeNode node)
  77:    {
  78:        if (node == null)
  79:            return;
  80:  
  81:        // traverse left subtree
  82:        PostorderHelper(node.LeftNode);
  83:  
  84:        // traverse right subtree
  85:        PostorderHelper(node.RightNode);
  86:  
  87:        // output node data
  88:        Console.Write(node.Data + " ");
  89:    } // end method PostorderHelper
  90:  
  91:    // delete node function
  92:    // this will check the root first. 
  93:    // then try to search the delete value in the tree
  94:    // if the node is found, delete the node based on three consideration
  95:    // 1 Deleting a leaf
  96:    // 2 Deleting a node with one child
  97:    // 3 Deleting a node with two children
  98:    public void DeleteNode(int deleteValue)
  99:    {
 100:        if (root == null)
 101:            return;
 102:  
 103:        // check the root data
 104:        // delete root node if its data equals to delete value
 105:        else if (root.Data == deleteValue)
 106:        {   
 107:            if (root.LeftNode == null)
 108:                root = root.RightNode;
 109:            else if (root.RightNode == null)
 110:                root = root.LeftNode;
 111:  
 112:            else if (root.LeftNode != null && root.RightNode != null)
 113:            {
 114:                int data = getPredecessor(root.LeftNode);                    
 115:                delRecNode(root.LeftNode, data);
 116:                root.Data = data;
 117:            }
 118:        }
 119:        else
 120:        {
 121:            delRecNode(root, deleteValue);
 122:        }
 123:  
 124:  
 125:  
 126:    }
 127:    // recursive function to delete node based on value
 128:    private bool delRecNode(TreeNode node, int value)
 129:    {
 130:        if (node == null)
 131:            return false; // do nothing
 132:  
 133:        if (value < node.Data)
 134:        {
 135:            if(delRecNode(node.LeftNode, value))
 136:            {
 137:                // There are several cases to be considered in deletion:
 138:                // case 1 no children
 139:                if (node.LeftNode.LeftNode == null && node.LeftNode.RightNode == null)
 140:                    node.LeftNode = null;
 141:                // case 2 node has 1 child
 142:                else if (node.LeftNode.LeftNode == null)
 143:                    node.LeftNode = node.LeftNode.RightNode;
 144:                else if (node.LeftNode.RightNode == null)
 145:                    node.LeftNode = node.LeftNode.LeftNode;
 146:  
 147:                // case 3 node has 2 children
 148:                else if (node.LeftNode.LeftNode != null && node.LeftNode.RightNode != null)
 149:                {
 150:                    int data = getPredecessor(node.LeftNode.LeftNode);
 151:                    // delete precessor
 152:                    delRecNode(node.LeftNode.LeftNode, data);
 153:                    node.LeftNode.Data = data;
 154:                }
 155:            }
 156:            return false;
 157:  
 158:        }
 159:        else if (value > node.Data)
 160:        {
 161:       
 162:            if(delRecNode(node.RightNode, value))
 163:            {
 164:                // There are several cases to be considered in deletion:
 165:                // case 1 no children
 166:                if (node.RightNode.LeftNode == null && node.RightNode.RightNode == null)
 167:                    node.RightNode = null;
 168:  
 169:                // case 2 node has 1 child
 170:                else if (node.RightNode.LeftNode == null)
 171:                    node.RightNode = node.RightNode.RightNode;
 172:                else if (node.RightNode.RightNode == null)
 173:                    node.RightNode = node.RightNode.LeftNode;
 174:  
 175:                // case 3 node has 2 children
 176:                else if (node.RightNode.LeftNode != null && node.RightNode.RightNode != null)
 177:                {
 178:                    int data = getPredecessor(node.RightNode.LeftNode);
 179:                    // delete precessor
 180:                    delRecNode(node.RightNode.LeftNode, data);
 181:                    node.RightNode.Data = data;
 182:                }
 183:            }
 184:            return false;
 185:  
 186:        }
 187:        else if (value == node.Data)
 188:        {
 189:            return true;
 190:        }
 191:        return false;
 192:  
 193:    }
 194:    // get the value of predecessor
 195:    private int getPredecessor(TreeNode node)
 196:    {
 197:        if (node.RightNode != null)
 198:        {
 199:            return getPredecessor(node.RightNode);
 200:        }
 201:        else
 202:        {
 203:            return node.Data;
 204:        }
 205:        
 206:    }
 207:        
 208: }