Click here to Skip to main content
15,919,931 members
Home / Discussions / C#
   

C#

 
GeneralRe: inserting string into string Pin
carbon_golem29-Apr-08 3:58
carbon_golem29-Apr-08 3:58 
GeneralRe: inserting string into string Pin
carbon_golem29-Apr-08 3:59
carbon_golem29-Apr-08 3:59 
QuestionC# TextBox with forward slash Pin
WaleedH29-Apr-08 2:52
WaleedH29-Apr-08 2:52 
GeneralRe: C# TextBox with forward slash Pin
Bert delaVega29-Apr-08 4:00
Bert delaVega29-Apr-08 4:00 
GeneralRe: C# TextBox with forward slash [modified] Pin
WaleedH1-May-08 1:52
WaleedH1-May-08 1:52 
QuestionThe best overload method match system.IComparable has invalid arguments Pin
ADTC#29-Apr-08 2:15
ADTC#29-Apr-08 2:15 
when i am trying to insert i the patient detail in the BINARY SEARCH TREE it give me the following error "The best overload method for IBSTree.Insert(System.Icomparable) has some invalid arguments".

<br />
//This is the interface<br />
using System;<br />
using System.Collections;<br />
<br />
<br />
    //Invariants: FirstName, LastName,StreetName and suburb sre strings of chars and<br />
    //            their length > 0 and street no > 0 and age > 0<br />
<br />
    public interface IPatient<br />
    {<br />
        //Pre: true<br />
        //Post: return the first name of the patient <br />
        string FirstName { get; set; }<br />
<br />
        //Pre: true<br />
        //Post: return the last name of the patient <br />
        //string LastName { get; set; }<br />
<br />
    }<br />
<br />
<br />
//here is the IPatient implementation<br />
using System;<br />
using System.Collections;<br />
<br />
<br />
<br />
    public class Patient: IPatient<br />
    {<br />
        private string fname;<br />
                <br />
        public string FirstName<br />
        {<br />
            get<br />
            {<br />
                return fname;<br />
            }<br />
            set<br />
            {<br />
                fname = value;<br />
            }<br />
        }<br />
        public int CompareTo(IPatient obj)<br />
        {<br />
            Patient p1 = (Patient)obj; <br />
            return fname.CompareTo(p1.fname);<br />
        } <br />
  }<br />
<br />
//trying to store the details of the patient in Binary Search Tree<br />
using System;<br />
using System.Collections;<br />
<br />
    public class BTreeNode<br />
    {<br />
        private IComparable item; // value<br />
        private BTreeNode lchild; // reference to its left child <br />
        private BTreeNode rchild; // reference to its right child<br />
<br />
        public BTreeNode(IComparable item)<br />
        {<br />
            this.item = item;<br />
            lchild = null;<br />
            rchild = null;<br />
        }<br />
<br />
        public IComparable Item<br />
        {<br />
            get { return item; }<br />
            set { item = value; }<br />
        }<br />
<br />
        public BTreeNode LChild<br />
        {<br />
            get { return lchild; }<br />
            set { lchild = value; }<br />
        }<br />
<br />
        public BTreeNode RChild<br />
        {<br />
            get { return rchild; }<br />
            set { rchild = value; }<br />
        }<br />
    }<br />
