Skip to content

Commit 3a4d514

Browse files
committed
Almost endded Testing adding and removing and corrected them
1 parent c339836 commit 3a4d514

File tree

2 files changed

+150
-19
lines changed

2 files changed

+150
-19
lines changed

src/main/kotlin/treeLib/bintrees/interfaces/Treap.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ class Treap<K : Comparable<K>, V: Comparable<V> > : BinTree<K, V, TreapNode<K, V
1919
if(node.left != null) node.left?.let{ collectNodes(it) }
2020
}
2121

22-
fun buildSubTree(): TreapNode<K, V> {
23-
var baum = Treap<K, V>()
22+
fun buildSubTree(): TreapNode<K, V>? {
23+
val baum = Treap<K, V>()
2424
for(node in subTree) baum.add(node.key, node.value)
25-
baum.root?.let { baum.collectNodes(it) }
25+
/*baum.root?.let { baum.collectNodes(it) }
2626
subTree = baum.subTree //После такого оно сохранит свойство автоматической сортировки иль як?
27-
return subTree.first()
28-
/*
29-
subTree.clean()
30-
baum.root?.let { return it }
31-
*/
27+
return subTree.first()*/
28+
subTree.clear()
29+
return baum.root()
3230
//Может быть можно не собирать массив, а просто взять корень и уже с ним работать?
3331
}
3432

@@ -48,10 +46,10 @@ class Treap<K : Comparable<K>, V: Comparable<V> > : BinTree<K, V, TreapNode<K, V
4846

4947
override fun remove(key: K): V? {
5048
if(root == null) return null
51-
/*if(root?.key == key && amountOfNodes == 1) {
49+
if(root?.key == key && amountOfNodes == 1) {
5250
amountOfNodes -= 1
5351
root = null
54-
}*/
52+
}
5553

5654
val parent = this.findParent(key)
5755
var count = 0
@@ -62,7 +60,7 @@ class Treap<K : Comparable<K>, V: Comparable<V> > : BinTree<K, V, TreapNode<K, V
6260

6361
if(count == 0) {
6462
if(parent?.right == curNode) parent?.right = null
65-
else parent?.left == null
63+
else parent?.left = null
6664
}
6765
if(count == 1) {
6866
if(parent?.right == curNode) parent?.right = curNode?.right ?: curNode?.left
@@ -72,9 +70,8 @@ class Treap<K : Comparable<K>, V: Comparable<V> > : BinTree<K, V, TreapNode<K, V
7270
curNode?.right?.let { collectNodes(it) }
7371
curNode?.left?.let { collectNodes(it) }
7472
val result = buildSubTree()
75-
if(parent == root) root = result
76-
else if(parent?.right == curNode) parent?.right = result
77-
else parent?.left == result
73+
if(parent?.right == curNode) parent?.right = result
74+
else parent?.left = result
7875
}
7976
return null
8077
}
@@ -98,7 +95,7 @@ class Treap<K : Comparable<K>, V: Comparable<V> > : BinTree<K, V, TreapNode<K, V
9895
it.right = buildSubTree()
9996
}
10097
}
101-
else if(it.key < key)( //Не забудь подумать о случае, когда добавленный ключ вже существует
98+
else if(it.key > key)( //Не забудь подумать о случае, когда добавленный ключ вже существует
10299
if(it.left == null) it.left = newNode
103100
else{
104101
newNode.left = it.left
@@ -112,15 +109,16 @@ class Treap<K : Comparable<K>, V: Comparable<V> > : BinTree<K, V, TreapNode<K, V
112109
root = buildSubTree()
113110
}
114111

115-
amountOfNodes += 1
116-
return newNode
112+
amountOfNodes += 1
113+
return newNode
117114
}
118115

119116
return root
120117
}
121118

122119

