[英]How to get the left and right child of a node in an unordered binary search tree?
[英]how to find the height of right child minus height of the left child in a tree
我在java中有一棵樹的節點有這個類
public class Node {
Node (int v, Node lt, Node rt){
value = v;
left = lt;
right = rt;
height = 0;
parent = null;
}
Node (int v){
this (v, null, null);
}
int value;
int height;
Node left;
Node right;
Node parent;
}
此節點的高度為(this.right.height-this.left.height)
該節點只有一個子節點且其子節點為葉子的節點的高度和高度,如果子節點為右子節點則為1,如果子節點為左子節點則為-1
我怎樣才能做到這一點??
(我想寫一個avl樹)
試試下面的一個
public int height(Node root){
if(root == null)return 0;
return 1+Max(height(root.left),height(root.right));
}
heightDifference = (height(this.right) - height(this.left))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.