如何从二叉搜索树中打印给定节点

本文关键字:打印 节点 搜索树 | 更新日期: 2023-09-27 17:50:44

如何使用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;
    }

我知道我必须以某种方式将传入方法的字符串与我试图打印的节点进行比较,但我不知道该怎么做,我的大脑一片空白。

如何从二叉搜索树中打印给定节点

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

欢呼编辑:我假设你的节点对象将有一些其他的"有效载荷"附加到它,因为现在你只是打印搜索字符串,如果找到:-)

我认为您需要在树中找到一些字符串并打印它。如果是

这里有一个伪代码

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