mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 09:49:49 +08:00
Add kotlin code for the chapter of tree (#1172)
* modified array.kt. * feat(kotlin): add kotlin code for the chapter of stack and queue. * modified array.kt * modified comments. * feat(kotlin): add kotlin code for the chapter of tree. * feat(kotlin): add kotlin code for the chapter of tree.
This commit is contained in:
40
codes/kotlin/chapter_tree/binary_tree.kt
Normal file
40
codes/kotlin/chapter_tree/binary_tree.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: binary_tree.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_tree
|
||||
|
||||
import utils.TreeNode
|
||||
import utils.printTree
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
val n1 = TreeNode(1)
|
||||
val n2 = TreeNode(2)
|
||||
val n3 = TreeNode(3)
|
||||
val n4 = TreeNode(4)
|
||||
val n5 = TreeNode(5)
|
||||
// 构建节点之间的引用(指针)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
n2.right = n5
|
||||
println("\n初始化二叉树\n")
|
||||
printTree(n1)
|
||||
|
||||
/* 插入与删除节点 */
|
||||
val P = TreeNode(0)
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = P
|
||||
P.left = n2
|
||||
println("\n插入节点 P 后\n")
|
||||
printTree(n1)
|
||||
// 删除节点 P
|
||||
n1.left = n2
|
||||
println("\n删除节点 P 后\n")
|
||||
printTree(n1)
|
||||
}
|
||||
Reference in New Issue
Block a user