<< Chapter < Page Chapter >> Page >
The binary tree structure can be used as an efficient way to organize data objects that are totally ordered. This is done by maintaining the tree in such a way that for any given subtree, the data elements in its left subtree are less than the root and the data elements in the right subtree are greater than the root. Such a binary tree is called a binary search tree.

Binary search tree

1. binary search tree property (bstp)

Consider the following binary tree of Integer objects.

-7 |_ -55| |_ [] | |_ -16| |_ -20 | | |_ []| | |_ [] | |_ -9| |_ [] | |_ []|_ 0 |_ -4| |_ [] | |_ []|_ 23 |_ []|_ []

Notice the following property:

  • all elements in the left subtree are less than the root element,
  • and the root element is less than all elements in the right subtree.

Moreover, this property holds recursively for all subtrees.  It is called the binary search tree (BST) property.  

In general, instead of Integer objects, suppose we have a set of objects that can be compared for equality with "equal to" and "totally ordered" with an order relation called "less or equal to" .  Define "less than" to mean "less or equal to" AND "not equal to".  Let T be a BiTree structure that stores such totally ordered objects.  

Definition of binary search tree property

The binary search tree property (BSTP) is defined on the binary tree structure as follows.

  • An empty binary tree satisfies the BSTP.
  • A non-empty binary tree T satisfies the BSTP if and only if 
    • the left and right subtrees of T both satisfy BSTP, and 
    • all elements in the left subtree of T are less than the root of T, and 
    • the root of T is less than all elements in the right subtree of T.

We can take advantage of this property when looking up for a particular ordered object in the tree.  Instead of scanning the whole tree for the search target, we can compare the search target against the root element and narrow the search to the left subtree or the right subtree if necessary.  So in the worst possible case, the number of comparisons is proportional to the height of the binary tree.  This is a big win if the tree is balanced .  It can be proven that when a tree containing N elements is balanced, its height is at most a constant multiple of logN.  For example, the height of a balanced tree containing 10 6 elements is at most a fixed multiple of 6.  Here is the definition of what a balanced tree is.

Definition of balanced tree

  • An empty tree is balanced .
  • A non-empty tree is balanced if and only if
    •  its subtrees are balanced and
    • the heights of the subtrees differ by a fixed constant or by a fixed constant factor.

A binary tree with  the BST property is called a binary search tree.  It can serve as an efficient way for storage/retrieval of data.  We are lead to the following question: how to create and maintain a binary search tree?  

2. binary search tree insertion

Suppose we start with an empty binary tree T and  a  Comparator that models a total ordering in a given set of objects S.  Then T clearly has the BST property with respect the Comparator ordering of S.  The following algorithm (visitor on binary trees) will allow us to insert elements of S into T and at the same time maintain the BST property for T.  This algorithm also works for binary search tree containing Comparable objects.

Questions & Answers

Akash Reply
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
what is the actual application of fullerenes nowadays?
Abhijith Reply
is Bucky paper clear?
s. Reply
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
Himanshu Reply
good afternoon madam
what is system testing
what is the application of nanotechnology?
silver nanoparticles could handle the job?
not now but maybe in future only AgNP maybe any other nanomaterials
I'm interested in Nanotube
can nanotechnology change the direction of the face of the world
Prasenjit Reply
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?