TreeSet中的HeadSet,TailSet,SubSet

时间:2018-10-29 14:22:11

标签: kotlin treeset sortedset

(headSet,TailSet,Subset)和TreeSet之间的关系如何工作?当您从树中删除某些元素(如果此元素包含在Headset,TailSet或SubSet中)时,它将在此集中删除。我想自己实现二进制搜索树,并且想知道这种关系是如何工作的。有人可以解释一下或提供一些我可以找到信息的资源吗?我拥有的全部:
界面:

import java.util.*
interface CheckableSortedSet<T> : SortedSet<T> {
    fun checkInvariant(): Boolean
}

    import java.util.*
    import kotlin.NoSuchElementException
    import java.util.TreeSet

class KtBinaryTree<T : Comparable<T>> : AbstractMutableSet<T>(), CheckableSortedSet<T> {

private var root: Node<T>? = null

override var size = 0
    private set

private class Node<T>(val value: T) {

    var left: Node<T>? = null

    var right: Node<T>? = null
}

override fun add(element: T): Boolean {
    val closest = find(element)
    val comparison = if (closest == null) -1 else element.compareTo(closest.value)
    if (comparison == 0) {
        return false
    }
    val newNode = Node(element)
    when {
        closest == null -> root = newNode
        comparison < 0 -> {
            assert(closest.left == null)
            closest.left = newNode
        }
        else -> {
            assert(closest.right == null)
            closest.right = newNode
        }
    }
    size++
    return true
}

override fun checkInvariant(): Boolean =
        root?.let { checkInvariant(it) } ?: true

private fun checkInvariant(node: Node<T>): Boolean {
    val left = node.left
    if (left != null && (left.value >= node.value || !checkInvariant(left))) return false
    val right = node.right
    return right == null || right.value > node.value && checkInvariant(right)
}

override fun remove(element: T): Boolean {
    var currentNode = root ?: return false
    var parentNode = root ?: return false
    var onRight = true
    while (currentNode.value != element) {
        parentNode = currentNode
        if (element > currentNode.value) {
            currentNode = currentNode.right ?: return false
            onRight = true
        } else if (element < currentNode.value) {
            currentNode = currentNode.left ?: return false
            onRight = false
        }
    }
    if (currentNode.left == null && currentNode.right == null) {
        //if removal point is leaf
        when {
            currentNode == root -> root = null
            onRight -> parentNode.right = null
            else -> parentNode.left = null
        }
    } else if (currentNode.left == null) {
        //if removal point have only right child
        if (currentNode == root) root = currentNode.right
        else {
            val right = currentNode.right ?: return false
            setNode(onRight, parentNode, right)
        }
    } else if (currentNode.right == null) {
        //if removal point have only left child
        if (currentNode == root) root = currentNode.left
        else {
            val left = currentNode.left ?: return false
            setNode(onRight, parentNode, left)
        }
    } else {
        //worst case - if removal point have both children
        var minNode = currentNode.right ?: return false
        var parentMinNode = currentNode.right ?: return false
        while (minNode.left != null) {
            parentMinNode = minNode
            val left = minNode.left ?: return false
            minNode = left
        }
        when {
            currentNode == root && parentMinNode == minNode -> {
                val rootLeft = root!!.left
                root = minNode
                minNode.left = rootLeft
            }
            currentNode == root && parentMinNode != minNode -> {
                parentMinNode.left = minNode.right
                root = minNode
                minNode.left = currentNode.left
                minNode.right = currentNode.right
            }
            parentMinNode == minNode -> setNode(onRight, parentNode, minNode)
            else -> {
                parentMinNode.left = minNode.right
                minNode.right = currentNode.right
                minNode.left = currentNode.left
                setNode(onRight, parentNode, minNode)
            }
        }
        minNode.left = currentNode.left
    }
    size--
    return true
}

private fun setNode(onRight: Boolean, parentNode: Node<T>, currentNode: Node<T>) {
    if (onRight)
        parentNode.right = currentNode
    else parentNode.left = currentNode
}

override operator fun contains(element: T): Boolean {
    val closest = find(element)
    return closest != null && element.compareTo(closest.value) == 0
}

private fun find(value: T): Node<T>? =
        root?.let { find(it, value) }

private fun find(start: Node<T>, value: T): Node<T> {
    val comparison = value.compareTo(start.value)
    return when {
        comparison == 0 -> start
        comparison < 0 -> start.left?.let { find(it, value) } ?: start
        else -> start.right?.let { find(it, value) } ?: start
    }
}

inner class BinaryTreeIterator : MutableIterator<T> {

    private var current: Node<T>? = null

    private fun findNext(): Node<T>? {
        val currentNode = current ?: return find(first())
        if (currentNode.value == last()) return null
        if (currentNode.right != null) {
            var successor = currentNode.right ?: throw IllegalArgumentException()
            while (successor.left != null) {
                successor = successor.left ?: return successor
            }
            return successor
        } else {
            var successor = root ?: throw IllegalArgumentException()
            var ancestor = root ?: throw IllegalArgumentException()
            while (ancestor != currentNode) {
                if (currentNode.value < ancestor.value) {
                    successor = ancestor
                    ancestor = ancestor.left ?: return null
                } else ancestor = ancestor.right ?: return null
            }
            return successor
        }
    }

    override fun hasNext(): Boolean = findNext() != null

    override fun next(): T {
        current = findNext()
        return (current ?: throw NoSuchElementException()).value
    }


override fun subSet(fromElement: T, toElement: T): SortedSet<T> =
        tailSet(fromElement).intersect(headSet(toElement)).toSortedSet()

override fun headSet(toElement: T): SortedSet<T> {
    val flag = this.contains(toElement)
    if (!flag) this.add(toElement)
    val setOfMax = mutableSetOf<T>()
    val iter = iterator()
    var element: T = iter.next()
    while (element != toElement) {
        if (!iter.hasNext()) throw IllegalArgumentException()
        setOfMax.add(element)
        element = iter.next()
    }
    if (!flag) this.remove(toElement)
    return setOfMax.toSortedSet()
}


override fun tailSet(fromElement: T): SortedSet<T> {
    val flag = !this.contains(fromElement)
    if (flag) this.add(fromElement)
    val setOfMax = mutableSetOf<T>()
    val iter = this.iterator()
    var element: T = iter.next()
    while (element != fromElement) {
        element = iter.next()
    }
    while (element != last()) {
        setOfMax.add(element)
        element = iter.next()
    }
    if (flag) this.remove(fromElement)
    setOfMax.add(element)
    return setOfMax.toSortedSet()
}

override fun first(): T {
    var current: Node<T> = root ?: throw NoSuchElementException()
    while (current.left != null) {
        current = current.left!!
    }
    return current.value
}

override fun last(): T {
    var current: Node<T> = root ?: throw NoSuchElementException()
    while (current.right != null) {
        current = current.right!!
    }
    return current.value
}

}

1 个答案:

答案 0 :(得分:0)

我发现我该怎么做,我创建了一个将被赋予相同节点的类。并更改我称为SubSet的此其他树的迭代器。这是KtBinary树中的更改:

override fun subSet(fromElement: T, toElement: T): SortedSet<T> =
            SubSet(this, fromElement, toElement)

override fun headSet(toElement: T): SortedSet<T> =
            SubSet(this, null, toElement)

override fun tailSet(fromElement: T): SortedSet<T> =
            SubSet(this, fromElement, null)

这是SubSet(附加树)

import java.util.*

class SubSet<T : Comparable<T>>(private val delegate: SortedSet<T>,
                                private val fromElement: T?,
                                private val toElement: T?) : AbstractMutableSet<T>(), SortedSet<T> {
    override fun comparator(): Comparator<in T>? = delegate.comparator()

    override fun subSet(fromElement: T, toElement: T): SortedSet<T> {
        return SubSet(this, fromElement, toElement)
    }

    override fun headSet(toElement: T): SortedSet<T> {
        return SubSet(this, null, toElement)
    }

    override fun tailSet(fromElement: T): SortedSet<T> {
        return SubSet(this, fromElement, null)
    }

    override fun last(): T {
        val iter = iterator()
        var element = iter.next()
        while (iter.hasNext()) {
            element = iter.next()
        }
        return element
    }

    override fun first(): T {
        return iterator().next()
    }

    override fun add(element: T): Boolean {
        return when {
            fromElement == null && toElement == null -> false
            fromElement == null && element < toElement!! -> delegate.add(element)
            toElement == null && element >= fromElement!! -> delegate.add(element)
            element >= fromElement!! && element < toElement!! -> delegate.add(element)
            else -> false
        }
    }

    override fun remove(element: T): Boolean {
        return when {
            fromElement == null && toElement == null -> false
            fromElement == null && element < toElement!! -> delegate.remove(element)
            toElement == null && element >= fromElement!! -> delegate.remove(element)
            element >= fromElement!! && element < toElement!! -> delegate.remove(element)
            else -> false
        }
    }

    override fun iterator(): MutableIterator<T> = object : MutableIterator<T> {
        private val delegate = this@SubSet.delegate.iterator()

        private var next: T? = null

        init {
            while (delegate.hasNext()) {
                if (fromElement == null) {
                    this.next = delegate.next()
                    break
                }
                val next = delegate.next()
                if (next >= fromElement) {
                    this.next = next
                    break
                }
            }
        }

        override fun hasNext(): Boolean {
            val n = next ?: return false
            return n < toElement ?: return true
        }

        override fun next(): T {
            val result = next ?: throw NoSuchElementException()
            next = if (delegate.hasNext()) delegate.next() else null
            return result
        }

        override fun remove() {
            delegate.remove()
        }

    }

    override val size: Int
        get() = when {
            fromElement == null && toElement == null -> 0
            fromElement == null -> delegate.count { it < toElement!! }
            toElement == null -> delegate.count { it >= fromElement }
            else -> delegate.count { it >= fromElement && it < toElement }
        }
}