没有子节点的BST节点不被删除

时间:2018-11-18 01:15:42

标签: java binary-search-tree

我的BST删除具有子节点的节点后,没有删除没有子节点的节点。

删除功能:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

int main()
{
    int c;
    HANDLE old_scrbuff = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE new_scrbuff = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, NULL, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    while (TRUE)
    {
        c = _getch();                   //_getch has a bug on April update of windows 10. Use static linkage with an older SDK
        printf("%d %c\n", c, c);
        if (c == 0 || c == 224)
        {
            c = _getch();
            SetConsoleActiveScreenBuffer(new_scrbuff);
            HANDLE swap = old_scrbuff;
            old_scrbuff = new_scrbuff;
            new_scrbuff = swap;
            printf("%d %c\n", c, c);
        }
    }
    return 0;
}

主要:

private Node remove(Node current, Pair k) throws DictionaryException {
        if (current == null) {
            throw new DictionaryException("Key does not exist");
        }

        if (k.compareTo(current.data.getKey()) < 0) {
            current.left = remove(current.left, k);
        } else if (k.compareTo(current.data.getKey()) > 0) {
            current.right = remove(current.right, k);
        } else {
            if (current.left == null && current.right == null) {
                current = null;
            } else if (current.right == null) {
                current = current.left;
            } else if (current.left == null) {
                current = current.right;
            } else {
                Record smallest = smallest(current.right).data;
                current.data = smallest;
                remove(current.right, smallest.getKey());
            }
        }
        return current;
    }

我希望public static void main(String[] args) { Pair key1 = new Pair("homework", "text"); Record record1 = new Record(key1, "hello world"); Pair key2 = new Pair("course", "text"); Record record2 = new Record(key2, "world hello"); Pair key3 = new Pair("class", "text"); Record record3 = new Record(key3, "bean man"); Pair key4 = new Pair("computer", "text"); Record record4 = new Record(key4, "despacito"); Pair key5 = new Pair("four", "text"); Record record5 = new Record(key5, "zebras"); OrderedDictionary od = new OrderedDictionary(); try { od.put(record1); od.put(record2); od.put(record3); od.put(record4); od.put(record5); } catch (DictionaryException e) { System.out.println("exception in main - put"); } try { od.remove(key2); } catch (DictionaryException e){ System.out.println("exception in main - remove"); } od.preOrder(); 将返回“作业四级计算机”。但是,它返回:“作业四级计算机四”。由于某些原因,它并没有删除作为“课程”的正确子项的“四个”,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

要删除根元素时,有些事情很棘手。您可以通过将root-right-child设置为right-child的最右边子元素的root-left-child来做到这一点,反之亦然,而不是修改root元素的值并删除right的最右边元素分支。

答案 1 :(得分:0)

弄清楚了...只需将remove(current.right, smallest.getKey());中的current.right = remove(current.right, smallest.getKey());更改为remove(Node current, Pair k),以便它引用正确的节点