262 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			262 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | ||
|  | id: 587d8258367417b2b2512c7f | ||
|  | title: 二分探索木に幅優先探索を使用する | ||
|  | challengeType: 1 | ||
|  | forumTopicId: 301718 | ||
|  | dashedName: use-breadth-first-search-in-a-binary-search-tree | ||
|  | --- | ||
|  | 
 | ||
|  | # --description--
 | ||
|  | 
 | ||
|  | ここでは、木を走査するもう一つの方法である幅優先探索について説明します。 直前のチャレンジで学んだ深さ優先探索とは対照的に、幅優先探索は、次のレベルに進む前に、木の中において与えられたレベルに位置するノードをすべて探索します。 一般に、幅優先探索アルゴリズムの設計において、ヘルパーデータ構造としてキューが利用されます。 | ||
|  | 
 | ||
|  | このメソッドでは、まず、キューに根ノードを追加します。 次に、キューの先頭要素をキューから取り出す操作のループを開始し、それを新しい配列に追加し、次に、その要素の子部分木を両方とも調べます。 子が null でない場合、それぞれがキューに追加されます。 このプロセスはキューが空になるまで続きます。 | ||
|  | 
 | ||
|  | # --instructions--
 | ||
|  | 
 | ||
|  | `levelOrder` という名前の木に幅優先探索メソッドを作成しましょう。 このメソッドは、幅優先方式で探索された、すべての木ノードの値が含まれている配列を返す必要があります。 ノード自体ではなく、配列の値を返すようにしてください。 各レベルは左から右へ走査される必要があります。 次に、これに似た `reverseLevelOrder` と呼ばれるメソッドを書きましょう。このメソッドは同じ探索を行いますが、各レベルでの方向が逆 (右から左) になります。 | ||
|  | 
 | ||
|  | # --hints--
 | ||
|  | 
 | ||
|  | `BinarySearchTree` データ構造が存在する必要があります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } | ||
|  |     return typeof test == 'object'; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | 二分探索木に `levelOrder` というメソッドが必要です。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } else { | ||
|  |       return false; | ||
|  |     } | ||
|  |     return typeof test.levelOrder == 'function'; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | 二分探索木に `reverseLevelOrder` というメソッドが必要です。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } else { | ||
|  |       return false; | ||
|  |     } | ||
|  |     return typeof test.reverseLevelOrder == 'function'; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `levelOrder` メソッドは、レベル順に探索された木ノード値の配列を返す必要があります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } else { | ||
|  |       return false; | ||
|  |     } | ||
|  |     if (typeof test.levelOrder !== 'function') { | ||
|  |       return false; | ||
|  |     } | ||
|  |     test.add(7); | ||
|  |     test.add(1); | ||
|  |     test.add(9); | ||
|  |     test.add(0); | ||
|  |     test.add(3); | ||
|  |     test.add(8); | ||
|  |     test.add(10); | ||
|  |     test.add(2); | ||
|  |     test.add(5); | ||
|  |     test.add(4); | ||
|  |     test.add(6); | ||
|  |     return test.levelOrder().join('') == '719038102546'; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `reverseLevelOrder` メソッドは、レベル順の逆順に探索された木ノード値の配列を返す必要があります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } else { | ||
|  |       return false; | ||
|  |     } | ||
|  |     if (typeof test.reverseLevelOrder !== 'function') { | ||
|  |       return false; | ||
|  |     } | ||
|  |     test.add(7); | ||
|  |     test.add(1); | ||
|  |     test.add(9); | ||
|  |     test.add(0); | ||
|  |     test.add(3); | ||
|  |     test.add(8); | ||
|  |     test.add(10); | ||
|  |     test.add(2); | ||
|  |     test.add(5); | ||
|  |     test.add(4); | ||
|  |     test.add(6); | ||
|  |     return test.reverseLevelOrder().join('') == '791108305264'; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `levelOrder` メソッドは、空の木の場合に `null` を返す必要があります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } else { | ||
|  |       return false; | ||
|  |     } | ||
|  |     if (typeof test.levelOrder !== 'function') { | ||
|  |       return false; | ||
|  |     } | ||
|  |     return test.levelOrder() == null; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `reverseLevelOrder` メソッドは、空の木の場合に `null` を返す必要があります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     var test = false; | ||
|  |     if (typeof BinarySearchTree !== 'undefined') { | ||
|  |       test = new BinarySearchTree(); | ||
|  |     } else { | ||
|  |       return false; | ||
|  |     } | ||
|  |     if (typeof test.reverseLevelOrder !== 'function') { | ||
|  |       return false; | ||
|  |     } | ||
|  |     return test.reverseLevelOrder() == null; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | # --seed--
 | ||
