Sunday, March 30, 2014

Week 9 -- Binary Search Tree

In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • The left and right subtree each must also be a binary search tree.
  • There must be no duplicate nodes.
Because of the special structure of the binary tree, it's easier for us to search an element which is in the tree. We just need to make a recursion: if the element is greater than the root, we search the right tree in the same way; if the element is smaller than the root, we search the left tree.

No comments:

Post a Comment