Download Binary Search Tree ADT (BST)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
(1)
Data Structures – CSC212
Binary Search Tree
ADT
56
26
28
18
12
Dr Muhammad Hussain
200
24
190
213
27
Lecture - Binary Search Tree ADT
(2)
Data Structures – CSC212
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
28
18
12
Dr Muhammad Hussain
200
24
190
213
27
Lecture - Binary Search Tree ADT
(3)
Data Structures – CSC212
Note:
™ The property 2 is called binary
binary--searchsearch-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
rightright
i ht-mostt
28
18
left--most
left
Dr Muhammad Hussain
12
24
190
213
27
Lecture - Binary Search Tree ADT
(4)
Data Structures – CSC212
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 value
l 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
(5)
Data Structures – CSC212
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
(6)
Data Structures – CSC212
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
(7)
Data Structures – CSC212
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
(8)
Data Structures – CSC212
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
(9)
Data Structures – CSC212
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
public
public
public
public
public
boolean empty()
p y
T retrieve ()
boolean findkey(int tkey)
boolean insert (int k, T val)
boolean remove_key (int tkey)
boolean update(int key, T data)
}
Dr Muhammad Hussain
Lecture - Binary Search Tree ADT
(10)
Data Structures – CSC212
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
(11)
Data Structures – CSC212
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)
Start with node p and
p.left = remove_aux(key, p.left, flag);
else if (key > p.key)
remove the node whose
p right = remove_aux(key,
p.right
remove aux(key p.right,
p right flag);
k is
key
i ‘key’
‘k ’ recursively.
i l
else {
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
(12)
Data Structures – CSC212
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
(13)
Data Structures – CSC212
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
(14)
Data Structures – CSC212
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
(15)
Data Structures – CSC212
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
Related documents