<br />
<br />
    public class BSTree : IBSTree<br />
    {<br />
        private BTreeNode root;<br />
        public BSTree()<br />
        {<br />
            root = null;<br />
        }<br />
<br />
        public bool IsEmpty()<br />
        {<br />
            return root == null;<br />
        }<br />
<br />
        public bool Search(IComparable item)<br />
        {<br />
            return Search(item, root);<br />
        }<br />
<br />
        private bool Search(IComparable item, BTreeNode r)<br />
        {<br />
            if (r != null)<br />
            {<br />
                if (item.CompareTo(r.Item) == 0)<br />
                    return true;<br />
                else<br />
                    if (item.CompareTo(r.Item) < 0)<br />
                        return Search(item, r.LChild);<br />
                    else<br />
                        return Search(item, r.RChild);<br />
            }<br />
            else<br />
                return false;<br />
        }<br />
<br />
<br />
        // pre: item != null, it might be incomplete, but must contain the key<br />
        // post: return item if item is in the binary search tree;<br />
        //	     otherwise, return null.<br />
        public IComparable PartialSearch(IComparable item)<br />
        {<br />
            return PartialSearch(item, root);<br />
        }<br />
<br />
        private IComparable PartialSearch(IComparable item, BTreeNode r)<br />
        {<br />
            if (r != null)<br />
            {<br />
                if (item.CompareTo(r.Item) == 0)<br />
                    return r.Item;<br />
                else<br />
                    if (item.CompareTo(r.Item) < 0)<br />
                        return PartialSearch(item, r.LChild);<br />
                    else<br />
                        return PartialSearch(item, r.RChild);<br />
            }<br />
            else<br />
                return null;<br />
        }<br />
<br />
<br />
        public void Insert(IComparable item)<br />
        {<br />
            if (root == null)<br />
            {<br />
                root = new BTreeNode(item);<br />
<br />
            }<br />
            else<br />
                Insert(item, root);<br />
        }<br />
<br />
        // pre: ptr != null<br />
        // post: item is inserted to the binary search tree rooted at ptr<br />
        private void Insert(IComparable item, BTreeNode ptr)<br />
        {<br />
            if (item.CompareTo(ptr.Item) < 0)<br />
            {<br />
                if (ptr.LChild == null)<br />
                {<br />
                    ptr.LChild = new BTreeNode(item);<br />
<br />
                }<br />
                else<br />
                    Insert(item, ptr.LChild);<br />
            }<br />
            else<br />
            {<br />
                if (ptr.RChild == null)<br />
                {<br />
                    ptr.RChild = new BTreeNode(item);<br />
<br />
                }<br />
                else<br />
                    Insert(item, ptr.RChild);<br />
            }<br />
        }<br />
<br />
        // there are three cases to consider:<br />
        // 1. the node to be deleted is a leaf<br />
        // 2. the node to be deleted has only one child <br />
        // 3. the node to be deleted has both left and right children<br />
        public void Delete(IComparable item)<br />
        {<br />
            // search for item and its parent<br />
            BTreeNode ptr = root; // search reference<br />
            BTreeNode parent = null; // parent of ptr<br />
            while ((ptr != null) && (item.CompareTo(ptr.Item) != 0))<br />
            {<br />
                parent = ptr;<br />
                if (item.CompareTo(ptr.Item) < 0) // move to the left child of ptr<br />
                    ptr = ptr.LChild;<br />
                else<br />
                    ptr = ptr.RChild;<br />
            }<br />
<br />
            if (ptr != null) // if the search was successful<br />
            {<br />
                // case 3: item has two children<br />
                if ((ptr.LChild != null) && (ptr.RChild != null))<br />
                {<br />
                    // find the right-most node in left subtree of ptr<br />
                    if (ptr.LChild.RChild == null) // a special case: the right subtree of ptr.LChild is empty<br />
                    {<br />
                        ptr.Item = ptr.LChild.Item;<br />
                        ptr.LChild = ptr.LChild.LChild;<br />
                    }<br />
                    else<br />
                    {<br />
                        BTreeNode p = ptr.LChild;<br />
                        BTreeNode pp = ptr; // parent of p<br />
                        while (p.RChild != null)<br />
                        {<br />
                            pp = p;<br />
                            p = p.RChild;<br />
                        }<br />
                        // copy the item at p to ptr<br />
                        ptr.Item = p.Item;<br />
                        pp.RChild = p.LChild;<br />
                    }<br />
                }<br />
                else // cases 1 & 2: item has no or only one child<br />
                {<br />
                    BTreeNode c;<br />
                    if (ptr.LChild != null)<br />
                        c = ptr.LChild;<br />
                    else<br />
                        c = ptr.RChild;<br />
<br />
                    // remove node ptr<br />
                    if (ptr == root) //need to change root<br />
                        root = c;<br />
                    else<br />
                    {<br />
                        if (ptr == parent.LChild)<br />
                            parent.LChild = c;<br />
                        else<br />
                            parent.RChild = c;<br />
                    }<br />
                }<br />
<br />
            }<br />
        }<br />
<br />
        public void PreOrderTraverse()<br />
        {<br />
            //Console.Write("PreOrder: ");<br />
            PreOrderTraverse(root);<br />
            Console.WriteLine();<br />
        }<br />
<br />
        private void PreOrderTraverse(BTreeNode root)<br />
        {<br />
            if (root != null)<br />
            {<br />
                Console.Write(root.Item);<br />
                PreOrderTraverse(root.LChild);<br />
                PreOrderTraverse(root.RChild);<br />
            }<br />
        }<br />
<br />
        public void InOrderTraverse()<br />
        {<br />
            //Console.Write("InOrder: ");<br />
            InOrderTraverse(root);<br />
            Console.WriteLine();<br />
        }<br />
<br />
        private void InOrderTraverse(BTreeNode root)<br />
        {<br />
            if (root != null)<br />
            {<br />
                InOrderTraverse(root.LChild);<br />
                Console.WriteLine(root.Item);<br />
                InOrderTraverse(root.RChild);<br />
            }<br />
        }<br />
<br />
        public void PostOrderTraverse()<br />
        {<br />
            //Console.Write("PostOrder: ");<br />
            PostOrderTraverse(root);<br />
            Console.WriteLine();<br />
        }<br />
<br />
        private void PostOrderTraverse(BTreeNode root)<br />
        {<br />
            if (root != null)<br />
            {<br />
                PostOrderTraverse(root.LChild);<br />
                PostOrderTraverse(root.RChild);<br />
                Console.Write(root.Item);<br />
            }<br />
        }<br />
<br />
        public void Clear()<br />
        {<br />
            root = null;<br />
        }<br />
<br />
        // pre: true<br />
        // post: Return all items in the collection<br />
        public ArrayList AllItems()<br />
        {<br />
            ArrayList items = new ArrayList();<br />
            AllItems(root, items);<br />
            return items;<br />
        }<br />
<br />
        private void AllItems(BTreeNode root, ArrayList keys)<br />
        {<br />
            if (root != null)<br />
            {<br />
                AllItems(root.LChild, keys);<br />
                keys.Add(root.Item);<br />
                AllItems(root.RChild, keys);<br />
            }<br />
        }<br />
}<br />
<br />
// the main method <br />
using System;<br />
using System.Collections;<br />
<br />
namespace PatientName<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            int choice;<br />
            IPatient p1 = new Patient();<br />
            IBSTree container = new BSTree();<br />
            Console.WriteLine("Enter the first Name of the patient:");<br />
            p1.FirstName = Console.ReadLine();<br />
            container.Insert(p1);<br />
                    <br />
        }<br />
    }<br />
}<br />
<br />
<br />

