<!DOCTYPE html>
<html>
<head>
<title>Binary Tree</title>
</head>
<body>
<script type="text/javascript">
function BinaryTree(){
var Node = function(key) {
this.key = key;
this.left = null;
this.right = null;
}
var root = null;
this.insert = function(key) {
var newNode = new Node(key);
if (root === null) {
root = newNode
} else {
inserNode(root, newNode);
}
}
function inserNode(node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
inserNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
inserNode(node.right, newNode);
}
}
}
/*中序遍历*/
this.inOrderTravseral = function(callback){
inOrderTravseralNode(root, callback);
}
var inOrderTravseralNode = function(node, callback) {
if (node !== null) {
inOrderTravseralNode(node.left, callback);
callback(node.key);
inOrderTravseralNode(node.right, callback);
}
}
/*前序遍历*/
this.preOrderTravseral = function(callback){
preOrderTravseralNode(root, callback);
}
function preOrderTravseralNode(node, callback){
if (node != null) {
callback(node.key);
preOrderTravseralNode(node.left, callback);
preOrderTravseralNode(node.right, callback);
}
}
/*后序遍历*/
this.postOrderTravseral = function(callback){
postOrderTravseralNode(root, callback);
}
function postOrderTravseralNode(node, callback){
if (node != null) {
postOrderTravseralNode(node.left, callback);
postOrderTravseralNode(node.right, callback);
callback(node.key);
}
}
/*查找树中的最小节点*/
this.findMin = function(){
return findMinNode(root);
}
function findMinNode(node){
if (node !== null) {
while (node.left !== null) {
node = node.left;
}
return node.key;
}
return null;
}
/*查找树中的最大的节点*/
this.findMax = function(){
return findMaxNode(root);
}
function findMaxNode(node){
if (node !== null) {
while (node.right !== null) {
node = node.right;
}
return node.key;
}
return null;
}
/*查找树中的指定节点*/
this.findOne = function(key){
return findOneNode(root, key);
}
function findOneNode(node, key){
if (node !== null){
if (node.key !== key) {
if (key < node.key) {
return findOneNode(node.left, key);
} else {
return findOneNode(node.right, key);
}
} else {
return node.key
}
}
return null;
/*while (node !== null && node.key !== key){
if (key < node.key) {
node = node.left;
} else {
node = node.right;
}
}
if (node !== null) {
return node.key;
} else {
return null;
}*/
}
/*删除节点*/
this.remove = function(key){
removeNode(root, key);
}
function removeNode(node, key){
if (node === null) {
return null;
}
if (key < node.key) {
node.left = removeNode(node.left, key);
return node;
}else if (key > node.key) {
node.right = removeNode(node.right, key);
return node;
} else {
if (node.left === null && node.right === null) {
node = null;
return node;
}
if (node.left === null) {
node = node.right;
return node;
} else if (node.right === null) {
node = node.left;
return node;
} else {
var temp = findOneNodeMaxInRight(node.right);
node.key = temp;
node.right = removeNode(node.right, temp);
return node;
}
}
/*找出node节点的右子树中最小的节点*/
function findOneNodeMaxInRight(node){
if (node === null) {
return null;
}
while (node.left !== null) {
node = node.left;
}
return node.key;
}
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 16];
var binaryTree = new BinaryTree();
nodes.forEach(function (key) {
binaryTree.insert(key);
});
var callback = function(key) {
console.log(key);
}
binaryTree.inOrderTravseral(callback);
binaryTree.remove(8);
binaryTree.inOrderTravseral(callback);
</script>
</body>
</html>
<html>
<head>
<title>Binary Tree</title>
</head>
<body>
<script type="text/javascript">
function BinaryTree(){
var Node = function(key) {
this.key = key;
this.left = null;
this.right = null;
}
var root = null;
this.insert = function(key) {
var newNode = new Node(key);
if (root === null) {
root = newNode
} else {
inserNode(root, newNode);
}
}
function inserNode(node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
inserNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
inserNode(node.right, newNode);
}
}
}
/*中序遍历*/
this.inOrderTravseral = function(callback){
inOrderTravseralNode(root, callback);
}
var inOrderTravseralNode = function(node, callback) {
if (node !== null) {
inOrderTravseralNode(node.left, callback);
callback(node.key);
inOrderTravseralNode(node.right, callback);
}
}
/*前序遍历*/
this.preOrderTravseral = function(callback){
preOrderTravseralNode(root, callback);
}
function preOrderTravseralNode(node, callback){
if (node != null) {
callback(node.key);
preOrderTravseralNode(node.left, callback);
preOrderTravseralNode(node.right, callback);
}
}
/*后序遍历*/
this.postOrderTravseral = function(callback){
postOrderTravseralNode(root, callback);
}
function postOrderTravseralNode(node, callback){
if (node != null) {
postOrderTravseralNode(node.left, callback);
postOrderTravseralNode(node.right, callback);
callback(node.key);
}
}
/*查找树中的最小节点*/
this.findMin = function(){
return findMinNode(root);
}
function findMinNode(node){
if (node !== null) {
while (node.left !== null) {
node = node.left;
}
return node.key;
}
return null;
}
/*查找树中的最大的节点*/
this.findMax = function(){
return findMaxNode(root);
}
function findMaxNode(node){
if (node !== null) {
while (node.right !== null) {
node = node.right;
}
return node.key;
}
return null;
}
/*查找树中的指定节点*/
this.findOne = function(key){
return findOneNode(root, key);
}
function findOneNode(node, key){
if (node !== null){
if (node.key !== key) {
if (key < node.key) {
return findOneNode(node.left, key);
} else {
return findOneNode(node.right, key);
}
} else {
return node.key
}
}
return null;
/*while (node !== null && node.key !== key){
if (key < node.key) {
node = node.left;
} else {
node = node.right;
}
}
if (node !== null) {
return node.key;
} else {
return null;
}*/
}
/*删除节点*/
this.remove = function(key){
removeNode(root, key);
}
function removeNode(node, key){
if (node === null) {
return null;
}
if (key < node.key) {
node.left = removeNode(node.left, key);
return node;
}else if (key > node.key) {
node.right = removeNode(node.right, key);
return node;
} else {
if (node.left === null && node.right === null) {
node = null;
return node;
}
if (node.left === null) {
node = node.right;
return node;
} else if (node.right === null) {
node = node.left;
return node;
} else {
var temp = findOneNodeMaxInRight(node.right);
node.key = temp;
node.right = removeNode(node.right, temp);
return node;
}
}
/*找出node节点的右子树中最小的节点*/
function findOneNodeMaxInRight(node){
if (node === null) {
return null;
}
while (node.left !== null) {
node = node.left;
}
return node.key;
}
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 16];
var binaryTree = new BinaryTree();
nodes.forEach(function (key) {
binaryTree.insert(key);
});
var callback = function(key) {
console.log(key);
}
binaryTree.inOrderTravseral(callback);
binaryTree.remove(8);
binaryTree.inOrderTravseral(callback);
</script>
</body>
</html>