Advertisement
Guest User

Blackone

a guest
Nov 10th, 2009
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. // Jeffrey Cartagena Alicea
  2. //
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace SternNumberSystem
  9. {
  10.     class MainClass
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             try
  15.             {
  16.                 StreamReader inFile = new StreamReader("input.in");
  17.                 StreamWriter outFile = new StreamWriter("output.out");
  18.                 BinarySearchTree tree = new BinarySearchTree();
  19.                 MatchCollection matches = Regex.Matches(inFile.ReadLine(), @"\d+");
  20.                 int i, j;
  21.                 //int a = 1, b = 1;
  22.                
  23.                 ResizeTree(100, 100, tree);
  24.                
  25.                 if(matches.Count != 2)
  26.                     throw new Exception("Invalid Input");
  27.                
  28.                 i = Convert.ToInt32(matches[0].ToString());
  29.                 j = Convert.ToInt32(matches[1].ToString());                            
  30.                      
  31.                
  32.                 while(i != j)
  33.                 {
  34.                     if(!tree.Search(i, j))
  35.                     {
  36.                         tree.RemoveAllNodes();
  37.                         ResizeTree(i, j, tree);
  38.                         tree.ResetRoute();
  39.                         tree.Search(i,j);
  40.                     }
  41.                    
  42.                     outFile.WriteLine(tree.Result());
  43.                     matches = Regex.Matches(inFile.ReadLine(), @"\d{1,5}");
  44.                     if(matches.Count != 2)
  45.                         break;
  46.                     tree.ResetRoute();
  47.                     i = Convert.ToInt32(matches[0].ToString());
  48.                     j = Convert.ToInt32(matches[1].ToString());
  49.                 }  
  50.                 Console.Write("Please press enter");
  51.                 Console.Read();
  52.                 inFile.Close();
  53.                 outFile.Close();               
  54.             }
  55.             catch(System.IO.FileNotFoundException)
  56.             {
  57.                 Console.WriteLine("Input File does not exist\n");
  58.             }
  59.         }
  60.        
  61.         static public void ResizeTree(int num, int den, BinarySearchTree tree)
  62.         {
  63.             for(int i=1; i <= num; i++)
  64.                 for(int j=1; j <= den; j++)
  65.                     tree.Insert(i, j);
  66.         }
  67.     }
  68.    
  69.     public class BinarySearchTree
  70.     {
  71.         private TreeNode root;
  72.         private String route;
  73.        
  74.         public bool Search(int num, int den)
  75.         {
  76.             return PrintSearch(root, num, den);
  77.         }
  78.        
  79.         // Search the value in the tree
  80.         private bool PrintSearch(TreeNode node, int num, int den)
  81.         {
  82.             float nodeValue, val;
  83.            
  84.             if(node == null)
  85.                 return false;
  86.             else
  87.             {
  88.                 nodeValue = node.numerator/(float)node.denominator;
  89.                 val = num/(float)den;
  90.                
  91.                 if(nodeValue == val)
  92.                     return true;
  93.                 else if(val < nodeValue)
  94.                 {
  95.                     route += "L";
  96.                     PrintSearch(node.left, num, den);
  97.                 }
  98.                 else
  99.                 {
  100.                     route += "R";
  101.                     PrintSearch(node.right, num, den);
  102.                 }
  103.             }
  104.             return true;
  105.                
  106.         }
  107.        
  108.         // Insert a new value in the tree
  109.         public void Insert(int num, int den)
  110.         {
  111.             TreeNode newNode = new TreeNode(num, den);
  112.             TreeNode parent = null;
  113.            
  114.             if(IsEmpty())
  115.                 root = newNode;
  116.            
  117.             else if(num == den && num != 1 && den != 1)
  118.                 return;
  119.            
  120.             else
  121.             {
  122.                 TreeNode current = root;
  123.                 float nodeValue, currentValue;
  124.                
  125.                 while(current != null)
  126.                 {
  127.                     parent = current;
  128.                     nodeValue = (float)newNode.numerator/newNode.denominator;
  129.                     currentValue = (float)current.numerator/current.denominator;
  130.                    
  131.                     if(nodeValue == currentValue)
  132.                         return;
  133.                     else if(nodeValue > currentValue)
  134.                         current = current.right;
  135.                     else
  136.                         current = current.left;
  137.                 }
  138.                
  139.                 nodeValue = (float)newNode.numerator/newNode.denominator;
  140.                 currentValue = (float)parent.numerator/parent.denominator;
  141.                
  142.                 if(nodeValue < currentValue)
  143.                     parent.left = newNode;
  144.                 else
  145.                     parent.right = newNode;
  146.             }
  147.         }      
  148.        
  149.        
  150.         public bool IsEmpty()
  151.         {
  152.             return root == null;
  153.         }  
  154.        
  155.         // Default Constructor
  156.         public BinarySearchTree()
  157.         {
  158.             root = null;
  159.             route = "";
  160.         }
  161.        
  162.         // Returns the Result
  163.         public String Result()
  164.         {
  165.             return route;
  166.         }
  167.        
  168.         // Reset the route to null
  169.         public void ResetRoute()
  170.         {
  171.             route = "";
  172.         }
  173.        
  174.         public void RemoveAllNodes()
  175.         {
  176.             RemoveAllNodes(root);
  177.             root = null;
  178.         }
  179.        
  180.         private void RemoveAllNodes(TreeNode node)
  181.         {
  182.             if(node != null)
  183.             {
  184.                 RemoveAllNodes(node.left);
  185.                 RemoveAllNodes(node.right);
  186.                 node = null;
  187.                 route = "";
  188.             }
  189.         }
  190.        
  191.        
  192.     }
  193.    
  194.     public class TreeNode
  195.     {
  196.         public TreeNode left;
  197.         public TreeNode right;
  198.         public int numerator, denominator;
  199.  
  200.         // Parameterized Constructor
  201.         public TreeNode(int num, int den)
  202.         {
  203.             left = null;
  204.             right = null;
  205.             numerator = num;
  206.             denominator = den;
  207.         }
  208.     }  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement