C#在二进制搜索树中删除有两个孩子的节点&从树中获取平均值

时间:2016-05-16 17:48:40

标签: c# binary-search-tree

我无法删除树中有两个孩子的节点。 请帮我在代码中找到错误。 我的节点类:

public class Node
{
    public int value;
    public Node left;
    public Node right;
}

我的二进制搜索树类:

public class BST
{
public Node root;

public BST()
{
    root = null;
}
public bool Remove(int key)
{
    Node current = root;
    Node parent = root;
    bool isleftChild = true;
    while (current.value != key)
    {
        parent = current;
        if (key < current.value)
        {
            isleftChild = true;
            current = current.left;
        }
        else
        {
            isleftChild = false;
            current = current.right;
        }
        if (current == null)
            return false;
    }

    if ((current.left == null) && (current.right == null))
    {
        if (current == root)
            root = null;
        else if (isleftChild)
            parent.left = null;
        else
            parent.right = null;
    }
    else if (current.right == null)
    {
        if (current == root)
            root = current.left;
        else if (isleftChild)
            parent.left = current.left;
        else
            parent.right = current.right;
    }
    else if (current.left == null)
    {
        if (current == root)
            root = current.right;
        else if (isleftChild)
            parent.left = parent.right;
        else
            parent.right = current.right;
    }
    else  //Removing a Node With Two Children
    {
        Node successor = GetSuccessor(current);
        if (current == root)
            root = successor;
        else if (isleftChild)
            parent.left = successor;
        else
            parent.right = successor;
        successor.left = current.left;
        successor.right = current.right;
    }
    return true;
}
      public Node GetSuccessor(Node delNode) //search a node that should stand up instead of the remote node.
        {         
            Node successorParent = delNode;
            Node successor = delNode;
            Node current = delNode.right;
            while (!(current == null))
            {
                successorParent = current;
                successor = current;
                current = current.left;
            }
            if (!(successor == delNode.right))
            {
                successorParent.left = successor.right;
                successor.right = delNode.right;
            }
        return successor;
    }
}

另外,我在从树中获取平均值时遇到问题:

public class BST
    {
    public Node root;

    public BST()
    {
        root = null;
    }
    public void SA(Node Root, double sum, int count)
        {
            if (Root == null)
                return;
            sum += Root.value;
            SA(Root.left, sum, count);
            SA(Root.right, sum, count);
            count++;
        }
  }

主要功能的代码

                    BST data = new BST();
                    data.SA(data.root, sum, count);
                    Console.WriteLine((sum / count));

0 个答案:

没有答案