Sei sulla pagina 1di 15

Data Structures – CSC212 (1)

Binary Search Tree


ADT
56

26 200

18 28 190 213

12 24 27
Dr Muhammad Hussain Lecture - Binary Search Tree ADT
Data Structures – CSC212 (2)

Binaryy Search Tree ADT ((BST))


It is a binary tree with the following properties
1. with each node associate a key
2. the key of each node is greater than the keys of the nodes
in its left subtree and it is less that the keys of the nodes in
its right subtree
56

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

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (4)

Whyy BST?
™ It speeds up search operation on a collection of data elements

Specification of Binary Search Tree ADT


Elements: Any valid data type
Structure: A binary tree such that if N is any node in the tree then all
nodes in its left subtree are smaller than N, and all nodes in its
right subtree are larger than N.
N t : A node
Note:
Note d N iis llarger th
than th
the node
d M if key
k valuel off N is
i
larger than that of M and vice versa.
versa.
Domain: Number of elements is bounded

Operations:

Operation Specification
void empty
empty()
() Precondition: none
none..
Process: returns true if the BST has no nodes.

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (5)

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.

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (6)

Represent
p ion of Binary
y Search Tree ADT
Since BST is a special kind binary tree, it can be represented using
- Linked List

Note : Array is not suitable for BST

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (7)

Implementation of BST
public class BSTNode <T> {
public int key;
public T data;
public BSTNode<T> left,
p , right;
g ;

public BSTNode(int k, T val) {


key
y = k;
data = val;
left = right = null;
}
public BSTNode (int k, BSTNode<T> l, BSTNode<T> r) {
key = k;
left = l;
right = r;
}
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (8)

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;
}
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (9)

Implementation of BST
public class 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
p ( key,
y, BSTNode<T> p, Flag
g
flag)

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)
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (10)

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;
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (12)

Implementation of Operations
public BST()
{
root = current = null;
}

public boolean empty()


{
return root == null ? true: false;
}

public T retrieve ()
{
return current.data
current.data;;
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (13)

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;
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (14)

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;
}
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT


Data Structures – CSC212 (15)

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();
}

Dr Muhammad Hussain Lecture - Binary Search Tree ADT

Potrebbero piacerti anche