Frage

How do I print out a given node from my binary search tree using a method called PrintNodes.

Here is my Node class:

public class Node
{
    public string data;
    public Node left;
    public Node right;

    public Node(string data)
    {
        this.data = data;
    } 
}

And here is my Binary Search Tree Class:

class BinarySearchTree
{
    public Node root, current;

    public BinarySearchTree()
    {
        this.root = null;
    }

    public void AddNode(string a) // code to insert nodes to the binary search tree 
    {
        Node newNode = new Node(a); //create a new node 
        if (root == null) // if the tree is empty new node will be the root node 
            root = newNode;
        else
        {
            Node previous;
            current = root;
            while (current != null)
            {
                previous = current;
                if (string.Compare(a,  current.data) < 0) //if the new node is less than the current node 
                {
                    current = current.left;
                    if (current == null)
                        previous.left = newNode;
                }
                else //if the new node is greater than the current node 
                {
                    current = current.right;

                    if (current == null)
                        previous.right = newNode;
                }
            }
        }
    }  

This is the what I have so far for my PrintNodes method, but I just realised that its not actually printing the given node its just printing the string that I pass into the method(I'm really new to this as you can see)

 public String PrintNodes(string a)
    {
        string Output = "";

        if (a != null)
        {
            Output += "Node: " + a;
        }
        else
        {
            Output += "There is no node " + a;
        }


        return Output;
    }

I know I have to somehow compare the string that is passed into the method to the node I am trying to print but I have no clue what to do my mind has gone blank.

War es hilfreich?

Lösung

Your Find method would look something like this:

public Node Find(key, root)
{
    Node current-node = root;
    while (current-node != null)
    {
        if (current-node.data == key)
            return current-node;
        else if (key < current-node.data)
            current-node = current-node.left;
        else
            current-node = current-node.right;
    }

    return null;
}

Your PrintNodes method could then be:

public String PrintNodes(string a)
{
    string Output = "";

    Node theNode = Find(a);

    if (Node != null)
    {
        Output += "Node: " + theNode.data;
    }
    else
    {
        Output += "There is no node " + a;
    }


    return Output;
}

Cheers

EDIT: I assume that your Node object will have some other "payload" attached to it, as right now you're simply printing the search string if found :-)

Andere Tipps

I believe that you need to find some string in the tree and print it. if it is

here is a pseudo code

public String PrintNodes(string a , Node root)
{
string Output = "";
if( a == null )
return "string is empty";

Node curent;
curent = root;

while( curent != null )
{
    compVal = string.Compare(a,  current.data)
    if ( compVal < 0 ) 
    {
        //goto leftNode
        curent = curent.left
    }
    else if ( compVal > 0 )
    {
            //goto rightNode
        curent = curent.right
    }
    else
    {
        //this is it
        break;
    }
}
if curent == null
return "string not found";
else
return
curent.data;

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top