Javascript converts an array from small to large into a binary search tree

陈政宽~
Release: 2017-06-28 12:47:07
Original
1127 people have browsed it

This article mainly introduces the relevant information ofJavascriptconvertingarraysfrom small to large into binarysearchtrees. Friends in need can refer to the following

No more nonsense, let me just post the code for you. The specific code is as follows:

var Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var Tree = createTree(Array); console.log(Tree); // 构造一个节点 function Node(nodeData, leftData, rightData) { this.nodeData = nodeData; this.leftData = leftData; this.rightData = rightData; } // 每次取中点作为根节点,向左和向右递归 function createTree(array) { if (array.length <= 0) { return null; } else { var mid = parseInt(array.length / 2); var node = new Node(array[mid], null, null); var leftArray = array.slice(0 , mid); var rightArray = array.slice(mid + 1 , array.length ); node.leftData = createTree(leftArray); node.rightData = createTree(rightArray); return node; } }
Copy after login

The above is the Javascript implementation introduced by the editor to convert small to large arrays. into a binary search tree. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Script House website!

The above is the detailed content of Javascript converts an array from small to large into a binary search tree. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!