UMBC CS 201, Fall 98
A Binary Tree
A binary tree is a tree whose nodes contain two links, one or both of
them may be NULL. Since each node contains two links, and these links will
point to that node's children, we call one of them the left child and
the other one the right child.
The definition of a node for a binary tree might look like this:
typedef struct tag* nodePtr;
typedef struct tag
{
int data;
nodePtr leftChild;
nodePtr rightChild;
}node;
Many times there are data that you might like to store as a sorted list,
but want to be able to search quickly. We gave up being able to do a
search in lg(n) time, when we went from arrays to linked lists, because we
no longer knew where the middle of the list was (or the middle of the middle),
etc.
We can store data in what is known as a binary search tree, which will enable
us to search for an item stored in approximately lg(n) time. The maximum
amount of time it takes to find an item in a binary search tree is equivalent
to the height of the tree.
Inserting
A node can only be inserted as a leaf node in a binary search tree.
If the value of the node to be inserted is less than the value at the current
node, further attempts to be placed will be made using the left subtree.
If the value of the node to be inserted is greater than the value at the
current node, further attempts to be placed will be made using the right
subtree.
(Drawings will be done in class)
Properties
All children to the left of a node, contain values less than that node.
All children to the right of a node, contain values greater than that node.
The left-most leaf of a binary search tree is always the smallest
node in the binary search tree.
The right-most leaf of a binary search tree is always the largest
node in the binary search tree.
CSEE
|
201
|
201 F'98
|
lectures
|
news
|
help
Wednesday, 09-Dec-1998 09:05:33 EST