如何反转(镜像)递归二叉搜索树的子树

时间:2015-07-13 18:25:19

标签: algorithm recursion data-structures tree binary-search-tree

我正在尝试用Java编写二进制搜索树。我的BST采用了许多“关键字”,并使用大多数递归方法将它们插入到树中。

不幸的是,似乎是向后添加它们,E.x。右侧是高于左侧(x-z ......)的字母(a-c ...)。

我无法弄清楚如何正确地反转逻辑。

这是我的插入代码:

  /**
 * This method creates a new record for theFileData.
 * This is a recursive insertion method, that adds recordToAdd to the list of records
 * for the node associated with theKeyword.
 * 
 * If there is no keyword, create a new Node for it.
 * 
 * @param theKeyword keyword to associate with new record.
 * @param theFileData file data to associate with new record.
 */
public void insert(String theKeyword, FileData fd) {
    if (fd == null) {
        throw new NullPointerException("Invalid file data.");
    }
    if (theKeyword == null) {
        throw new NullPointerException("Invalid keyword.");
    }
    theKeyword = theKeyword.toLowerCase();

    Record recordToAdd = new Record(fd.id, fd.author, fd.title, null);

    // step one is to find the node with keyword theKeyword. That will give us the correct list to insert into.
    if (root == null) {
        /*
         * If the tree is currently empty, we create a new node as root.
         * This node than has the record added to it's records list.
         */
        Node newNode = new Node(theKeyword);
        newNode.update(recordToAdd);
        root = newNode;
    } else if (!contains(theKeyword)) {
        Node newNode = new Node(theKeyword);
        newNode.update(recordToAdd);
        insert(root, newNode);
    } else {
        Node target = find(theKeyword, root);
        target.update(recordToAdd);
    }
}

/**
 * This recursive insertion helper method allows us to quickly and easily add a new Node object
 * to our BST.
 */
private Node insert(Node theParent, Node theNode) {
    if (theParent == null) {
        return theNode;
    } else if (theNode.keyword.compareTo(theParent.keyword) < 0) {
        theParent.right = insert(theParent.right, theNode);
    } else if (theNode.keyword.compareTo(theParent.keyword) > 0) {
        theParent.left = insert(theParent.left, theNode);
    }
    return theParent;
}

/**
 * This helper method searches for a given keyword, returning the node when found.
 * 
 * @return Node containing the keyword you are looking for. Else null.
 */
private Node find(String keyword, Node root) {
    if (keyword == null) {
        throw new IllegalArgumentException("Invalid keyword.");
    }

    if (root == null) {
        return null;
    }
    keyword = keyword.toLowerCase();
    if (keyword.compareTo(root.keyword) > 0) {
        return find(keyword, root.left);
    }
    if (keyword.compareTo(root.keyword) < 0) {
        return find(keyword, root.right);
    }
    return root;
}

/**
 * This method simply calls the find helper method. If find returns null, we know the value does not exist.
 * 
 * @param keyword keyword to search for.
 * @return true or false depending on if the keyword exists in the BST.
 */
public boolean contains(String keyword) {

    keyword = keyword.toLowerCase();

    if (find(keyword, root) != null) {
        return true; // if the keyword exists.
    }
    return false;
}

强文

以下是树的图形表示:

| | | -------斑点
| | -------建筑
| | | | -------因果关系
| | | | | -------分类规则
| | | -------集群
| -------基于内容的
| | -------数据挖掘
数据库
| | | -------距离措施
| | | | -------图像显示
| | -------图像管理
| -------图像检索
| | | | -------图像叠
| | | | | | -------索引
| | | | | | | | -------信息检索
| | | | | | | -------基于实例的
| | | | | | | | -------基于实例的
| | | | | -------知识
| | | -------行
| | -------匹配
| | | | | | -------多媒体
| | | | | | | -------神经网络
| | | | | -------构成
| | | | -------修剪
| | | | | -------查询
| | | -------查询通过例如
| | | | | -------查询树
| | | | -------识别
| | | | | | -------基于区域的
| | | | | | | -------关系
| | | | | -------搜索
| | | | | | -------相似
| | | | | | | | -------空间
| | | | | | | | | -------时间
| | | | | | | | | | -------时间相关
| | | | | | | -------三角不等式
| | | | | | | | -------加权

Blob应位于左侧,匹配应位于右侧,等等。

2 个答案:

答案 0 :(得分:1)

在这种方法中:

private Node insert(Node theParent, Node theNode) {
    if (theParent == null) {
        return theNode;
    } else if (theNode.keyword.compareTo(theParent.keyword) < 0) {
        theParent.right = insert(theParent.right, theNode);
    } else if (theNode.keyword.compareTo(theParent.keyword) > 0) {
        theParent.left = insert(theParent.left, theNode);
    }
    return theParent;
}

当您要插入的节点按字典顺序小于父节点时,您将插入到右侧。以'a'开头的单词按字典顺序小于以'z'开头的单词,因此您可以获得代码建议您想要的内容。

要修改此功能,只需在各处进行比较即可。

答案 1 :(得分:1)

在你的代码中,反转&lt;和&gt;。以便代码读取

private Node insert(Node theParent, Node theNode) {
if (theParent == null) {
    return theNode;
} else if (theNode.keyword.compareTo(theParent.keyword) > 0) {
    theParent.right = insert(theParent.right, theNode);
} else if (theNode.keyword.compareTo(theParent.keyword) < 0) {
    theParent.left = insert(theParent.left, theNode);
}
    return theParent;
}