Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

JavaScript defines how to write the code for a binary search tree.

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to write the code of the JavaScript definition binary search tree. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

The binary search tree consists of nodes, so we define a Node object with the following code:

Function Node (data,left,right) {/ / Node class this.data=data; this.left=left; this.right=right; this.show=show;} function show () {/ / display the data in the node return this.data;}

Left and right are used to point to the left and right child nodes, respectively.

Next, you need to create the class of the binary lookup tree, as follows:

Function BST () {/ / tree class this.root=null; this.insert=insert; this.inOrder=inOrder; this.preOrder=preOrder; this.postOrder=postOrder;}

Next is the code to insert the node. Go through the small ones on the left and the big ones on the right. The code is as follows:

Function insert (data) {/ / insert operation var n=new Node (data,null,null); if (this.root==null) {/ / first element this.root=n;} else {var current=this.root;// always points to the root node var parent; while (true) {/ / until the left or right node is found parent=current; if (data

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report