Ciao ho costruito un labero binario:
Il mio problema è il metodo lengthLeft(SimpleNode)...non mi calcola la lunghezza del nodo sinistro...infatti se eseguo da main:codice:class SimpleNode { private SimpleNode left; private SimpleNode right; private String content; SimpleNode(String content){ if(content==null){ throw new RuntimeException("argomento nullo"); } this.content=content; } public String getContent(){ return content; } public boolean setLeftNode(SimpleNode n){ if(n.left==null&&n!=null){ left=n; return true; }else return false; } public SimpleNode getLeft(){ return left; } public boolean setRightNode(SimpleNode n){ if(n.right==null&&n!=null){ right=n; return true; }else return false; } public SimpleNode getRight(){ return right; } public void add(SimpleNode n){ if(left==null){ setLeftNode(n); }else if(right==null){ setRightNode(n); } } //calocolo lunghezza nodo sinistro private static int lengthLeft(SimpleNode node) { if (node == null){ return(0); }else return lengthLeft(node.getLeft()) + 1; } public String toString(){ String s=""; return s+="nodo: "+content+"\nleft: "+getLeft()+"\nright: "+getRight(); } }
Il terminale stampa 2...e non 4...come posso calcolare la lunghezza del nodo sinistro del mio albero?codice:public static void main(String[] args){ SimpleNode n=new SimpleNode("70"); boolean b=n.setLeftNode(new CleverNode("40")); boolean bz=n.setLeftNode(new CleverNode("50")); boolean bx=n.setLeftNode(new CleverNode("80")); boolean bc=n.setLeftNode(new CleverNode("90")); System.out.println(lengthLeft(n)); } }![]()

Rispondi quotando