二叉樹
function Node(data, left, right) {
this.data = data
this.left = left
this.right = right
this.show = () => this.data
}
function BST() {
this.root = null
this.insert = function (data) {
let node = new Node(data, null, null);
if (this.root === null) {
this.root = node
} else {
let current = this.root;
let parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.left;
if (current === null) {
parent.left = node;
break;
}
} else {
current = current.right;
if (current === null) {
parent.right = node;
break;
}
}
}
}
}
this.inOrder = function () {
const arr = []
const _inOrder = (node) => {
if (node !== null) {
_inOrder(node.left);
arr.push(node.show())
_inOrder(node.right);
}
}
_inOrder(this.root)
return arr
}
this.preOrder = function () {
const arr = []
const _preOrder = (node) => {
if (node !== null) {
arr.push(node.show())
_preOrder(node.left);
_preOrder(node.right);
}
}
_preOrder(this.root)
return arr
}
this.postOrder = function () {
const arr = []
const _postOrder = (node) => {
if (node !== null) {
_postOrder(node.left);
_postOrder(node.right);
arr.push(node.show())
}
}
_postOrder(this.root)
return arr
}
this.getMin = function () {
let current = this.root;
while (current.left !== null) {
current = current.left;
}
return current.data;
}
this.getMax = function () {
let current = this.root;
while (current.right !== null) {
current = current.right;
}
return current.data;
}
this.find = function (target) {
let current = this.root
let [layer, cols] = [0, []]
while (current !== null) {
layer++
if (target === current.data) {
const col = parseInt(cols.join(''), 2)
return [layer - 1, col]
} else if (target < current.data) {
current = current.left
cols.push(0)
} else if (target > current.data) {
current = current.right
cols.push(1)
}
}
return -1
}
this.breadth = function (callback) {
let current = this.root
let res = [current.data]
const deep = (node) => {
node.left && res.push(node.left.data)
node.right && res.push(node.right.data)
node.left && setTimeout(deep, 0, node.left)
node.right && setTimeout(deep, 0, node.right)
callback(res)
}
deep(current)
}
this.depth = function () {
let current = this.root
let res = [current.data]
const deep = (node) => {
node.left && res.push(node.left.data)
node.left && deep(node.left)
node.right && res.push(node.right.data)
node.right && deep(node.right)
return res
}
return deep(current)
}
}
let bst = new BST()
let arr = [56, 22, 81, 10, 30, 77, 92]
arr.forEach(item => bst.insert(item))
const a = bst.preOrder()
const b = bst.inOrder()
const c = bst.postOrder()
console.log(a, b, c)
const d = bst.getMin()
const e = bst.getMax()
console.log(d, e)
const f = bst.find(92)
console.log(f)
bst.breadth(data => { console.log(data) })
const g = bst.depth()
console.log(g)
[56, 22, 81, 10, 30, 77, 92]
前序遍歷:56 22 10 30 81 77 92
中序遍歷:10 22 30 56 77 81 92
後序遍歷:10 30 22 77 92 81 56
最小值:10
最大值:92
鏈表
function LinkedList() {
let Node = function (element) {
this.element = element
this.next = null;
}
let length = 0
let head = null
this.append = function (element) {
let node = new Node(element),
current
if (head === null) {
head = node
} else {
current = head;
while (current.next) {
current = current.next
}
current.next = node
}
length++
}
this.insert = function (position, element) {
if (position >= 0 && position <= length) {
let node = new Node(element),
current = head,
previous,
index = 0
if (position === 0) {
node.next = current
head = node
} else {
while (index++ < position) {
previous = current
current = current.next
}
node.next = current
previous.next = node
}
length++;
return true
} else {
return false
}
}
this.removeAt = function (position) {
if (position > -1 && position < length) {
let current = head,
previous,
index = 0
if (position === 0) {
head = current.next
} else {
while (index++ < position) {
previous = current
current = current.next
}
previous.next = current.next
}
length--
return current.element
} else {
return null
}
}
this.remove = function (element) {
let index = this.indexOf(element)
this.removeAt(index)
}
this.indexOf = function (element) {
let current = head,
index = 0
while (current) {
if (current.element === element) {
return index
}
index++
current = current.next
}
return -1
}
this.isEmpty = function () {
return length === 0
}
this.size = function () {
return length
}
this.getHead = function () {
return head
}
this.toString = function () {
let current = head,
string = ""
while (current) {
string += "," + current.element
current = current.next
}
return string.slice(1)
}
}
let list = new LinkedList()
for (let i = 0; i < 5; i++) { list.append(i) }
list.remove(3)
list.removeAt(3)
console.log(list.toString())