此次来相识一下二叉树
,以及响应的算法。以下代码并不是一切都由本人所写,只是在此分享出来,以便人人进修。
有关javascript算法,数据结构的代码已上传至 javascript算法与数据结构。
主要内容:
/**
* 运用js完成一个二叉树。
* Tree 组织函数
* traverseDF 深度优先遍历
* traverseBF 广度优先遍历
* insert 插进去
* inOrderTraverse中序遍历
* preOrderTraverse前序遍历
* postOderTraverse后序遍历
*/
声明一棵树:
function Tree () {
this._root;
}
声明一个节点:
function Node (data) {
this.data = data;
this.left = null;
this.right = null;
}
相干算法:
深度优先遍历
/**
* 深度优先遍历,先检察左孩子是不是存在,若存在,传入recurse递归,
* 不然,再检察右孩子。若都不存在,对该节点实行callback操纵。
*/
Tree.prototype.traverseDF = function (callback) {
(function recurse (currentNode) {
if (currentNode.left) {
recurse(currentNode.left);
}
if (currentNode.right) {
recurse(currentNode.right);
}
callback(currentNode);
})(this._root)
}
宽度优先遍历
/**
* 宽度优先遍历借助行列来完成。
*/
Tree.prototype.traverseBF = function (callback) {
var queue = new Queue();
if (!this._root) {
console.log('empty tree');
return;
}
queue.enqueue(this._root);
var currentNode = queue.dequeue();
while (currentNode) {
if (currentNode.left) {
queue.enqueue(currentNode.left);
}
if (currentNode.right) {
queue.enqueue(currentNode.right);
}
callback(currentNode);
currentNode = queue.dequeue();
}
}
插进去树接节点:
/**
* 插进去节点用到了宽度优先遍历的头脑
*/
Tree.prototype.insert = function (data) {
var node = new Node(data);
var message = {
success: "Inserted successfully!",
}
if (!this._root) {
this._root = node;
return;
}
var queue = new Queue();
queue.enqueue(this._root);
var currentNode = queue.dequeue();
while (currentNode) {
if (currentNode.left) {
queue.enqueue(currentNode.left);
} else {
currentNode.left = node;
console.log(message.success);
return;
}
if (currentNode.right) {
queue.enqueue(currentNode.right);
} else {
currentNode.right = node;
console.log(message.success);
return;
}
currentNode = queue.dequeue();
}
}
中序遍历:
/**
* 中序遍历
*/
Tree.prototype.forInOrder = function (node) {
if (node) {
this.forInOrder(node.left);
console.log(node.data);
this.forInOrder(node.right);
}
}
Tree.prototype.inOrderTraverse = function () {
this.forInOrder(this._root);
}
中序遍历的非递归算法
/**
* 借助一个栈,先沿着左子树到恭弘=叶 恭弘节点,顺次入栈,
* 再出栈遍历,对该栈顶节点的右子树举行一致的操纵
*/
Tree.prototype.inOrder = function (callback) {
var currentNode = null;
if (this.root) {
currentNode = root;
} else {
return;
}
var stack = [];
do {
while (currentNode != null) {
stack.push(currentNode);
currentNode = currentNode.left;
}
if (!stack.length) {
stack.pop(currentNode);
callback(currentNode);
currentNode = currentNode.right;
}
} while (currentNode !== null && stack.length)
}
前序遍历
/**
* 前序遍历
*/
Tree.prototype.forPreOrder = function (node) {
if (node) {
console.log(node.data);
this.forPreOrder(node.left);
this.forPreOrder(node.right);
}
}
Tree.prototype.preOrderTraverse = function () {
this.forPreOrder(this._root);
}
固然另有前序遍历的非递归算法。
/**
* 算法症结头脑是用栈为右子树预留位置。
* 能够应用数组作为一个栈。
*/
Tree.prototype.preOrder = function (callback) {
var currentNode = null;
if (this.root) {
currentNode = this.root;
} else {
return;
}
var stack = [];
while (currentNode) {
callback(currentNode);
if (currentNode.right) {
stack.push(currentNode.right);
}
if (currentNode.left) {
currentNode = currentNode.left;
} else {
currentNode = stack.pop();
}
}
}
后序遍历
/**
* 后序遍历
*/
Tree.prototype.forPostOrder = function (node) {
if (node) {
this.forPostOrder(node.left);
this.forPostOrder(node.right);
console.log(node.data);
}
}
Tree.prototype.postOderTraverse = function () {
this.forPostOrder(this._root);
}
末了给出行列的完成
function Queue () {
this._oldestIndex = 1;
this._newestIndex = 1;
this._storage = {};
}
Queue.prototype.enqueue = function (data) {
this._storage[this._newestIndex] = data;
this._newestIndex++;
}
Queue.prototype.dequeue = function () {
var oldestIndex = this._oldestIndex,
newestIndex = this._newestIndex,
deletedData;
if (oldestIndex !== newestIndex) {
deletedData = this._storage[oldestIndex];
delete this._storage[oldestIndex];
this._oldestIndex++;
return deletedData;
}
return null;
}