123120
fun x(curNode: TreapNode<K, V>, parent: TreapNode<K, V>, node: TreapNode<K, V>): TreapNode<K, V>{
121+
//Добавь ? к возвращаему значению чтоб отработать случаи одинаковости ключа и/иль приоритета
124122
if(curNode.value < node.value) {
125123
if(curNode == parent) return node
126124
return parent

src/test/kotlin/TreapTest.kt

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.junit.jupiter.api.BeforeEach
12
import org.junit.jupiter.api.Nested
23
import org.junit.jupiter.api.Test
34
import treeLib.bintrees.BSTree
@@ -8,6 +9,78 @@ import kotlin.test.assertEquals
89
class TreapTest {
910
var baum = Treap<Int, Int>()
1011

12+
@Nested
13+
inner class TestindRemoveMethod {
14+
15+
@BeforeEach
16+
fun setUp() {
17+
baum.add(20,100)
18+
baum.add(50,90)
19+
baum.add(35,80)
20+
baum.add(40,70)
21+
baum.add(30,60)
22+
baum.add(70,50)
23+
baum.add(60,40)
24+
baum.add(80,30)
25+
}
26+
27+
@Test
28+
fun removeNodeWithoutChildren() {
29+
val rt = baum.root()?.right?.left
30+
31+
baum.remove(40)
32+
assertEquals(null, rt?.right)
33+
34+
baum.remove(30)
35+
assertEquals(null, rt?.left)
36+
37+
baum.remove(35)
38+
assertEquals(null, baum.root()?.right?.left)
39+
}
40+
41+
@Test
42+
fun removeNodeWithOnlyOneChild() {
43+
baum.remove(60)
44+
baum.remove(80)
45+
baum.remove(70)
46+
assertEquals(null, baum.root()?.right?.right)
47+
48+
baum.remove(50)
49+
assertEquals(TreapNode(35, 80), baum.root()?.right)
50+
}
51+
52+
@Test
53+
fun removeNodeWithTwoChildrenFirstCase() {
54+
val rt = baum.root()?.right
55+
baum.remove(35)
56+
57+
assertEquals(TreapNode(40, 70), rt?.left)
58+
assertEquals(TreapNode(30,60), rt?.left?.left)
59+
}
60+
61+
@Test
62+
fun removeNodeWithTwoChildrenSecondCase() {
63+
val rt = baum.root()?.right
64+
baum.remove(70)
65+
66+
assertEquals(TreapNode(60, 40), rt?.right)
67+
assertEquals(TreapNode(80, 30), rt?.right?.right)
68+
}
69+
70+
@Test
71+
fun removeNodeWithTwoChildrenThirdCase() {
72+
val rt = baum.root()
73+
baum.remove(50)
74+
75+
assertEquals(TreapNode(35,80), rt?.right)
76+
assertEquals(TreapNode(30,60), rt?.right?.left)
77+
assertEquals(TreapNode(40,70), rt?.right?.right)
78+
assertEquals(TreapNode(70,50), rt?.right?.right?.right)
79+
assertEquals(TreapNode(60,40), rt?.right?.right?.right?.left)
80+
assertEquals(TreapNode(80,30), rt?.right?.right?.right?.right)
81+
}
82+
}
83+
1184

1285
@Nested
1386
inner class TestingAddingMethod {
@@ -20,7 +93,7 @@ class TreapTest {
2093

2194

2295
@Test
23-
fun addingLikeInBST() {
96+
fun addingLikeInBSTree() {
2497
baum.add(4, 60)
2598
baum.add(7, 50)
2699
baum.add(2, 40)
@@ -41,12 +114,72 @@ class TreapTest {
41114
}
42115

43116
@Test
44-
fun changinRoot() {
117+
fun changingRootToLeft() {
45118
baum.add(2, 10)
46119
baum.add(4,20)
47120

48121
assertEquals(TreapNode(4,20), baum.root())
49122
assertEquals(TreapNode(2,10), baum.root()?.left)
50123
}
124+
125+
@Test
126+
fun changingRootToRight() {
127+
baum.add(4, 10)
128+
baum.add(2,20)
129+
130+
assertEquals(TreapNode(2,20), baum.root())
131+
assertEquals(TreapNode(4,10), baum.root()?.right)
132+
}
133+
134+
@Test
135+
fun addingLikeInBSTreeAndChangingByAddingRootLeftAndRightChildren() {
136+
baum.add(4, 60)
137+
baum.add(7, 50)
138+
baum.add(2, 40)
139+
baum.add(10, 30)
140+
baum.add(-1, 20)
141+
baum.add(3, 10)
142+
baum.add(1, 0)
143+
baum.add(11, 55)
144+
baum.add(0, 45)
145+
146+
val rt = baum.root()
147+
148+
assertEquals(TreapNode(4, 60), baum.root())
149+
assertEquals(TreapNode(11,55), rt?.right)
150+
assertEquals(TreapNode(0,45), rt?.left)
151+
assertEquals(TreapNode(7,50), rt?.right?.left)
152+
assertEquals(TreapNode(10,30), rt?.right?.left?.right)
153+
assertEquals(TreapNode(2,40), rt?.left?.right)
154+
assertEquals(TreapNode(3,10), rt?.left?.right?.right)
155+
assertEquals(TreapNode(1,0), rt?.left?.right?.left)
156+
assertEquals(TreapNode(-1,20), rt?.left?.left)
157+
}
158+
159+
@Test
160+
fun changingSubTreesFarFromRoot() {
161+
baum.add(20,100)
162+
baum.add(50,90)
163+
baum.add(35,80)
164+
baum.add(40,70)
165+
baum.add(30,60)
166+
baum.add(70,50)
167+
baum.add(60,40)
168+
baum.add(80,30)
169+
baum.add(32, 85)
170+
baum.add(75, 65)
171+
172+
val rt = baum.root()
173+
174+
assertEquals(TreapNode(20,100), rt)
175+
assertEquals(TreapNode(32,85), rt?.right?.left)
176+
assertEquals(TreapNode(75,65), rt?.right?.right)
177+
assertEquals(TreapNode(30, 60), rt?.right?.left?.left)
178+
assertEquals(TreapNode(35,80), rt?.right?.left?.right)
179+
assertEquals(TreapNode(40,70), rt?.right?.left?.right?.right)
180+
assertEquals(TreapNode(80,30), rt?.right?.right?.right)
181+
assertEquals(TreapNode(70,50), rt?.right?.right?.left)
182+
assertEquals(TreapNode(60,40), rt?.right?.right?.left?.left)
183+
}
51184
}
52185
}

0 commit comments

Comments
 (0)