Binary Search Tree
Binary Search Tree
ADT
ADT
56
26
200
18
28
190
213
12
24
27
12
24
27
Binary Search Tree ADT (BST)
Binary Search Tree ADT (BST)
y
y
(
(
)
)
It is a binary
It is a binary tree
tree with the following properties
with the following properties
1
1. with each node associate a key
. with each node associate a key
1
1. with each node associate a key
. with each node associate a key
2
2. the key of each node is greater than the keys of the
. the key of each node is greater than the keys of the nodes
nodes
in its left
in its left subtree
subtree and it is less that the keys of
and it is less that the keys of the nodes in
the nodes in
its right
its right subtree
subtree
56
26
200
18
28
190
213
Note:
Note:
The property
The property 2
2 is called
is called
binary
binary--search
search--tree property
tree property
This property guarantees that:
This property guarantees that:
-- The
The
minimum
minimum
is located at the
is located at the
left
left--most
most
node.
node.
-- The
The
maximum
maximum
is located at the
is located at the
right
right--most
most
node.
node.
56
26
200
i ht
i ht
tt
18
28
190
213
right
right--most
most
12
24
27
left
left--most
most
24
27
Why BST?
Why BST?
y
y
It speeds up search operation on a collection of data
It speeds up search operation on a collection of data elements
elements
Specification of Binary Search Tree ADT
Specification of Binary Search Tree ADT
Elements:
Elements: Any
Any valid data
valid data type
type
Structure:
Structure: A binary tree such that if N is any node in the tree then all
A binary tree such that if N is any node in the tree then all
Structure:
Structure: A binary tree such that if N is any node in the tree then all
A binary tree such that if N is any node in the tree then all
nodes in its left
nodes in its left subtree
subtree are smaller than N, and all nodes in its
are smaller than N, and all nodes in its
right
right subtree
subtree are larger than N.
are larger than N.
N t
N t
A
A
d N i l
d N i l
th
th
th
th
d M if k
d M if k
l
l
f N i
f N i
Note
Note::
A node N is larger than the node M if key value of N is
A node N is larger than the node M if key value of N is
larger than that of M and vice versa
larger than that of M and vice versa
..
Domain:
Domain: Number of elements is
Number of elements is bounded
bounded
Operations:
Operations:
Operation
Operation SpecificationSpecification
void
void emptyempty()() Precondition: Precondition: nonenone..
Process:
Operation
Operation SpecificationSpecification Operation
Operation SpecificationSpecification
void
void findkeyfindkey ((intint k) k) Precondition: Precondition: none.none.
Process:
Process: searches the BST for a node whose key = k. If found then that searches the BST for a node whose key = k. If found then that node will be set as the current node and returns true. And if search failed, node will be set as the current node and returns true. And if search failed, ode wode w be se asbe se as e cu ee cu e ode a d e u s ue.ode a d e u s ue. dd sea csea c a ed,a ed, then (
then (11) BST empty then just return false; or () BST empty then just return false; or (22) BST not empty then ) BST not empty then current node is the node to which a node containing k would be attached current node is the node to which a node containing k would be attached as a child if it were added to BST and return false.
as a child if it were added to BST and return false. void
void insertinsert((intint k,k, Type Type valval)) Precondition: Precondition: BST not full.BST not full.
Process:
Process: ((11) ) if we already have a node with key = k then current retain if we already have a node with key = k then current retain its old value (the value prior to calling this operation) and return false; or its old value (the value prior to calling this operation) and return false; or ((22) insert a new node with the given key and data and setting it as current) insert a new node with the given key and data and setting it as current ((22) insert a new node with the given key and data and setting it as current ) insert a new node with the given key and data and setting it as current node, return true.
node, return true. void
void updateupdate((Type)Type) Precondition/Requires : Precondition/Requires : BST is not empty.BST is not empty.
Process:
Process: update the value of data of the current node key remainsupdate the value of data of the current node key remains
Process:
Process: update the value of data of the current node, key remains update the value of data of the current node, key remains unchanged.
unchanged. Type
Type retrieveretrieve()() Precondition: Precondition: BST is not emptyBST is not empty..
Process:
Process: returns data of the current node.returns data of the current node...
Process:
Process: returns data of the current node.returns data of the current node... bool
bool remove_keyremove_key((intint k)k) Precondition: Precondition: nonenone..
Process:
Process: removes the node containing the key = k, and in case BST is removes the node containing the key = k, and in case BST is not empty then sets the root as the current node. Returns true or false not empty then sets the root as the current node. Returns true or false p yp y depending on whether the operation was successful or not.
Represent ion of Binary Search Tree ADT
Represent ion of Binary Search Tree ADT
p
p
y
y
Since BST is a special
Since BST is a special kind binary
kind binary tree, it can be represented using
tree, it can be represented using
Linked List
Linked List
--
Linked List
Linked List
Note
Note ::
Array is not suitable for BST
Array is not suitable for BST
Note
Implementation of BST
public class
BSTNode
BSTNode
<T> {
Implementation of BST
public class
BSTNode
BSTNode
<T> {
public int key;
public T data;
public BSTNode<T> left, right;
p
,
g
;
public BSTNode(int k, T val) {
key = k;
y
data = val;
left = right = null;
}
public BSTNode (int k, BSTNode<T> l, BSTNode<T> r) {
key = k;
left = l;
right = r;
}
Implementation of BST
public class
Flag
Flag
{
Implementation of BST
public class
Flag
Flag
{
boolean value;
/** Creates a new instance of Flag */
public Flag() {
p
g() {
value = false;
}
public Flag(boolean v){
p
g
value = v;
}
public boolean get_value (){
return value;
}
public void set_value(boolean v){
value = v;
}
Implementation of BST
public class
BST
BST
<T> {
Implementation of BST
public class
BST
BST
<T> {
BSTNode<T> root, current;
public BST()
private BSTNode<T> findparent (BSTNode<T> p)
private BSTNode<T> find_min(BSTNode<T> p)
private BSTNode<T> remove aux(int key, BSTNode<T> p, Flag
p
_
(
y,
p,
g
flag)
public boolean empty()
p
p y
public T retrieve ()
public boolean findkey(int tkey)
public boolean insert (int k, T val)
public boolean remove_key (int tkey)
public boolean update(int key, T data)
Implementation of Operations
private BSTNode<T>
findparent
findparent
(BSTNode<T> p) {
LinkStack<BSTNode<T>> stack = new LinkStack<BSTNode<T>>();
BSTNode<T> q = root;
q
;
while (q.right != p && q.left != p){
if (q.right != null) stack.push(q.right);
if (q.left != null)
q
q = q.left;
else
q = stack.pop();
}
return q;
}
private BSTNode<T>
find_min
(BSTNode<T> p){
if (p == null) return null;
while (p.left != null){
while (p.left ! null){
p = p.left;
}
Implementation of Operations
private BSTNode<T> remove_aux(int key, BSTNode<T> p, Flag flag) {BSTNode<T> q, child = null; if (p == null)
ll return null; if (key < p.key)
p.left = remove_aux(key, p.left, flag); else if (key > p.key)
p right = remove aux(key p right flag);
Start with node
p
and
remove the node whose
k
i ‘
k
’
i
l
p.right = remove_aux(key, p.right, flag); else {
flag.set_value(true);
if (p.left != null && p.right != null){ q = find min(p.right);
key is ‘
key
’ recursively.
q _ p g
p.key = q.key; p.data = q.data;
p.right = remove_aux(q.key, p.right, flag); }
else {
if (p.right == null) child = p.left;
else if (p.left == null) child = p right; child = p.right; return child; } } return p;p }
Implementation of Operations
public BST()
public BST()
{
{
Implementation of Operations
{
{
root = current = null;
root = current = null;
}
}
public
public boolean
boolean empty()
empty()
{
{
return root == null ? true: false;
return root == null ? true: false;
public T retrieve ()
public T retrieve ()
return root == null ? true: false;
return root == null ? true: false;
}
}
public T retrieve ()
public T retrieve ()
{
{
return
return current.data
current.data;
;
}
}
}
}
Implementation of Operations
public boolean
findkey
findkey
(int tkey){
BSTNode<T> p,q;
p = root; q = root;
p
; q
;
if (empty()) return false;
while (p != null){
q = p;
q
p
if (p.key == tkey){
current = p;
return true;
}
else if (tkey < p.key)
p = p.left;
else
p = p.right;
}
t
current = q;
return false;
}
Implementation of Operations
public boolean
insert
insert
(int k, T val){ BSTNode<T> p, q = current;if (findkey(k)){ if (findkey(k)){
current = q;
return false; /* key already in the BST */ }
p = new BSTNode<T>(k, val); if (empty()) { root = current = p; return true; return true; } else {
/* current is pointing to the parent of the new key. if (k k ) if (k < current.key) current.left = p; else current.right = p; cu e t. g t p; current = p; return true; } } }
Implementation of Operations
public boolean
remove_key
remove_key
(int tkey){
Flag removed = new Flag(false);
BSTNode<T> p;
BSTNode<T> p;
p = remove_aux(tkey, root, removed);
current = root = p;
return removed get value();
return removed.get_value();
}