数据结构_二叉树
//二叉树BST
class Node {
constructor (data) {
this.data = data
this.left = null
this.right = null
}
}
class BST {
constructor () {
this.root = null
}
insert (data) {
let newNode = new Node(data)
if (!this.root) this.root = newNode
else {
this.insertNode(this.root, newNode)
}
}
//插入节点的辅助函数
insertNode (root, newNode) {
if (newNode.data < root.data) {
if (root.left == null) root.left = newNode
else this.insertNode(root.left, newNode)
}else {
if (root.right == null) root.right = newNode
else this.insertNode(root.right, newNode)
}
}
}
let tree = new BST() 相关推荐
koushr 2020-11-12
kikaylee 2020-10-31
范范 2020-10-28
MILemon 2020-10-22
hugebawu 2020-10-12
LauraRan 2020-09-28
shenwenjie 2020-09-24
omyrobin 2020-09-23
guangcheng 2020-09-22
qiangde 2020-09-13
hanyujianke 2020-08-18
晨曦之星 2020-08-14
xiesheng 2020-08-06
KAIrving 2020-08-02
xiesheng 2020-08-02
范范 2020-07-30
chenfei0 2020-07-30