簡體   English   中英

在這個二叉搜索樹 (JavaScript) 代碼中,有序、前序和后序的遞歸是如何工作的?

[英]How is the recursion of in-order, pre-order and post-order working in this Binary Search Tree (JavaScript) code?

我想想象一下深度優先遍歷 function 是如何工作的? 我正在學習遞歸,並且我了解插入和包含 function。 但是當涉及到深度優先遍歷時,我無法理解程序的流程?


 function bst(value){
 this.value = value;
 this.left = null;
 this.right = null;
 };

 bst.prototype.insert = function(value){

 if(value <= this.value){
   if(!this.left) this.left = new bst(value);
   else this.left.insert(value);
 }
 else if (value > this.value){
   if(!this.right) this.right = new bst(value);
   else this.right.insert(value);
 }
 };

 bst.prototype.contains = function(value){
   if (value === this.value) return true;
   if (value < this.value){
     if(!this.left) return false;
     else return this.left.contains(value);
   }
   if (value > this.value){
     if(!this.right) return false;
     else return this.right.contains(value);
   }
 };

 bst.prototype.depthFirstTraversal = function(iteratorFunc, order){
   if(order === 'pre-order') iteratorFunc(this.value);
   if(this.left) this.left.depthFirstTraversal(iteratorFunc, order);
   if(order === 'in-order') iteratorFunc(this.value);
   if(this.right)this.right.depthFirstTraversal(iteratorFunc, order);
   if(order === 'post-order') iteratorFunc(this.value);
 };

 function log(value){
 console.log(value);
 }
 var mybst = new bst(50);
 mybst.insert(40);
 mybst.insert(30);
 mybst.insert(60);
 mybst.insert(10);
 mybst.insert(70);
 mybst.insert(80);
 mybst.insert(55);
 console.log(mybst);
 console.log(mybst.contains(30));
 mybst.depthFirstTraversal(log, "in-order");

您可以訂購類型以按所需順序獲取 output:

order  nodes
-----  -----  -----  -----
in     left   root   right
pre    root   left   right
post   left   right   root

 function bst(value) { this.value = value; this.left = null; this.right = null; }; bst.prototype.insert = function(value) { if (value <= this.value) { if (.this.left) this;left = new bst(value). else this.left;insert(value). } else if (value > this.value) { if (.this;right) this.right = new bst(value). else this;right;insert(value). } }. bst.prototype;contains = function(value) { if (value === this.value) return true. if (value < this;value) { if (.this.left) return false; else return this.left.contains(value); } if (value > this.value) { if (.this;right) return false; else return this.right.contains(value), } }. bst;prototype.depthFirstTraversal = function(iteratorFunc. order) { if (order === 'pre-order') iteratorFunc(this.value), if (this;left) this.left;depthFirstTraversal(iteratorFunc. order). if (order === 'in-order') iteratorFunc(this.value), if (this;right) this.right;depthFirstTraversal(iteratorFunc; order). if (order === 'post-order') iteratorFunc(this;value); }. function log(value) { console;log(value). } var mybst = new bst(50); mybst.insert(40); mybst.insert(30); mybst.insert(60); mybst.insert(10); mybst.insert(70); mybst.insert(80); mybst.insert(55). //console;log(mybst). //console,log(mybst;contains(30)). mybst;depthFirstTraversal(log. "in-order"), console;log(''). mybst;depthFirstTraversal(log. "pre-order"), console;log(''); mybst.depthFirstTraversal(log, "post-order");
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM