写一个合适的二叉树高度函数?

时间:2018-04-27 03:58:49

标签: java binary-search-tree

我试图编写一个显示二叉搜索树高度的函数,如下所示。问题是我应该编写一个没有任何参数或参数的函数。这真的让我很难过。我尝试在参数列表之外声明root但是没有用。任何解决方案?

int height (Node root){
if (root == null) {
    return 0;
    }
int hleftsub = height(root.m_left);
int hrightsub = height(root.m_right);
return Math.max(hleftsub, hrightsub) + 1;
    }

我的导师提供的方法签名是

int height ()

编辑:

我的完整代码

import javax.swing.tree.TreeNode;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;

class BinarySearchTree<E extends Comparable<E>> {
    public Node<E> root;
    public int m_size = 0;

    public BinarySearchTree() {

    }

public boolean search(E value) {
    boolean ret = false;
    Node<E> current = root;
    while (current != null && ret != true) {
        if (current.m_value.compareTo(current.m_value) == 0) {
            ret = true;
        } else if (current.m_value.compareTo(current.m_value) > 0) {
            current = current.m_left;
        } else {
            current = current.m_right;
        }
    }
    return false;
}

public boolean insert(E value) {
    if (root == null) {
        root = new Node<>(value);
        m_size++;
    } else {
        Node<E> current = root;
        Node<E> parentNode = null;
        while (current != null)
            if (current.m_value.compareTo(value) > 0) {
                parentNode = current;
                current = current.m_left;
            } else if (current.m_value.compareTo(value) < 0) {
                parentNode = current;
                current = current.m_right;
            } else {
                return false;
            }
        if (current.m_value.compareTo(value) < 0) {
            parentNode.m_left = new Node<>(value);
        } else {
            parentNode.m_right = new Node<>(value);

        }
    }
    m_size++;
    return true;
}

boolean remove(E value) {
    if (!search(value)) {
        return false;
    }
    Node check = root;
    Node parent = null;
    boolean found = false;
    while (!found && check != null) {
        if (value.compareTo((E) check.m_value) == 0) {
            found = true;
        } else if (value.compareTo((E) check.m_value) < 0) {
            parent = check;
            check = check.m_left;
        } else {
            parent = check;
            check = check.m_right;
        }

    }
    if (check == null) {
        return false;
    } else if (check.m_left == null) {
        if (parent == null) {
            root = check.m_right;
        } else if (value.compareTo((E) parent.m_value) < 0) {
            parent.m_left = check.m_right;
        } else {
            parent.m_right = check.m_right;
        }
    } else {
        Node<E> parentofRight = check;
        Node<E> rightMost = check.m_left;
        while (rightMost.m_right != null) {
            parentofRight = rightMost;
            rightMost = rightMost.m_right;
        }
        check.m_value = rightMost.m_value;
        if (parentofRight.m_right == rightMost) {
            rightMost = rightMost.m_left;
        } else {
            parentofRight.m_left = rightMost.m_left;
        }
    }
    m_size--;
    return true;
}

int numberNodes () {
    return m_size;
    }

int height (Node root){
    if (root == null) {
        return 0;
        }
    int hleftsub = height(root.m_left);
    int hrightsub = height(root.m_right);
    return Math.max(hleftsub, hrightsub) + 1;
        }

int numberLeafNodes(Node node){
    if (node == null) {
        return 0;
    }
    else if(node.m_left == null && node.m_right == null){
        return 1;
    }
    else{
        return numberLeafNodes(node.m_left) + numberLeafNodes(node.m_right);
    }
}

void display(String message){
    if(root == null){
        return;
    }
    display(String.valueOf(root.m_left));
    display(String.valueOf(root));
    display(String.valueOf(root.m_right));
}
}

class Node<E> {
    public E m_value;
    public Node<E> m_left;
    public Node<E> m_right;


    public Node(E value) {
        m_value = value;
    }

}

1 个答案:

答案 0 :(得分:1)

  1. 如果迭代遍历树,则可以获得没有递归的高度。任何递归都可以迭代实现。但它可能是更多的代码行。这将是级别顺序图/树遍历的变体。
  2. 请参阅:https://www.geeksforgeeks.org/iterative-method-to-find-height-of-binary-tree/

    如果您使用该实现,请删除该参数,因为height()已有权访问root

    然而,这需要一个队列,O(n)时间和O(n)空间。

    1. height()可能是一个公共方法,它调用启动递归的私有方法height(Node node)。 BST的O(n)时间,O(1)空间。

    2. 您可以将高度作为额外参数传递,以递归方式插入树中,这样您就可以计算递归调用的数量(这与您所在树中的深度/#水平直接相关)。一旦节点找到它的位置,如果您传递的高度(递归调用的数量)超过了树存储的实例变量height,则将实例变量更新为新的高度。这也将允许tree.height()成为恒定时间函数。 O(1)时间,O(1)空间。