Imは再帰を使用して今すぐ作成しようとしていますが、Imは苦労しています。論理的なエラーがあると思います。バイナリツリー:1つのノードでLCAを実装するにはどうすればよいですか?
誰かがここに私のコードはこれまでのところですT_T
私に助けてください
import java.util.ArrayList;
public class BTNode{
private BTNode root, left,right;
private int item;
public BTNode(int item){
this(item,null,null,null);
}
public BTNode(int item, BTNode left, BTNode right, BTNode root,int count){
this.item = item;
this.left = left;
this.right = right;
this.root = root;
this.count=count;
}
public void build(){
this.left = new BTNode(40);
this.left.root = this;
this.right = new BTNode(100);
this.right.root = this;
this.left.right = new BTNode(45);
this.left.right.root = this.left;
this.left.left = new BTNode(25);
this.left.left.root = this.left;
this.right.left = new BTNode(70);
this.right.left.root = this.right;
this.right.right = new BTNode(200);
this.right.right.root = this.right;
}
public void inorder(){//recursion
//traverse the left
if(left != null)
left.inorder();
//visit the root do the item
System.out.print(this.item+" ");
//traverse the right
if(right != null)
right.inorder();
}
public String inToString(){
return ((left == null)?"": left.inToString()) + item + " " + ((right == null)?"": right.inToString()+" ");
}
//preorder()
public void preorder(){
//visit the root do the item
System.out.print(this.item+" ");
//traverse the left
if(left != null)
left.preorder();
//traverse the right
if(right != null)
right.preorder();
}
//preToString()
public String preToString(){
return item+ " "+((left == null)?"": left.preToString())+ ((right == null)?"": right.preToString()+" ");
}
//postorder()
public void postorder(){
//traverse the left
if(left != null)
left.postorder();
//traverse the right
if(right != null)
right.postorder();
//visit root do the item
System.out.print(this.item+" ");
}
//postToString()
public String postToString(){
return ((left == null)?"": left.postToString()) + ((right == null)?"": right.postToString()+" ")+item+ " ";
}
//findsum-recursive
public int findsum(){
//traverse left,if null traverse to right then add the two item lastly add to the root
return ((left == null)?0: left.findsum()) + ((right == null)?0: right.findsum())+item;
}
//findlevel-recursive
public int findlevel(){
//check left && right if it is not null then iterate method recursively
if (left == null)
return 0;
else if(right == null)
return 0;
else
return 1 + left.findlevel();
}
/*
//ancestor-recursive
public BTNode ancestor(int value){
if(left.count== 2){//check the root
return left.ancestor();//traverse left node
}
System.out.print(item+" ");//print item if the left turns false
if(root != null){
right.ancestor();//traverse right node
}
}*/
public int count(){
//check if left || right are null then return 0, otherwise method were recursively executed
return (((left==null)?0:1+left.count())+((right==null)?0:1+right.count()));
}
public void reference(){
//check left != null print the root, traverse left, traverse right
if(left != null)
left.reference();
if(left != null)
System.out.print(item+" ");
if(right != null)
right.reference();
}
public void sort(){
}
public void insert(int given){
BTNode node = new BTNode(given);
if(item == 0)
root = node;
else
{
BTNode current = root; //points to the current Node
BTNode parent; //points to the parent Node
while(current != null)
{
if(item > given)
{
parent = current;
current = left;
}
if(item < given)
{
parent = current;
current = right;
}
}
current = node;
if(item < given)
right = node;
else
left = node;
}
right.inorder();
}
public boolean contains(int given){
return ((left==null)?false:left.contains(given))|| item==given || ((right == null)?false : right.contains(given));
/*if(left != null)
left.contains(given);
if(right != null)
right.contains(given);
return item == given;*/
}
public static void main(String[]args){
BTNode root = new BTNode(50);
root.build();
System.out.print("\nGiven :"+root.inToString());
System.out.println("\ninorder");
root.inorder();
System.out.println("\npreorder");
root.preorder();
System.out.println("\npostorder");
root.postorder();
System.out.println("\nfindsum");
System.out.println(root.findsum());
System.out.println("\nfindlevel");
//System.out.println(root.findlevel(200));
System.out.println(root.findlevel());
System.out.println("\nancestor");
//System.out.println(root.ancestor());
root.ancestor();
System.out.println("\ncount");
System.out.println(root.count());
System.out.println("\nreference");
//System.out.println(root.reference());
root.reference();
System.out.println("\nsort");
//System.out.print(root.sort());
root.sort();
System.out.println("\nContains");
System.out.println(root.contains(new Integer(70)));
System.out.println("\ninsert");
//System.out.print(root.sort());
root.insert(new Integer(71));
root.printinorder();
}
}
も...私はそれを
あなたのアドバイスをいくつか教えてください... – Amits
あなたのロジックについての説明を追加してください。また、なぜあなたが達成しようとしているのかを正確に把握してください。 –
挨拶! @DanishAlsayed私のバイナリツリーの共通の祖先を見つけるためのコードをどうやって実装するのですか? root.ancestor(25)のように1つのノードしか与えられていない場合...再帰的に出力が50になる – Amits