Javascript:将二叉搜索树转换为双链表的算法

Javascript:将二叉搜索树转换为双链表的算法,javascript,algorithm,linked-list,binary-search-tree,Javascript,Algorithm,Linked List,Binary Search Tree,我正在尝试编写将BST转换为双链表的算法。这就是我目前所拥有的。代码如下: 功能树节点(val){ this.val=val; this.left=this.right=null; } 函数BinaryTree(){ this.root=null; } BinaryTree.prototype.push=函数(val){ var root=this.root; 如果(!root){ this.root=新树节点(val); 返回; } var currentNode=root; var new

我正在尝试编写将BST转换为双链表的算法。这就是我目前所拥有的。代码如下:

功能树节点(val){
this.val=val;
this.left=this.right=null;
}
函数BinaryTree(){
this.root=null;
}
BinaryTree.prototype.push=函数(val){
var root=this.root;
如果(!root){
this.root=新树节点(val);
返回;
}
var currentNode=root;
var newNode=新树节点(val);
while(当前节点){
if(valcurrentNode.val){
如果(!currentNode.right){
currentNode.right=newNode;
打破
}否则{
currentNode=currentNode.right;
}
}
}
}
var bt=新的二进制树();
bt.push(4);
bt.push(2);
bt.push(5);
bt.push(1);
bt.push(3);
//控制台日志(bt);
//var node=bt.root;
功能节点(节点){
//这个数据=值;
//this.previous=this.next=null;
var头=空;
var-tail=null;
var-prev=null;
log(bstToLL(节点、头、前、尾));
}
//函数DoublyLinkedList(){
//this.head=null;
//this.prev=null;
//this.tail=null;
//}
功能(节点、头部、前部、尾部){
如果(节点===null){
返回;
}
bstToLL(节点左、头、上、尾);
if(head==null){
头部=节点;
//控制台日志(头)
}
如果(上一个===null){
prev=节点;
//console.log(上一个)
}否则{
//console.log(节点);
//控制台日志(prev);
node.left=prev;
prev.right=节点;
}
prev=节点
bstToLL(节点、右侧、头部、前部、尾部);
if(node.right==null){
尾=节点;
}
回流头;
}

节点(bt.root)以下是一些将二叉树转换为链接列表的代码。它将按以下顺序记录
1
2
3
4
5

function TreeNode(left, value, right) {
  this.left = left;
  this.value = value;
  this.right = right;
}

function ListNode(prev, value, next) {
  this.prev = prev;
  this.value = value;
  this.next = next;
}

function LinkedList(head, tail) {
  if (tail === undefined) tail = head;
  this.head = head;
  this.tail = tail;
}
LinkedList.prototype.addToStart = function(list) {
  this.head.prev = list.tail;
  list.tail.next = this.head;
  this.head = list.head;
}
LinkedList.prototype.addToEnd = function(list) {
  this.tail.next = list.head;
  list.head.prev = this.tail;
  this.tail = list.tail;
};

function bstToLL(tree) {
  var centerNode = new ListNode(null, tree.value, null);
  var list = new LinkedList(centerNode);
  if (tree.left) list.addToStart(bstToLL(tree.left));
  if (tree.right) list.addToEnd(bstToLL(tree.right));
  return list;
}

var tree = new TreeNode(
  new TreeNode(
    new TreeNode(null, 1, null),
    2,
    new TreeNode(null, 3, null)
  ),
  4,
  new TreeNode(null, 5, null)
);
var linkedList = bstToLL(tree);
for (var node = linkedList.head; node; node = node.next) console.log(node.value);

下面是一些将二叉树转换为
LinkedList
的代码。它将按以下顺序记录
1
2
3
4
5

function TreeNode(left, value, right) {
  this.left = left;
  this.value = value;
  this.right = right;
}

function ListNode(prev, value, next) {
  this.prev = prev;
  this.value = value;
  this.next = next;
}

function LinkedList(head, tail) {
  if (tail === undefined) tail = head;
  this.head = head;
  this.tail = tail;
}
LinkedList.prototype.addToStart = function(list) {
  this.head.prev = list.tail;
  list.tail.next = this.head;
  this.head = list.head;
}
LinkedList.prototype.addToEnd = function(list) {
  this.tail.next = list.head;
  list.head.prev = this.tail;
  this.tail = list.tail;
};

function bstToLL(tree) {
  var centerNode = new ListNode(null, tree.value, null);
  var list = new LinkedList(centerNode);
  if (tree.left) list.addToStart(bstToLL(tree.left));
  if (tree.right) list.addToEnd(bstToLL(tree.right));
  return list;
}

var tree = new TreeNode(
  new TreeNode(
    new TreeNode(null, 1, null),
    2,
    new TreeNode(null, 3, null)
  ),
  4,
  new TreeNode(null, 5, null)
);
var linkedList = bstToLL(tree);
for (var node = linkedList.head; node; node = node.next) console.log(node.value);

请在问题中包含所有相关代码。该链接不保证在将来仍然有效。请在问题本身中包含所有相关代码。该链接不能保证将来仍然有效