GeneralRe: The best overload method match system.IComparable has invalid arguments Pin
Guffa29-Apr-08 2:52
Guffa29-Apr-08 2:52 
QuestionI tried tht earlier and i gott this error "Patient cannot implement interface member System.IComparable.CompareTo(Object)" Pin
ADTC#29-Apr-08 3:02
ADTC#29-Apr-08 3:02 
AnswerRe: I tried tht earlier and i gott this error "Patient cannot implement interface member System.IComparable.CompareTo(Object)" Pin
J a a n s29-Apr-08 3:06
professionalJ a a n s29-Apr-08 3:06 
Generalyet another one cropped up Pin
ADTC#29-Apr-08 3:14
ADTC#29-Apr-08 3:14 
GeneralRe: yet another one cropped up Pin
J a a n s29-Apr-08 3:28
professionalJ a a n s29-Apr-08 3:28 
GeneralThank you Pin
ADTC#29-Apr-08 4:30
ADTC#29-Apr-08 4:30 
AnswerRe: The best overload method match system.IComparable has invalid arguments Pin
J a a n s29-Apr-08 2:54
professionalJ a a n s29-Apr-08 2:54 
QuestionDoesnot implement interface member System.IComparable.CompareTo(Object) Pin
ADTC#29-Apr-08 3:06
ADTC#29-Apr-08 3:06 
AnswerRe: Doesnot implement interface member System.IComparable.CompareTo(Object) Pin
J a a n s29-Apr-08 3:09
professionalJ a a n s29-Apr-08 3:09 
Questionyet another one cropped up Pin
ADTC#29-Apr-08 3:19
ADTC#29-Apr-08 3:19 
GeneralInstallation Pin
coders_need29-Apr-08 1:14
coders_need29-Apr-08 1:14 
GeneralRe: Installation Pin
J a a n s29-Apr-08 1:25
professionalJ a a n s29-Apr-08 1:25 
GeneralRe: Installation Pin
Vikram A Punathambekar29-Apr-08 2:17
Vikram A Punathambekar29-Apr-08 2:17 
QuestionHow to use regular expression Pin
Exelioindia29-Apr-08 0:07
Exelioindia29-Apr-08 0:07 
AnswerRe: How to use regular expression Pin
c242329-Apr-08 0:48
c242329-Apr-08 0:48 
GeneralRe: How to use regular expression Pin
Exelioindia29-Apr-08 1:05
Exelioindia29-Apr-08 1:05 
AnswerRe: How to use regular expression Pin
Pete O'Hanlon29-Apr-08 0:51
mvePete O'Hanlon29-Apr-08 0:51 
GeneralRe: How to use regular expression Pin
Exelioindia29-Apr-08 1:03
Exelioindia29-Apr-08 1:03 
GeneralTCP Socket Exception , two ports / one IP address Pin
Rick van Woudenberg28-Apr-08 23:40
Rick van Woudenberg28-Apr-08 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.