Javascript 为了在广度优先搜索算法中打印水平,我应该做什么。
为了在二叉树中按级别打印值,可以在下面的算法中执行什么操作Javascript 为了在广度优先搜索算法中打印水平,我应该做什么。,javascript,Javascript,为了在二叉树中按级别打印值,可以在下面的算法中执行什么操作 BinarySearchTree.prototype.breadthFirstTraversal = function() { console.log("Breadth First Traversal"); var q = [] q.push(this.root);//You don't need to write the root here, it will be written in the loop
BinarySearchTree.prototype.breadthFirstTraversal = function() {
console.log("Breadth First Traversal");
var q = []
q.push(this.root);//You don't need to write the root here, it will be written in the loop
while (q.length > 0)
{
var n = q.shift();
console.log(n.value); //Only write the value when you dequeue it
if (n.left !=null)
{
q.push(n.left);//enqueue the left child
}
if (n.right !=null)
{
q.push(n.right);//enque the right child
}
}
};
存储每个节点的级别,并仅在级别更改时打印它们:
BinarySearchTree.prototype.breadthFirstTraversal = function() {
console.log("Breadth First Traversal");
var q = [];
var prevLevel = -1; // monitor level switch
var byLevel = []; // store node values for printing
this.root.level = 0; // set root level
q.push(this.root);//You don't need to write the root here, it will be written in the loop
byLevel.push(this.root.value);
while (q.length > 0)
{
var n = q.shift();
// Next level, print and reset
if (n.level > prevLevel) {
console.log(byLevel);
byLevel = [];
prevLevel = n.level;
}
if (n.left !=null)
{
n.left.level = n.level + 1;
q.push(n.left);//enqueue the left child
byLevel.push(n.left.value);
}
if (n.right !=null)
{
n.right.level = n.level + 1;
q.push(n.right);//enque the right child
byLevel.push(n.right.value);
}
}
if (byLevel.length > 0) {
console.log(byLevel);
}
};
存储每个节点的级别,并仅在级别更改时打印它们:
BinarySearchTree.prototype.breadthFirstTraversal = function() {
console.log("Breadth First Traversal");
var q = [];
var prevLevel = -1; // monitor level switch
var byLevel = []; // store node values for printing
this.root.level = 0; // set root level
q.push(this.root);//You don't need to write the root here, it will be written in the loop
byLevel.push(this.root.value);
while (q.length > 0)
{
var n = q.shift();
// Next level, print and reset
if (n.level > prevLevel) {
console.log(byLevel);
byLevel = [];
prevLevel = n.level;
}
if (n.left !=null)
{
n.left.level = n.level + 1;
q.push(n.left);//enqueue the left child
byLevel.push(n.left.value);
}
if (n.right !=null)
{
n.right.level = n.level + 1;
q.push(n.right);//enque the right child
byLevel.push(n.right.value);
}
}
if (byLevel.length > 0) {
console.log(byLevel);
}
};
只是想添加它,因为它不需要像上面的解决方案那样存储每个对应节点的级别,所以不需要向对象添加属性。如果你仔细想想,这个队列最多有两个不同的级别。因此,您只需要一个全局标记就可以知道您是否正在处理一个新级别,还需要一个标志来知道您是否应该标记正在推送的新右或左节点
BinarySearchTree.prototype.breadthFirstTraversal = function() {
console.log("Breadth First Traversal");
var q = new Array();
q.push(this.root);
var printArray = [];
var printArray.push(this.root.value);
var newlevel = false;
var marker = 1;
var level = 0;
while(q.length > 0){
if(marker==1){
console.log("level: " + level);
console.log(printArray);
newlevel = true;
}
var n = q.shift();
printArray.shift();
marker -= 1;
//console.log(n.value);
if(n.left != null){
q.push(n.left);
printArray.push(n.left.value);
if(newlevel){
marker = q.length;
level+=1;
newlevel = false;
}
}
if(n.right != null){
q.push(n.right);
printArray.push(n.right.value);
if(newlevel){
marker = q.length;
level+=1;
newlevel = false;
}
}
}
};
只是想添加它,因为它不需要像上面的解决方案那样存储每个对应节点的级别,所以不需要向对象添加属性。如果你仔细想想,这个队列最多有两个不同的级别。因此,您只需要一个全局标记就可以知道您是否正在处理一个新级别,还需要一个标志来知道您是否应该标记正在推送的新右或左节点
BinarySearchTree.prototype.breadthFirstTraversal = function() {
console.log("Breadth First Traversal");
var q = new Array();
q.push(this.root);
var printArray = [];
var printArray.push(this.root.value);
var newlevel = false;
var marker = 1;
var level = 0;
while(q.length > 0){
if(marker==1){
console.log("level: " + level);
console.log(printArray);
newlevel = true;
}
var n = q.shift();
printArray.shift();
marker -= 1;
//console.log(n.value);
if(n.left != null){
q.push(n.left);
printArray.push(n.left.value);
if(newlevel){
marker = q.length;
level+=1;
newlevel = false;
}
}
if(n.right != null){
q.push(n.right);
printArray.push(n.right.value);
if(newlevel){
marker = q.length;
level+=1;
newlevel = false;
}
}
}
};