最近正在看慕課網(wǎng)的《Javascript實現(xiàn)二叉樹算法》課程,現(xiàn)把看到的東西記錄下來阱表。
什么是二叉樹殿如?簡單來說,二叉樹是一種具有層級特性的數(shù)據(jù)結(jié)構(gòu)捶枢,一棵樹包含多個節(jié)點握截。節(jié)點自身含有一個屬性,就是他所代表的數(shù)值烂叔。
排序二叉樹具有以下特征:
1谨胞、如果他的左子樹上不為空,則他的左子樹上所有節(jié)點的值都小于根節(jié)點上的值蒜鸡;
2胯努、如果他的右子樹上不為空,則他的右子樹上所有節(jié)點的值都小于根節(jié)點上的值逢防;
3叶沛、他的左、右子樹也是二叉排序樹忘朝;
4灰署、沒有完全相等的兩個節(jié)點;
二叉排序樹的代碼實現(xiàn)
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
</script>
二叉樹的中序遍歷
從根節(jié)點開始局嘁,如果存在左子樹就開始遍歷左子樹溉箕,然后再遍歷右子樹,最后輸出根節(jié)點的值悦昵。遍歷左肴茄、右子樹時也是先遍歷左子樹,然后遍歷右子樹但指,最少輸出改接點寡痰,如此循環(huán)。
代碼實現(xiàn):
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
var inOrderTraverseNode = function(node , callback){
if( node !== null ){
inOrderTraverseNode(node.left , callback);
callback(node.key);
inOrderTraverseNode(node.right , callback);
}
}
this.inOrderTraverse = function(callback){
inOrderTraverseNode(rootNode , callback)
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
var callback = function(key){
console.log(key)
}
console.log("中序遍歷")
// 中序遍歷
binaryTree.inOrderTraverse(callback);
</script>
打印出來的結(jié)果
二叉樹的前序遍歷
從根節(jié)點開始棋凳,先輸出當(dāng)前節(jié)點拦坠,如果存在左子樹就開始遍歷左子樹,然后再遍歷右子樹剩岳。遍歷左贪婉、右子樹的規(guī)則同上。
代碼實現(xiàn):
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
var preOrderTraverseNode = function(node , callback){
if( node !== null ){
callback(node.key);
preOrderTraverseNode(node.left , callback);
preOrderTraverseNode(node.right , callback);
}
}
this.preOrderTraverse = function(callback){
preOrderTraverseNode(rootNode , callback);
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
var callback = function(key){
console.log(key)
}
console.log("前序遍歷")
// 前序遍歷
binaryTree.preOrderTraverse(callback)
</script>
打印出來的結(jié)果
二叉樹的后序遍歷
代碼實現(xiàn):
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
var nextOrderTraverseNode = function(node , callback){
if( node !== null ){
nextOrderTraverseNode(node.left , callback);
nextOrderTraverseNode(node.right , callback);
callback(node.key);
}
}
this.nextOrderTraverse = function(callback){
nextOrderTraverseNode(rootNode , callback);
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
var callback = function(key){
console.log(key)
}
console.log("后序遍歷")
// 后序遍歷
binaryTree.nextOrderTraverse(callback)
</script>
打印結(jié)果
查找最小值
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
var minNode = function(node){
if(node){
while(node && node.left !== null){
node = node.left;
}
return node.key;
}
return null;
}
this.min = function(){
return minNode(rootNode);
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
console.log("查找最小值")
// 查找最小值
console.log(binaryTree.min())
</script>
查找最大值
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
var maxNode = function(node){
if( node ){
while(node && node.right !== null){
node = node.right
}
return node.key
}
return null;
}
this.max = function(){
return maxNode(rootNode);
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
console.log("查找最大值")
// 查找最大值
console.log(binaryTree.max())
</script>
查找某個值
<script>
var BinaryTree = function(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var rootNode = null;
var insertNode = function(node , newNode){
if( newNode.key < node.key ){
if( node.left == null ){
node.left = newNode
}else{
insertNode(node.left , newNode)
}
}else{
if( node.right == null ){
node.right = newNode;
}else{
insertNode(node.right , newNode)
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if( rootNode == null ){
rootNode = newNode;
}else{
insertNode(rootNode , newNode);
}
}
var searchNode = function(num ,node){
if( node == null ){
return false;
}
if( node.key < num ){
return searchNode(num , node.right)
}else if( node.key > num ){
return searchNode(num , node.left)
}else{
return true;
}
}
this.search = function(num){
return searchNode(num , rootNode);
}
}
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key)
})
console.log("查找某個值")
// 查找某個值
var num = 7;
console.log(binaryTree.search(num))
</script>
未完待續(xù)...