@@ -12,10 +12,16 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
12
12
var root: NodeType ? = null
13
13
internal set
14
14
15
+ // This function is called after a node has been inserted or removed from the tree,
16
+ // and is used to balance the tree if necessary.
17
+ // The default implementation simply returns the starting node without performing any balancing.
15
18
protected open fun balance (initNode : NodeType ? ): NodeType ? {
16
19
return initNode
17
20
}
18
21
22
+ // This function is used to add a node to a tree while ensuring that the tree remains balanced.
23
+ // It recursively traverses the tree until it finds the correct position for the new node,
24
+ // and then calls balance to balance the tree.
19
25
protected fun balancedAdd (initNode : NodeType ? , node : NodeType ): NodeType ? {
20
26
21
27
if (initNode == null ) {
@@ -32,6 +38,8 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
32
38
return balance(initNode)
33
39
}
34
40
41
+ // This function is used to remove a node from a tree while ensuring that the tree remains balanced.
42
+ // It recursively traverses the tree until it finds the node to be removed, and then calls
35
43
protected fun balancedDelete (initNode : NodeType ? , node : NodeType ): NodeType ? {
36
44
if (initNode == null ) {
37
45
return null
@@ -57,6 +65,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
57
65
return balance(initNode)
58
66
}
59
67
68
+ // This function is used to check if a node matches with the same value as
60
69
protected fun contains (initNode : NodeType ? , node : NodeType ): NodeType ? {
61
70
if (initNode == null ) {
62
71
return null
@@ -71,6 +80,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
71
80
}
72
81
}
73
82
83
+ // This function returns the node with the smallest value in the subtree rooted at the specified node.
74
84
protected fun getMinimal (node : NodeType ): NodeType {
75
85
var minNode = node
76
86
while (true ) {
@@ -79,6 +89,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
79
89
return minNode
80
90
}
81
91
92
+ // This function returns the node with the largest value in the subtree rooted at the specified node.
82
93
protected fun getMaximal (node : NodeType ): NodeType {
83
94
var maxNode = node
84
95
while (true ) {
@@ -87,6 +98,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
87
98
return maxNode
88
99
}
89
100
101
+ // This function performs a left rotation on the specified node, changing its position to its right child node.
90
102
protected fun rotateLeft (node : NodeType ): NodeType ? {
91
103
val rightChild = node.right
92
104
val secondSubtree = rightChild?.left
@@ -97,6 +109,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
97
109
return rightChild
98
110
}
99
111
112
+ // This function performs a right rotation for the specified node, changing its position to its left child.
100
113
protected fun rotateRight (node : NodeType ): NodeType ? {
101
114
val leftChild = node.left
102
115
val secondSubtree = leftChild?.right
@@ -107,6 +120,7 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
107
120
return leftChild
108
121
}
109
122
123
+ // This function replaces the specified child node of a node with a new child node
110
124
protected fun replaceChild (child : NodeType , newChild : NodeType ? ): NodeType ? {
111
125
if (child == root) {
112
126
root = newChild
@@ -122,6 +136,9 @@ abstract class AbstractTree<T : Comparable<T>, NodeType : AbstractNode<T, NodeTy
122
136
return newChild
123
137
}
124
138
139
+ // This function returns a preview of the tree as a list of nodes.
140
+ // It uses the walk internal function to recursively traverse the tree in advance
141
+ // and add each node to the list.
125
142
fun preOrder (): List <NodeType > {
126
143
val result = mutableListOf<NodeType >()
127
144
fun walk (node : NodeType , lst : MutableList <NodeType >) {
0 commit comments