簡體   English   中英

如何反轉(鏡像)遞歸二進制搜索樹的子樹

[英]How to reverse (mirror) a recursive binary search tree's subtrees

我正在嘗試用Java編寫二進制搜索樹。 我的BST使用許多“關鍵字”,並使用大多數遞歸方法將它們插入樹中。

不幸的是,它似乎向后添加了它們,例如,右側的字母(ac ...)比左側的(xz ...)高。

我無法弄清楚如何正確逆轉邏輯。

這是我的插入代碼:

  /**
 * 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應該在左側,匹配項應該在右側,依此類推。

在這種方法中:

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'開頭的單詞要少,因此您可以准確地得到代碼所建議的內容。

要對此進行修改,只需在各處進行比較即可。

在代碼中,反轉<和>。 以便代碼讀取

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM