Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:

push:
branches: [ "main", "dev"]
branches: [ "main", "dev" ]
pull_request:
branches: [ "main" ]

Expand Down
5 changes: 0 additions & 5 deletions lib/src/main/kotlin/tree_tripper/SearchTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public interface SearchTree<K: Comparable<K>, V>: Iterable<Pair<K, V>> {
*/
public fun getMinInSubtree(key: K): Pair<K, V>?

/**
* Returns the size of a tree.
*/
public fun getSize(): Int

// Iterator
public fun iterator(order: IterationOrders): Iterator<Pair<K, V>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import tree_tripper.nodes.notNullNodeAction
public abstract class AbstractBSTree<K: Comparable<K>, V, N: AbstractBSTreeNode<K, V, N>>: SearchTree<K, V> {
protected var root: N? = null
private set
private var size: Int = 0
public var size: Int = 0
private set

override fun insert(key: K, value: V) {
insert(key, value, permissionUpdate = true)
Expand Down Expand Up @@ -77,10 +78,6 @@ public abstract class AbstractBSTree<K: Comparable<K>, V, N: AbstractBSTreeNode<
return notNullNodeAction(root, null) { node -> getMinInSubtree(node.key) }
}

override fun getSize(): Int {
return size
}

override fun iterator(): BinarySearchTreeIterator<K, V, N> {
return iterator(IterationOrders.WIDTH_ORDER)
}
Expand Down Expand Up @@ -210,7 +207,7 @@ public abstract class AbstractBSTree<K: Comparable<K>, V, N: AbstractBSTreeNode<
var nodeCurrent: N? = root ?: return null

while (nodeCurrent != null) {
val resultCompare = key.compareTo(nodeCurrent.key)
val resultCompare: Int = key.compareTo(nodeCurrent.key)
if (resultCompare < 0)
nodeCurrent = nodeCurrent.leftChild
else if (resultCompare > 0)
Expand Down
15 changes: 8 additions & 7 deletions lib/src/main/kotlin/tree_tripper/binary_trees/RBTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import tree_tripper.nodes.notNullNodeUpdate
public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K, V>>() {

override fun createNode(key: K, value: V): RBTreeNode<K, V> {
return RBTreeNode(key, value)
return RBTreeNode(key, value, isRed = true, leftChild = null, rightChild = null)
}

override fun updateRoot(node: RBTreeNode<K, V>?) {
Expand All @@ -23,7 +23,7 @@ public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K
}

override fun balanceTree(node: RBTreeNode<K, V>): RBTreeNode<K, V> {
var nodeCurrent: RBTreeNode<K, V> = node
var nodeCurrent = node
if (isRedColor(nodeCurrent.rightChild) && !isRedColor(nodeCurrent.leftChild)) {
nodeCurrent = rotateLeft(nodeCurrent)
}
Expand All @@ -43,7 +43,7 @@ public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K
var resultCompare: Int = key.compareTo(node.key)
var nodeCurrent: RBTreeNode<K, V> = node
if (resultCompare < 0) {
if (nodeCurrent.leftChild != null && !isRedColor(nodeCurrent.leftChild) && !isRedLeftChild(nodeCurrent.leftChild))
if (!isRedColor(nodeCurrent.leftChild) && !isRedLeftChild(nodeCurrent.leftChild))
nodeCurrent = moveRedLeft(nodeCurrent)

removeResult = removeNode(nodeCurrent.leftChild, key)
Expand All @@ -55,7 +55,7 @@ public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K
}
if (resultCompare == 0 && nodeCurrent.rightChild == null)
return Pair(null, nodeCurrent.value)
if (nodeCurrent.rightChild != null && !isRedColor(nodeCurrent.rightChild) && !isRedLeftChild(nodeCurrent.rightChild)) {
if (!isRedColor(nodeCurrent.rightChild) && !isRedLeftChild(nodeCurrent.rightChild)) {
nodeCurrent = moveRedRight(nodeCurrent)
resultCompare = key.compareTo(nodeCurrent.key)
}
Expand Down Expand Up @@ -149,7 +149,8 @@ public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K
* @param node the node to move
* @return the new root of the tree, which is balanced node subtree
*/
private fun moveRedRight(node: RBTreeNode<K, V>): RBTreeNode<K, V> {
protected fun moveRedRight(node: RBTreeNode<K, V>): RBTreeNode<K, V> {
if (node.rightChild == null) return node
var nodeCurrent: RBTreeNode<K, V> = node

flipColors(nodeCurrent)
Expand All @@ -168,6 +169,7 @@ public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K
* @return the new root of the tree, which is balanced node subtree
*/
private fun moveRedLeft(node: RBTreeNode<K, V>): RBTreeNode<K, V> {
if (node.leftChild == null) return node
var nodeCurrent: RBTreeNode<K, V> = node

flipColors(nodeCurrent)
Expand All @@ -187,9 +189,8 @@ public open class RBTree<K: Comparable<K>, V>: AbstractBSTree<K, V, RBTreeNode<K
* @param node the root of the binary search tree
* @return the root of the binary search tree with the node removed, or `null` if the tree is empty
*/
private fun removeMinNode(node: RBTreeNode<K, V>?): RBTreeNode<K, V>? {
protected fun removeMinNode(node: RBTreeNode<K, V>?): RBTreeNode<K, V>? {
if (node == null) return null

val leftChild = node.leftChild ?: return node.rightChild

var nodeCurrent: RBTreeNode<K, V> = node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public abstract class AbstractBSTreeNode<K: Comparable<K>, V, N: AbstractBSTreeN
public val key: K,
public var value: V
): SearchTreeNode<K, V, N> {
public open var leftChild: N? = null
public open var rightChild: N? = null
public var leftChild: N? = null
public var rightChild: N? = null

override fun getChildren(): List<N> {
return listOfNotNull(leftChild, rightChild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AVLTreeTest {
@DisplayName("tree initialization")
public fun testTreeInitialization() {
tree.assertRoot(null) { "Root of AVLTree is not null by standard initialize." }
Assertions.assertEquals(0, tree.getSize())
Assertions.assertEquals(0, tree.size)
}

@ParameterizedTest
Expand Down
Loading