Tuesday, March 17, 2020


Binary Search Tree

Binary Search Tree or BST is one of the Data Structure that have the same properties as Binary Tree, but more organized. The difference on BST is they are made for easier and faster search. This is accomplished by seperating the data using the root node. If there's already a root node on the tree, then you need to see whether the new node you want to insert is larger or smaller than the root. If the new node is smaller, then you place it on the left side of the root as the child. If the new node is larger, then it goes to the right. This way the tree will be sorted easily.


Binary Search Tree has the following properties:

  1. The left subtree of a node contains only nodes with keys lesser than the node’s key.
  2. The right subtree of a node contains only nodes with keys greater than the node’s key.
  3. The left and right subtree each must also be a binary search tree.




There are 3 functions that BST has:

Search

If you already have BST, you can proceed by searching the node that you're looking for.  You need to pay attention on whether the node that you're looking for is larger or smaller than your current nodes. This mean that everytime you do the search per nodes, check it whether its larger or smaller.


(courtesy of GeeksforGeeks)

Insert


(Courtesy of GeeksforGeeks)

Deletion

As almost the same as insert, you first need to search for the nodes that you want to delete. After you found the nodes that you want to delete, procceed by connecting the childs (If exist) of the node to the parents of the node.




Reference


No comments:

Post a Comment

Summary Linked List Sebelum dimulai, saya ingin memberikan pengertian sedikit apa itu  Linked List .  Linked List  adalah sebuah  Da...