I guess I should try writing a post about a lecture topic at some point, so here goes.
On Monday we were introduced to Binary Search Trees -- binary trees that are sorted by value. They are used in opposition to regular, unsorted binary trees in order to apply a search method that has greater than O(n) efficiency.
How It Works
The binary tree has a number in each node. Any nodes with numbers less than the root node go to the left, and nodes with numbers greater than the root node go to the right. And so on and so on in a great branching tree.
This means that at each check, 'half' the tree can be eliminated. I put half in quotes because this only works if the tree is balanced (the right and left branch have the same number of nodes). As shown in class, its possible to have a binary tree which only makes use of one of its children. In this case, no extra elimination is possible.
My Question
How does the efficiency of finding a value in a binary search tree compare to the efficiency of finding a value in a sorted list. It seems to me that searching through a binary tree follows the same process as doing a binary search on a regular sorted list (non-tree format). If there is no efficiency difference, what is the benefit? It was shown in class that a binary search tree isn't that helpful when your tree isn't balanced. Which means an extra step on inserting to the tree is necessary to keep the search efficient. In addition, reshuffling a tree after deleting an item seems much more difficult than deleting an item from a list.
The only benefit to the binary tree that I can see is the linking. However, I still can't find a situation where linking would be useful. Any suggestions?
This is a very good question Lauren. In terms of efficiency, it depends what is the the data your dealing with it. If you already have a sorted list, the cost of search in the list is O(log n), which is similar to searching in a BST. (I am hoping you are familiar with the big O notation.) But how about creating (and/or maintaining) the BST versus creating (and/or maintaining) a sorted list?
ReplyDelete