Algorithm 在二叉搜索树中查找高度
我想知道是否有人能帮我重新设计这个方法来找到二叉搜索树的高度。到目前为止,我的代码是这样的。然而,我得到的答案比实际高度大1。但是当我从return语句中删除+1时,它比实际高度小1。我仍在尝试用这些BST来处理递归。如果有任何帮助,我将不胜感激Algorithm 在二叉搜索树中查找高度,algorithm,recursion,binary-search-tree,Algorithm,Recursion,Binary Search Tree,我想知道是否有人能帮我重新设计这个方法来找到二叉搜索树的高度。到目前为止,我的代码是这样的。然而,我得到的答案比实际高度大1。但是当我从return语句中删除+1时,它比实际高度小1。我仍在尝试用这些BST来处理递归。如果有任何帮助,我将不胜感激 public int findHeight(){ if(this.isEmpty()){ return 0; } else{ TreeNode<T> node = root;
public int findHeight(){
if(this.isEmpty()){
return 0;
}
else{
TreeNode<T> node = root;
return findHeight(node);
}
}
private int findHeight(TreeNode<T> aNode){
int heightLeft = 0;
int heightRight = 0;
if(aNode.left!=null)
heightLeft = findHeight(aNode.left);
if(aNode.right!=null)
heightRight = findHeight(aNode.right);
if(heightLeft > heightRight){
return heightLeft+1;
}
else{
return heightRight+1;
}
}
public int findHeight(){
if(this.isEmpty()){
返回0;
}
否则{
树节点=根;
返回findHeight(节点);
}
}
私有内部findHeight(TreeNode阳极){
int heightLeft=0;
int heightRight=0;
if(阳极左!=null)
heightLeft=findHeight(阳极。左侧);
if(阳极右侧!=null)
heightRight=findHeight(阳极右侧);
如果(左高>右高){
返回高度左+1;
}
否则{
返回高度右侧+1;
}
}
以下是一种简洁且希望正确的表达方式:
private int findHeight(TreeNode<T> aNode){
if(aNode == null || (aNode.left == null && aNode.right == null))
return 0;
return Math.max(findHeight(aNode.left), findHeight(aNode.right)) + 1;
}
private int findHeight(TreeNode阳极){
如果(阳极==null | |(阳极.左==null和阳极.右==null))
返回0;
返回数学最大值(findHeight(阳极左)、findHeight(阳极右))+1;
}
如果当前节点为空,则不存在树。如果两个子层都是,则只有一层,表示高度为0。这使用了高度的定义(Stephen提到)作为“层的定义-1”在我看来,您的代码将受益于简化一点。当子指针为空时,不要尝试结束递归,而应仅在当前指针为空时结束递归。这使得代码编写起来简单得多。在伪代码中,它看起来像这样:
if (node = null)
return 0;
else
left = height(node->left);
right = height(node->right);
return 1 + max(left, right);
二叉搜索树的高度等于
层数-1
请参见
您的递归很好,所以只需在根级别减去1
另外请注意,您可以通过处理空节点稍微清理函数:
int findHeight(node) {
if (node == null) return 0;
return 1 + max(findHeight(node.left), findHeight(node.right));
}
这是未经测试的,但显然是正确的:
private int findHeight(Treenode<T> aNode) {
if (aNode.left == null && aNode.right == null) {
return 0; // was 1; apparently a node with no children has a height of 0.
} else if (aNode.left == null) {
return 1 + findHeight(aNode.right);
} else if (aNode.right == null) {
return 1 + findHeight(aNode.left);
} else {
return 1 + max(findHeight(aNode.left), findHeight(aNode.right));
}
}
private int findHeight(Treenode阳极){
if(aNode.left==null&&aNode.right==null){
返回0;//为1;显然,没有子节点的节点的高度为0。
}else if(阳极左==null){
返回1+findHeight(阳极右侧);
}else if(阳极右==null){
返回1+findhight(阳极左);
}否则{
返回1+max(findHeight(阳极左)、findHeight(阳极右));
}
}
通常,简化代码要比一个一个地找出它的原因容易得多。这段代码很容易理解:四种可能的情况都以明显正确的方式得到了明确的处理:
- 如果左树和右树都为null,则返回0,因为根据定义,单个节点的高度为0。(一)
- 如果左树或右树(但不是全部!)都为空,则返回非空树的高度加1以说明当前节点的附加高度
- 如果两棵树都不为null,则返回较高子树的高度,再加上当前节点的高度
- 问题在于你的基本情况
“树的高度是从根到树中最深节点的路径长度。只有一个节点(根)的(根)树的高度为零。”-
如果没有节点,则希望返回-1而不是0。这是因为您在末尾添加了1
因此,如果没有节点,则返回-1以抵消+1
int findHeight(TreeNode<T> aNode) {
if (aNode == null) {
return -1;
}
int lefth = findHeight(aNode.left);
int righth = findHeight(aNode.right);
if (lefth > righth) {
return lefth + 1;
} else {
return righth + 1;
}
}
int findHeight(树状阳极){
如果(阳极==null){
返回-1;
}
int lefth=findHeight(阳极左);
int righth=findHeight(阳极右侧);
如果(左H>右H){
返回lefth+1;
}否则{
返回righth+1;
}
}
C代码。
在BST类中包括这两个方法。你们需要两种方法来计算树的高度。HeightHelper计算它,&HeightRecursive在main()中打印它 上面给出的高度定义不正确。这就是深度的定义 树中节点M的深度是从树的根到M的路径长度。树的高度比树中最深节点的深度大一个。深度为d的所有节点都位于树中的d级。根是级别为0的唯一节点,其深度为0 引文:“数据结构和算法分析实用介绍” 版本3.2(Java版本) 克利福德·A·沙弗 计算机科学系 弗吉尼亚理工大学
弗吉尼亚州布莱克斯堡24061为所有读过这篇文章的人 高度定义为从根节点到叶节点的最长路径中的节点数。因此:只有根节点的树的高度为1,而不是0 给定节点的级别是到根的距离加1。因此:根节点位于级别1,其子节点位于级别2,依此类推
(信息来源:数据结构:使用Java的抽象和设计,第二版,作者Elliot B.Koffman&Paul A.T.Wolfgang)-我目前在哥伦布州立大学学习的数据结构课程中使用的书。我想这个问题可能意味着两件不同的事情
int-calcHeight(节点*根){
if(root==NULL)
返回0;
int l=calcHeight(根->左);
int r=calcHeight(根->右);
如果(l>r)
返回l+1;
其他的
返回r+1;
}
int-calcSize(节点*根){
if(root==NULL)
返回0;
返回(calcSize(root->left)+1+calcSize(root->right));
}
将tempHeight设置为静态变量(最初为0) 静态void findHeight(节点,int计数){
if(节点==null){
返回;
}
if((node.right==null)和&(node.left==null)){
如果(温度高度<计数)
public void HeightRecursive()
{
Console.WriteLine( HeightHelper(root) );
}
private int HeightHelper(TreeNode node)
{
if (node == null)
{
return -1;
}
else
{
return 1 + Math.Max(HeightHelper(node.LeftNode),HeightHelper(node.RightNode));
}
}
public int getHeight(Node node)
{
if(node == null)
return 0;
int left_val = getHeight(node.left);
int right_val = getHeight(node.right);
if(left_val > right_val)
return left_val+1;
else
return right_val+1;
}
private static int heightOfTree(Node root)
{
if (root == null)
{
return 0;
}
int left = 1 + heightOfTree(root.left);
int right = 1 + heightOfTree(root.right);
return Math.Max(left, right);
}
int getHeight(Node node) {
if (node == null) return -1;
return 1 + Math.max(getHeight(node.left), getHeight(node.right));
}
if (node == null) {
return;
}
if ((node.right == null) && (node.left == null)) {
if (tempHeight < count) {
tempHeight = count;
}
}
findHeight(node.left, ++count);
count--; //reduce the height while traversing to a different branch
findHeight(node.right, ++count);
}
public static int getHeight (Node root){
int lheight = 0, rheight = 0;
if(root==null) {
return 0;
}
else {
if(root.left != null) {
lheight = 1 + getHeight(root.left);
System.out.println("lheight" + " " + lheight);
}
if (root.right != null) {
rheight = 1+ getHeight(root.right);
System.out.println("rheight" + " " + rheight);
}
if(root != null && root.left == null && root.right == null) {
lheight += 1;
rheight += 1;
}
}
return Math.max(lheight, rheight);
}
class Solution{
public static int getHeight(Node root) {
int height = -1;
if (root == null) {
return height;
} else {
height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
return height;
}
int getHeight(Node* root)
{
if(root == NULL) return -1;
else return max(getHeight(root->left), getHeight(root->right)) + 1;
}
public int height(){
if(this.root== null) return 0;
int leftDepth = nodeDepth(this.root.left, 1);
int rightDepth = nodeDepth(this.root.right, 1);
int height = leftDepth > rightDepth? leftDepth: rightDepth;
return height;
}
private int nodeDepth(Node node, int startValue){
int nodeDepth = 0;
if(node.left == null && node.right == null) return startValue;
else{
startValue++;
if(node.left!= null){
nodeDepth = nodeDepth(node.left, startValue);
}
if(node.right!= null){
nodeDepth = nodeDepth(node.right, startValue);
}
}
return nodeDepth;
}
public int getHeight(Node root) {
return Math.max(root.left != null ? getHeight(root.left) : -1,
root.right != null ? getHeight(root.right) : -1)
+ 1;
}
int height(Node* root) {
if(root == NULL){
return -1;
}
int sum=0;
int rheight = height(root->right);
int lheight = height(root->left);
if(lheight>rheight){
sum = lheight +1;
}
if(rheight > lheight){
sum = rheight + 1;
}
return sum;
}
int height(Node* root) {
if(root==NULL) return -1;
return max(height(root->left),height(root->right))+1;
}
def height(node, current_height)
return current_height if node.nil? || (node.left.nil? && node.right.nil?)
return [height(node.left, current_height + 1), height(node.right, current_height + 1)].max if node.left && node.right
return height(node.left, current_height + 1) if node.left
return height(node.right, current_height + 1)
end
int maxDepth(BinaryTreeNode root) {
if(root == null || (root.left == null && root.right == null)) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
public static int height(Node root)
{
// Base case: empty tree has height 0
if (root == null) {
return 0;
}
// recursively for left and right subtree and consider maximum depth
return 1 + Math.max(height(root.left), height(root.right));
}
public class Node
{
public Node(string key)
{
Key = key;
Height = 1;
}
public int Height { get; set; }
public string Key { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public override string ToString()
{
return $"{Key}";
}
}
public class BinarySearchTree
{
public Node RootNode { get; private set; }
public void Put(string key)
{
if (ContainsKey(key))
{
return;
}
RootNode = Put(RootNode, key);
}
private Node Put(Node node, string key)
{
if (node == null) return new Node(key);
if (node.Key.CompareTo(key) < 0)
{
node.Right = Put(node.Right, key);
}
else
{
node.Left = Put(node.Left, key);
}
// since each node has height property that is maintained through this Put method that creates the binary search tree.
// calculate the height of this node by getting the max height of its left or right subtree and adding 1 to it.
node.Height = Math.Max(GetHeight(node.Left), GetHeight(node.Right)) + 1;
return node;
}
private int GetHeight(Node node)
{
return node?.Height ?? 0;
}
public Node Get(Node node, string key)
{
if (node == null) return null;
if (node.Key == key) return node;
if (node.Key.CompareTo(key) < 0)
{
// node.Key = M, key = P which results in -1
return Get(node.Right, key);
}
return Get(node.Left, key);
}
public bool ContainsKey(string key)
{
Node node = Get(RootNode, key);
return node != null;
}
}
[TestCase("SEARCHEXAMPLE", 6)]
[TestCase("SEBAQRCHGEXAMPLE", 6)]
[TestCase("STUVWXYZEBAQRCHGEXAMPLE", 8)]
public void HeightTest(string data, int expectedHeight)
{
// Arrange.
var runner = GetRootNode(data);
// Assert.
Assert.AreEqual(expectedHeight, runner.RootNode.Height);
}
private BinarySearchTree GetRootNode(string data)
{
var runner = new BinarySearchTree();
foreach (char nextKey in data)
{
runner.Put(nextKey.ToString());
}
return runner;
}