|  | 
 | ||
|  | ## --after-user-code--
 | ||
|  | 
 | ||
|  | ```js | ||
|  | BinarySearchTree.prototype = Object.assign( | ||
|  |   BinarySearchTree.prototype, | ||
|  |   { | ||
|  |     add: function(value) { | ||
|  |       function searchTree(node) { | ||
|  |         if (value < node.value) { | ||
|  |           if (node.left == null) { | ||
|  |             node.left = new Node(value); | ||
|  |             return; | ||
|  |           } else if (node.left != null) { | ||
|  |             return searchTree(node.left); | ||
|  |           } | ||
|  |         } else if (value > node.value) { | ||
|  |           if (node.right == null) { | ||
|  |             node.right = new Node(value); | ||
|  |             return; | ||
|  |           } else if (node.right != null) { | ||
|  |             return searchTree(node.right); | ||
|  |           } | ||
|  |         } else { | ||
|  |           return null; | ||
|  |         } | ||
|  |       } | ||
|  |       var node = this.root; | ||
|  |       if (node == null) { | ||
|  |         this.root = new Node(value); | ||
|  |         return; | ||
|  |       } else { | ||
|  |         return searchTree(node); | ||
|  |       } | ||
|  |     } | ||
|  |   } | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | ## --seed-contents--
 | ||
|  | 
 | ||
|  | ```js | ||
|  | var displayTree = tree => console.log(JSON.stringify(tree, null, 2)); | ||
|  | function Node(value) { | ||
|  |   this.value = value; | ||
|  |   this.left = null; | ||
|  |   this.right = null; | ||
|  | } | ||
|  | function BinarySearchTree() { | ||
|  |   this.root = null; | ||
|  |   // Only change code below this line | ||
|  | 
 | ||
|  |   // Only change code above this line | ||
|  | } | ||
|  | ``` | ||
|  | 
 | ||
|  | # --solutions--
 | ||
|  | 
 | ||
|  | ```js | ||
|  | var displayTree = tree => console.log(JSON.stringify(tree, null, 2)); | ||
|  | function Node(value) { | ||
|  |   this.value = value; | ||
|  |   this.left = null; | ||
|  |   this.right = null; | ||
|  | } | ||
|  | function BinarySearchTree() { | ||
|  |   this.root = null; | ||
|  |   // Only change code below this line | ||
|  |   this.levelOrder = (root = this.root) => { | ||
|  |     if(!root) return null; | ||
|  |     let queue = [root]; | ||
|  |     let results = []; | ||
|  |     while(queue.length > 0) { | ||
|  |       let node = queue.shift(); | ||
|  |       results.push(node.value); | ||
|  |       if(node.left) queue.push(node.left); | ||
|  |       if(node.right) queue.push(node.right); | ||
|  |     } | ||
|  |     return results; | ||
|  |   } | ||
|  | 
 | ||
|  |   this.reverseLevelOrder = (root = this.root) => { | ||
|  |     if(!root) return null; | ||
|  |     let queue = [root]; | ||
|  |     let results = [] ; | ||
|  |     while ( queue.length > 0) { | ||
|  |       let node = queue.shift(); | ||
|  |       results.push(node.value); | ||
|  |       if(node.right) queue.push(node.right); | ||
|  |       if(node.left ) queue.push(node.left); | ||
|  |     } | ||
|  |     return results; | ||
|  |   } | ||
|  |   // Only change code above this line | ||
|  | } | ||
|  | ``` |