如何从二进制搜索树中打印给定节点?

时间:2014-03-17 01:26:48

标签: c# nodes binary-search-tree

如何使用名为PrintNodes的方法从二进制搜索树中打印出给定节点。

这是我的Node类:

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

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

这是我的二进制搜索树类:

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;
                }
            }
        }
    }  

这是我到目前为止我的PrintNodes方法,但我刚刚意识到它实际上没有打印给定节点,只是打印我传入方法的字符串(我真的很新,因为你可以见)

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

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


        return Output;
    }

我知道我必须以某种方式将传递给方法的字符串与我正在尝试打印的节点进行比较,但我不知道该做什么让我的思绪变得空白。

2 个答案:

答案 0 :(得分:2)

你的Find方法看起来像这样:

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;
}

您的PrintNodes方法可以是:

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;
}

干杯

编辑:我假设您的Node对象将具有其他一些&#34;有效载荷&#34;附加到它,现在你只需打印搜索字符串,如果找到: - )

答案 1 :(得分:0)

我相信你需要在树中找到一些字符串并打印出来。 如果是

这是伪代码

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;

}