我现在尝试使用递归来制作它但我很难过,我想我有一个逻辑错误。
请某人帮我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();
}
}
请同时检查我的插入机制......我不认为这是正确的方法
答案 0 :(得分:1)
由于您的问题不明确,我正在回答您的两个潜在问题。
1-如果你想找到一个节点的最小祖先,你可以做的是给一个节点继续遍历它的父节点,同时保持到目前为止找到的最小父值的轨迹。到达根节点后,您将获得最小的值。该算法的时间复杂度为O(h),其中h是树的高度。由于这很简单,我不在这里给出代码示例。
2-如果你想找到LCA(这就是你所质疑的标题),你可以做一些非常相似的事情。如果我们假设密钥n1和n2存在于二进制树中,我们可以使用二进制树的单遍历找到LCA,而无需为路径数组提供额外的存储。我们的想法是从根开始遍历树。如果任何给定键(n1和n2)与root匹配,则root是LCA(假设两个键都存在)。如果root与任何键都不匹配,我们会重复左右子树。在其左子树中存在一个键而在右子树中存在的另一个键的节点是LCA。如果两个键都位于左子树中,则左子树也具有LCA,否则LCA位于右子树中。下面是一个示例代码,您可以根据需要进行定制(此代码有效):
//Java implementation to find lowest common ancestor of
// n1 and n2 using one traversal of binary tree
/* Class containing left and right child of current
node and key value*/
class Node
{
int data;
Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
public class BinaryTree
{
//Root of the Binary Tree
Node root;
Node findLCA(int n1, int n2)
{
return findLCA(root, n1, n2);
}
// This function returns pointer to LCA of two given
// values n1 and n2. This function assumes that n1 and
// n2 are present in Binary Tree
Node findLCA(Node node, int n1, int n2)
{
// Base case
if (node == null)
return null;
// If either n1 or n2 matches with root's key, report
// the presence by returning root (Note that if a key is
// ancestor of other, then the ancestor key becomes LCA
if (node.data == n1 || node.data == n2)
return node;
// Look for keys in left and right subtrees
Node left_lca = findLCA(node.left, n1, n2);
Node right_lca = findLCA(node.right, n1, n2);
// If both of the above calls return Non-NULL, then one key
// is present in once subtree and other is present in other,
// So this node is the LCA
if (left_lca!=null && right_lca!=null)
return node;
// Otherwise check if left subtree or right subtree is LCA
return (left_lca != null) ? left_lca : right_lca;
}
/* Driver program to test above functions */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
System.out.println("LCA(4, 5) = " +
tree.findLCA(4, 5).data);
System.out.println("LCA(4, 6) = " +
tree.findLCA(4, 6).data);
System.out.println("LCA(3, 4) = " +
tree.findLCA(3, 4).data);
System.out.println("LCA(2, 4) = " +
tree.findLCA(2, 4).data);
}
}
此方法和代码来自此网站上的http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/,您可以在IDE中运行代码以及在线。