Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用javascript反转二叉树_Javascript - Fatal编程技术网

用javascript反转二叉树

用javascript反转二叉树,javascript,Javascript,我试图在javascript中反转一个二叉树,但我不明白为什么我不让它打印我的树。看起来我可以在底部的示例代码中创建我的树,但是我无法保存数据 //节点包含值、左指针和右指针 类节点{ 建造师(项目){ 这个数据=项目; this.left=this.right=null; } } 类二叉树{ 构造函数(){ this.root=null; } 倒置{ this.root=this.invert(this.root); } 反转(节点){ if(node==null) 返回节点; /*递归调用

我试图在javascript中反转一个二叉树,但我不明白为什么我不让它打印我的树。看起来我可以在底部的示例代码中创建我的树,但是我无法保存数据

//节点包含值、左指针和右指针
类节点{
建造师(项目){
这个数据=项目;
this.left=this.right=null;
}
}
类二叉树{
构造函数(){
this.root=null;
}
倒置{
this.root=this.invert(this.root);
}
反转(节点){
if(node==null)
返回节点;
/*递归调用*/
设left=this.invert(node.left);
设right=this.invert(node.right);
/*交换左右指针*/
node.left=右;
node.right=左;
返回节点;
}
printree(){
this.printree(this.root);
}
//按顺序打印二叉树遍历。
打印树(节点){
if(node==null)
返回;
这个.printTree(node.left);
console.log(node.data+“”);
这个.printTree(node.right);
}
}
/*测试示例节点*/
常量树=新的二进制树();
tree.root=新节点(2);
tree.root.left=新节点(11);
tree.root.right=新节点(4);
tree.root.right.left=新节点(13);
tree.root.right.right=新节点(5);
/*输入树的日志顺序遍历*/
log(“输入树的顺序遍历为:”);
printTree();
控制台日志(“”);
/*倒树*/
tree.invert();
/*小树的日志顺序遍历*/
log(“二叉树的顺序遍历为:”);

printTree()您不能在javascript中重载函数,它将始终调用相同的函数,而不管哪个函数“匹配”

类演示{
超载(a){
console.log('hi');
}
过载(){
console.log('bye');
}
超载(a、b){
console.log('wow');
}
}
const d=新的演示();
d、 过载();
d、 超载(1);

d、 过载(1,2)我已经创建了自己的js二叉树类。它具有类似reverse()的函数,它将反转树中的所有节点。反向文档-