1 // 树的结构
2 class BTNode {
3 constructor() {
4 this.key = key;
5 this.lchild = null;
6 this.rchild = 7 }
8 }
1 let BST = (function () {
2
3 let ROOT = Symbol();
4
5 节点结构
6 let BTNode = class {
7 constructor(key) {
8 9 10 11 }
12 13
14 递归插入节点
15 let recursionInsert = (root,node) {
16 if (node.key < root.key) {
17 if (root.lchild) {
18 recursionInsert(root.lchild,node);
19 } else {
20 root.lchild = node;
21 }
22 } 23 (root.rchild) {
24 recursionInsert(root.rchild,1)">25 } 26 root.rchild =27 28 29 30
31 二叉搜索树类
32 return33 constructor() {
34 this[ROOT] = 35 36
37 插入
38 insert(key) {
39 let node = new BTNode(key);
40 let root = this[ROOT];
41 if (!root) {
42 this[ROOT] =43 44 45 递归插入
46 recursionInsert(root,1)">47 48 49
50 })();
51
52
53 let bst = BST();
54
55
56 bst.insert(20);
57 bst.insert(1058 bst.insert(559 bst.insert(1560 bst.insert(1361 bst.insert(1862 bst.insert(1763 bst.insert(3064
65 console.log(bst);
1 let BST = ( 2
3 let ROOT = 4
5 6 let BTNode = 7 8 9 10 11 12 13
14 15 let recursionInsert = 16 17 18 19 } 20 root.lchild = 21 22 } 23 24 25 } 26 root.rchild = 27 28 29 };
30
31 用于中序遍历二叉树的方法
32 let inorderTraversal = 33 if (!root) 34 inorderTraversal(root.lchild,arr);
35 arr.push(root.key);
36 inorderTraversal(root.rchild,1)"> 37 38
39 用于先序遍历的递归函数
40 let preOrderTraversal = 41 42 43 preOrderTraversal(root.lchild,1)"> 44 preOrderTraversal(root.rchild,1)"> 45 46
47 用于后续遍历的递归函数
48 let lastOrderTraversal = 49 50 lastOrderTraversal(root.lchild,1)"> 51 lastOrderTraversal(root.rchild,1)"> 52 53 54
55 56 57 58 59 60
61 62 63 let node = 64 let root = 65 66 67 68 69 70 71 72
73
74 中序遍历二叉树
75 inorderTraversal() {
76 let arr = [];
77 inorderTraversal([ROOT],1)"> 78 arr;
79 80
81 先序遍历二叉树
82 preOrderTraversal() {
83 let arr = 84 preOrderTraversal( 85 86 87
88 后续遍历
89 lastOrderTraversal() {
90 let arr = 91 lastOrderTraversal( 92 93 94 95
96 97
98
99 let bst = 100
101 bst.insert(20102 bst.insert(15103 bst.insert(7104 bst.insert(40105 bst.insert(30106 bst.insert(45107 bst.insert(50108
109
110 console.log(bst);
111
112
113 let a = bst.inorderTraversal();
114 let b = bst.preOrderTraversal();
115 let c = bst.lastOrderTraversal();
116
117 console.log(a);
118 console.log(b);
119 console.log(c);
广度优先遍历
breadthRirstSearch() {
初始化用于广度优先遍历的队列
let queue = Queue();
console.log('根节点',[ROOT]);
let arr = [];
let root = [ROOT];
;
queue.enqueue(root);
while (queue.size()) {
let queueFirst = queue.dequeue();
arr.push(queueFirst.key);
queueFirst.lchild && queue.enqueue(queueFirst.lchild);
queueFirst.rchild && queue.enqueue(queueFirst.rchild);
}
arr;
}
栈结构 用来辅助非递归遍历
class Stack {
constructor() {
this.items = [];
}
push(data) {
.items.push(data);
}
pop() {
return .items.pop();
}
peek() {
this.items[this.items.length - 1];
}
size() {
.items.length;
}
}
preOrderTraversal() {
console.log('先序遍历');
let root = 初始化辅助遍历存储的栈
let stack = Stack();
let arr = []; 用于存储先序遍历的顺序
stack.push(root);
如果栈不为空 则一直走
(stack.size()) {
let stackTop = stack.pop();
访问栈顶元素
arr.push(stackTop.key);
stackTop.rchild && stack.push(stackTop.rchild);
stackTop.lchild && stack.push(stackTop.lchild);
}
arr;
}
inorderTraversal() {
初始化用于辅助排序的栈
let stack = Stack;
let p = null; 用于指向当前遍历到的元素
let arr = []; 用户记录排序的顺序
p = [ROOT];
while (stack.size() || p !== ) {
while (p !== ) {
stack.push(p);
p = p.lchild;
}
如果栈不为空 出栈
(stack.size()) {
p = stack.pop();
arr.push(p.key);
p = p.rchild;
}
}
arr;
}