Saturday, March 15, 2014

Binary Search Trees

Previously I wrote about binary trees and this week I am going to talk about binary search trees, shortly BST. A BST is a binary tree, but every binary tree is not a BST. That might sound complicated at first, but let me explain.

A binary search tree is a binary tree with the following properties:

  • All the values of the tree on left node should be less than root.
  • All the values of the tree on right node should be more than root.
  • No values should be duplicated anywhere in the tree.
So if we were declaring a class BST that class could be a subclass of a binary tree class, since a BST has all the attributes of a binary tree. 

BST is designed this way because a binary tree arranged this way allows to search for a value on the tree way too easy. All you have to do is start from root and if root is smaller than what you are looking for then go search the right node or vice versa and if root is what you're looking for, then you are in luck. Continuing this algorithm would make finding a value on a really large BST really easy. And this algorithm is called "Binary Search", hence the name binary search tree.

When I look back I realize that this term/course went too fast, in 20 days or so lectures will end and then the exams and all that stuff. I just feel like we started yesterday. I'm not trying to say the course is too easy that it went too fast don't get me wrong. But this is nice, after all it means the summer is coming, yeah I know that is the opposite of the Game of Thrones quote. But wait.. Game of Thrones Season 4 is also coming, right during the exams, but some things have a price I believe. Anyways, I hope everyone is having a nice time, until next time.

No comments:

Post a Comment