Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
26 200
18 28 190 213
12 24 27
Dr Muhammad Hussain Lecture - Binary Search Tree ADT
Data Structures – CSC212 (2)
26 200
18 28 190 213
12 24 27
Dr Muhammad Hussain Lecture - Binary Search Tree ADT
Data Structures – CSC212 (3)
Note:
The property 2 is called binary
binary--search-
search-tree property
This property guarantees that:
- The minimum is located at the left
left--most node.
- The maximum is located at the right
right--most node.
56
26 200
right-
right
i ht-mostt
18 28 190 213
left--most
left
12 24 27
Whyy BST?
It speeds up search operation on a collection of data elements
Operations:
Operation Specification
void empty
empty()
() Precondition: none
none..
Process: returns true if the BST has no nodes.
Operation Specification
void findkey (int k) Precondition: none.
Process: searches the BST for a node whose key = k. If found then that
node
ode will
w be set se as thee current
cu e node
ode and
a d returns
e u s true.
ue. Andd if search
sea c failed,
a ed,
then (1
(1) BST empty then just return false; or ((2
2) BST not empty then
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.
void insert
insert((int k, Type val)
val) Precondition: BST not full.
Process: (1) 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
(2) insert a new node with the given key and data and setting it as current
node, return true.
void update
update((Type) Precondition/Requires : BST is not empty.
Process: update the value of data of the current node,
node key remains
unchanged.
Type retrieve
retrieve()
() Precondition: BST is not empty.
empty.
Process: returns data of the current node..
node..
bool remove_key
remove_key((int k) Precondition: none
none..
Process: removes the node containing the key = k, and in case BST is
not empty
p y then sets the root as the current node. Returns true or false
depending on whether the operation was successful or not.
Represent
p ion of Binary
y Search Tree ADT
Since BST is a special kind binary tree, it can be represented using
- Linked List
Implementation of BST
public class BSTNode <T> {
public int key;
public T data;
public BSTNode<T> left,
p , right;
g ;
Implementation of BST
public class Flag {
boolean value;
/** Creates a new instance of Flag */
public Flag()
p g() {
value = false;
}
public Flag(boolean
p g v){
value = v;
}
public boolean get_value (){
return value;
}
public void set_value(boolean v){
value = v;
}
}
Implementation of BST
public class BST <T> {
BSTNode<T> root, current;
public BST()
p
public boolean empty()
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 (BSTNode<T> p) {
LinkStack<BSTNode<T>> stack = new LinkStack<BSTNode<T>>();
BSTNode<T> q = root;
;
while (q.right != p && q.left != p){
if (q.right != null) stack.push(q.right);
if (q.left
q != null)
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){
p = p.left;
}
return p;
}
Dr Muhammad Hussain Lecture - Binary Search Tree ADT
Data Structures – CSC212 (11)
Implementation of Operations
private BSTNode<T> remove_aux(int key, BSTNode<T> p, Flag flag) {
BSTNode<T> q, child = null;
if (p == null)
return null;
ll
if (key < p.key)
p.left = remove_aux(key, p.left, flag); Start with node p and
else if (key > p.key) remove the node whose
p right = remove_aux(key,
p.right remove aux(key p.right,
p right flag);
else {
k is
key i ‘key’
‘k ’ recursively.
i l
flag.set_value(true);
if (p.left != null && p.right != null){
q = find_min(p.right);
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;
p right;
return child;
}
}
return p
p;
}
Implementation of Operations
public BST()
{
root = current = null;
}
public T retrieve ()
{
return current.data
current.data;;
}
Implementation of Operations
public boolean findkey
findkey(int tkey){
BSTNode<T> p,q;
p = root;
; q = root;
;
if (empty()) return false;
while (p != null){
q = p
p;
if (p.key == tkey){
current = p;
return true;
}
else if (tkey < p.key)
p = p.left;
else
p = p.right;
}
current
t = q;
return false;
}
Implementation of Operations
public boolean insert (int k, T val){
BSTNode<T> p, q = current;
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;
}
else {
/* current is pointing to the parent of the new key.
if (k < current.key)
k )
current.left = p;
else
current.right
cu e t. g t = p;
current = p;
return true;
}
}
Implementation of Operations
public boolean remove_key (int tkey){
Flag removed = new Flag(false);
BSTNode<T> p;
p = remove_aux(tkey, root, removed);
current = root = p;
return removed.get_value();
removed get value();
}