[JS data structure and algorithm] Encapsulation of binary search tree

Use chain storage structure to encapsulate search binary tree

thought:

From the definition of the binary tree, the node of the binary tree is composed of a data element and two branches pointing to the left and right nodes respectively, so the nodes in the linked list representing the binary tree must contain at least three fields.

  • data: Data field.
  • lchild: Left pointer domain.
  • rchild: Right pointer field.

Encapsulate binary search tree code

function BinarySearchTree(){

      // 新结点创建的构造函数
      function Node(data){
        this.data = data;
        this.lchild = null;
        this.rchild = null;
      }

      // 保存根的属性
      this.root = null;

      // 创建关于二叉搜索树的相关操作方法

}

Code analysis

  • Encapsulates the constructor of BinarySearchTree .
  • Encapsulate an internal class to store each node Node.
  • This inner class contains three attributes : the data value corresponding to the node, the pointer field to the left subtree and the pointer field to the right subtree (both initially point to null).
  • For a tree, the root is stored in the root , and then other nodes can be found through the root node.

Various ways to view binary search trees

[JS data structure] Common methods of binary search tree

Guess you like

Origin blog.csdn.net/weixin_42339197/article/details/99705894