我的问题:
我有一个Java程序,它在二叉树中搜索最低公共祖先。那部分应该工作,但我的问题是关于我的主要方法,所以我没有对它进行适当的测试。我需要从存储在我的数组中的字符创建一个二叉树。
我发现了什么
简单地说,并不多。我发现了一些触及主题的页面,但是它们的实现与我的其余代码差别很大。如果有人甚至可以提供指南的链接,最好提供代码示例,这可能会回答我的问题。
我的代码
public class TreeMain
{
static TreeNode root;
public static void main(String args[])
{
String[] myStringsChars = new String[26];
for(int i = 0; i < 26; i++)
{
myStringChars[i] = new String(Character.toChars(i+65));
System.out.println(myStringChars[i]);
}
// array to binary tree
TreeNode commonAncestor = findLowestCommonAncestor(firstNode, secondNode);
if(commonAncestor != null) {
System.out.println(commonAncestor.getContents()); }
}
public static TreeNode findLowestCommonAncestor(TreeNode node1, TreeNode node2)
{
return findLCA(root, node1, node2);
}
public static TreeNode findLCA(TreeNode node, TreeNode node1, TreeNode node2)
{
if (node == null) {
return null; }
if (node.getContents() == node1 || node.getContents() == node2) {
return node; }
TreeNode leftLCA = findLCA(node.getLeftChild(), node1, node2);
TreeNode rightLCA = findLCA(node.getRightChild(), node1, node2);
if (leftLCA!=null && rightLCA!=null)
return node;
// Otherwise check if left subtree or right subtree is LCA
return (leftLCA != null) ? leftLCA : rightLCA;
}
}
public class TreeNode<T extends Comparable>{
private T contents;
private TreeNode<T> parent;
private TreeNode<T> leftChild;
private TreeNode<T> rightChild;
private int level;
public TreeNode(T data, TreeNode parent)
{
contents = data;
this.parent = parent;
}
public void setLeftChild(TreeNode node)
{
this.leftChild = node;
}
public void setRightChild(TreeNode node)
{
this.rightChild = node;
}
public boolean isContentEquals(T data)
{
return 0 == getContents().compareTo(data);
}
public T getContents() {
return contents;
}
public void setContents(T contents) {
this.contents = contents;
}
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode getLeftChild() {
return leftChild;
}
public TreeNode getRightChild() {
return rightChild;
}
public TreeNode findNodeOnTree(T contentToSearch)
{
List<TreeNode> nodes = new LinkedList();
nodes.clear();
nodes.add(this);
while(!nodes.isEmpty())
{
TreeNode current = nodes.remove(0);
if(current.isContentEquals(contentToSearch))
{
return current;
}
if(current.leftChild != null)
{
nodes.add(current.leftChild);
}
if(current.rightChild != null)
{
nodes.add(current.rightChild);
}
}
return null;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
答案 0 :(得分:0)
我还没有尝试过您的算法,但您正在寻找的部分应该非常简单。实现它的一种快速方法是绘制您想到的二叉树的图片,并编写几种方法将该图片转换为代码。以下是我的开始:
public TreeNode<T> addLeft(T key) {
TreeNode<T> tmp = new Node<>(key);
this.left = tmp;
tmp.parent = this; // if you are using parent pointers
return tmp; // for chaining purpose, so returned node can be subject to addition
}
public TreeNode<T> addRight(T key) {
TreeNode<T> tmp = new TreeNode<>(key);
this.right = tmp;
tmp.parent = this;
return tmp;
}
要构建树,您可以创建根节点,然后调用addLeft()
和addRight()
方法,如您在二叉树的图片中所示:
TreeNode<Character> root = ...; // say root's key is 'A'
TreeNode<Character> left1 = root.addLeft('B');
TreeNode<Character> right1 = root.addRight('C');
//TreeNode<Character> left2 = left1.addLeft();
// and so on for your character array.
更复杂的方法是考虑你想到的树的inorder遍历和预序遍历,将两个遍历表示为两个单独的数组,然后编写另一个程序来接受这两个构造二进制树的数组。这是一个练习。