Binary search tree

In computer science, a binary search tree is a binary tree where every node has a value, every node's left subtree has values less than the node's value, and every right subtree has values greater. Note that this requires a linear order on the values. A new node is added as a leaf. There is a sort algorithm based on binary search trees, and also a search algorithm. An in-order traversal of a binary search tree will visit the values in increasing order.

If we write our binary tree nodes as triples (left subtree, node, right subtree), and the null pointer as None, we can build and search them as follows (in Python):

def binary_tree_insert(treenode, value):
   if treenode is None: return (None, value, None)
   left, nodevalue, right = treenode
   if nodevalue > value:
       return (binary_tree_insert(left, value), nodevalue, right)
   else:
       return (left, nodevalue, binary_tree_insert(right, value))

def build_binary_tree(values):
   tree = None
   for v in values:
       tree = binary_tree_insert(tree, v)
   return tree

def search_binary_tree(treenode, value):
   if treenode is None: return None  # failure
   left, nodevalue, right = treenode
   if nodevalue > value:
       return search_binary_tree(left, value)
   elif value > nodevalue:
       return search_binary_tree(right, value)
   else:
       return nodevalue

Note that the worst case of this build_binary_tree routine is O(n2) --- if you feed it a sorted list of values, it chains them into a linked list with no left subtrees. For example, build_binary_tree([1, 2, 3, 4, 5]) yields the tree (None, 1, (None, 2, (None, 3, (None, 4, (None, 5, None))))). There are a variety of schemes for overcoming this flaw with simple binary trees.

Once we have a binary tree in this form, a simple inorder traversal can give us the node values in sorted order:

def traverse_binary_tree(treenode):
   if treenode is None: return []
   else:
       left, value, right = treenode
       return (traverse_binary_tree(left) + [value] + traverse_binary_tree(right))

So the binary tree sort algorithm is just the following:

def treesort(array):
   array[:] = traverse_binary_tree(build_binary_tree(array))

Types of Binary Search Trees

There are many types of binary search trees. AVL trees and red-black trees are both forms of self-balancing binary search trees. A B-tree grows from the bottom up as elements are inserted. A splay tree is a self-adjusting binary search tree. In a treap ("tree heap"), each node also holds a priority and the parent node has higher priority than its children.

External Links



In the News

Smoking is Associated with Rectal Cancer
Cigarette smoking may be a risk factor for rectal -- but not colon -- cancer. The evidence linking cigarette smoking and colorectal cancer risk has been inconsistent.

All About Peppercorns
Descriptions of black, white, green, and pink peppercorns and their uses in the kitchen. Includes a recipe for skillet-steamed Swiss chard. From a food writer who has written a newspaper column on herbs and spices.

Tackle Poor Sanitation In Developing Countries, Researchers Urge
Improvements in sanitation and sewerage systems can have a dramatic effect on reducing cholera and other diarrheal diseases, research has shown. Improving urban sanitation is an effective way of improving health in developing countries.

Convention on the Elimination of All Forms of Discrimination Against W
Information about this United Nations (UN) convention, "often described as an international bill of rights for women ... [that] defines what constitutes discrimination against women and sets up an agenda for national action to end such discrimination."Provides the text of the convention, history, country reports, meeting information, and other documentation. Some material available in several languages. From the United Nations Division for the Advancement of Women.

Bumblebee See, Bumblebee Do
Just as travelers figure out which restaurant is good by the numbers of cars in the parking lot, bumblebees decide which flowers to visit by seeing which ones already have bee visitors. The finding is the first demonstration that insects can learn by just watching the behavior of other insects.

Curbing C. Difficile's Toxin Production
Researchers have discovered how the protein CodY controls toxin production of Clostridium difficile, a bacterium that has caused epidemics of severe diarrhea in hospital patients. In its search for food, C. difficile releases toxins that cause diarrhea and in rare cases death. Gaining a better understanding of how CodY prevents C. difficile from making toxins may lead to future drug development based on the properties of CodY.

Gene Mutation Thought To Control Energy Levels Discovered
Scientists have discovered a mutation in a gene that is widely considered to be the major controller of energy levels in our bodies. The discovery has significant implications for people suffering from diabetes and for endurance athletes. This study focused on the gene for AMPK (adenosine monophosphate-activated protein kinase), which controls the amount of energy in our cells by becoming active when fuel stores start to deplete, such as during exercise.

Insights Into Anemia Control In Dialysis Patients
Anemia is one of the most frequent complications of hemodialysis, and its correction is an important factor in restoring a tolerable quality of life to dialysis-dependent patients. Treatment with drugs called erythropoiesis-stimulating agents (ESAs), which stimulate the bone marrow to produce red blood cells, have been a major advance in the treatment of the anemia (low levels of red blood cells or hemoglobin, which carries oxygen in the blood) in chronic kidney disease (CKD).

Name the Year's Sexiest Geeks
Who blew your mind with their intellect in 2006, giving you that special tingle inside? Get your nominations in for the Wired News sexy geek roster. In Bodyhack.

Low Birth Weight Of A Baby Entails Risks For The Baby's Father
Parents whose children are born with a low birth weight run greater risk of dying of cardiovascular diseases. Even the fathers are at greater risk. These findings are published in a new report by Karolinska Institutet. The report shows that genetic factors affect both birth weight and the risk of cardiovascular diseases.


MP3 Music Downloads

Preview songs, Download Free Music,Burn CDs at ITunes.com
iTunes_RGB_9mm

 


Google




InformationQuickFind.com - Find Information Fast

Links