5. binary search trees
TRANSCRIPT
-
7/31/2019 5. Binary Search Trees
1/64
Prof. Amr Goneid, AUC 1
CSCE 210Data Structures and Algorithms
Prof. Amr Goneid
AUCPart 5. Dictionaries(2):
Binary Search Trees
-
7/31/2019 5. Binary Search Trees
2/64
Prof. Amr Goneid, AUC 2
Dictionaries(2): Binary SearchTrees
The Dictionary Data Structure
The Binary Search Tree (BST)
Search, Insertion and Traversal of BST
Removal of nodes from a BST
Binary Search Tree ADT
Template Class Specification
Other Search Trees (AVL Trees)
-
7/31/2019 5. Binary Search Trees
3/64
Prof. Amr Goneid, AUC 3
1.The Dictionary Data Structure
Simple containers such as tables, stacks and queuespermit access of elements by position or order of
insertion. A Dictionaryis a form of container that permits
access by content.
Should support the following main operations:
Insert (D,x): Insert item x in dictionary D Delete (D,x): Delete item x from D
Search (D,k): search for key k in D
-
7/31/2019 5. Binary Search Trees
4/64
Prof. Amr Goneid, AUC 4
The Dictionary Data Structure
Examples: Unsorted arrays and Linked Lists:permit linear
search Sorted arrays:permit Binary search Ordered Lists:permit linear search Binary Search Trees (BST): fast support of all
dictionary operations.
Hash Tables: Fast retrieval by hashing key to aposition.
-
7/31/2019 5. Binary Search Trees
5/64
Prof. Amr Goneid, AUC 5
The Dictionary Data Structure
There are 3 types of dictionaries:
Static Dictionaries These are built once and
never change. Thus they need to support search, butnot insertion or deletion. These are betterimplemented using arrays or Hash tables with linearprobing.
Semi-dynamic Dictionaries These structuressupport insertion and search queries, but notdeletion. These can be implemented as arrays, linkedlists or Hash tables with linear probing.
-
7/31/2019 5. Binary Search Trees
6/64
Prof. Amr Goneid, AUC 6
The Dictionary Data Structure
Fully Dynamic Dictionaries These needfast support of all dictionary operations.
Binary Search Trees are best. Hash tablesare also great for fully dynamic dictionaries aswell, provided we use chaining as thecollision resolution mechanism.
-
7/31/2019 5. Binary Search Trees
7/64
Prof. Amr Goneid, AUC 7
The Dictionary Data Structure
In the revision part R3, we present twodictionary data structures that support allbasic operations. Both are linear structuresand so employ linear search, i.e O(n). Theyare suitable for small to medium sized data.
The first uses a run-time arrayto implementan ordered list and is suitable if we know the
maximum data size The second uses a linked listand is suitable
if we do not know the size of data to insert.
-
7/31/2019 5. Binary Search Trees
8/64
Prof. Amr Goneid, AUC 8
The Dictionary Data Structure
In the following, we present the design and
implement a dictionary data structures that isbased on the Binary Search Tree (BST). This will be a Fully Dynamic Dictionary
and basic operations are usually O(log n)
-
7/31/2019 5. Binary Search Trees
9/64
Prof. Amr Goneid, AUC 9
2. The Binary Search Tree (BST)
A Binary Search Tree (BST) is a Dictionaryimplemented as a Binary Tree. It is a form of
container that permits access by content. It supports the following main operations:
Insert : Insert item in BST
Remove : Delete item from BST Search : search for key in BST
-
7/31/2019 5. Binary Search Trees
10/64
Prof. Amr Goneid, AUC 10
BST
A BST is a binary tree that stores keys or key-data pairs inits nodes and has the following properties:
A key identifies uniquely the node (no duplicate keys) If (u , v , w) are nodes such that (u) is any node in the left
subtree of (v) and (w) is any node in the right subtree of(v) then:
key(u) < key(v) < key(w)
v
uw
-
7/31/2019 5. Binary Search Trees
11/64
Prof. Amr Goneid, AUC 11
Examples Of BST
-
7/31/2019 5. Binary Search Trees
12/64
Prof. Amr Goneid, AUC 12
Examples Of BST
These are NOT BSTs.
-
7/31/2019 5. Binary Search Trees
13/64
Prof. Amr Goneid, AUC 13
3. Search, Insertion & Traversal of BST
Search (tree, target)
if (tree is empty)
target is not in the tree;else if (the target key is the root)
target found in root;
else if(target key smaller than the roots key)
search left sub-tree;
else search right sub-tree;
-
7/31/2019 5. Binary Search Trees
14/64
Prof. Amr Goneid, AUC 14
Searching Algorithm
(Pseudo Code)Searches for the item with same key as kin the tree (t).Bool search(t,k){
if (t is empty)return false;else if (k == key(t))return true;
else if (k < key(t))
return search(tleft, k);
else
return search(tright, k);}
-
7/31/2019 5. Binary Search Trees
15/64
Prof. Amr Goneid, AUC 15
Searching for a key
Search for the node containing e:
Maximum number of comparisons is tree height, i.e. O(h)
-
7/31/2019 5. Binary Search Trees
16/64
Prof. Amr Goneid, AUC 16
Demo
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BST.html
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BST.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BST.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BST.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BST.html -
7/31/2019 5. Binary Search Trees
17/64
Prof. Amr Goneid, AUC 17
Building a Binary Search Tree
Tree created from root downward
Item 1 stored in root
Next item is attached to left tree if valueis smaller or right tree if value is larger
To insert an item into an existing tree,we must first locate the items parent
and then insert
-
7/31/2019 5. Binary Search Trees
18/64
Prof. Amr Goneid, AUC 18
Algorithm for Insertion
Insert (tree, new_item)
if(tree is empty)
insert new item as root;else if (root key matches item)
skip insertion; (duplicate key)
else if(new key is smaller than root)insert in left sub-tree;
else insert in right sub-tree;
-
7/31/2019 5. Binary Search Trees
19/64
Prof. Amr Goneid, AUC 19
Insertion (Pseudo Code)Inserts key (k)in the tree (t)
Bool insert(t, k)
{ if (t is empty)
{
create node containing (k)and attach to (t);return true;
}
else if (k == key(t)) return false;
else if (k < key(t)) return insert(tleft
, k);
else return insert(tright, k);
}
-
7/31/2019 5. Binary Search Trees
20/64
Prof. Amr Goneid, AUC 20
Example: Building a Tree
Insert: 40,20,10,50,65,45,30
-
7/31/2019 5. Binary Search Trees
21/64
Prof. Amr Goneid, AUC 21
Effect of Insertion Order
The shape of the tree depends on the orderof insertion. Shape determines the height (h)
of the tree. Since cost of search is O(h), the insertion
order will affect the search cost.
The previous tree is full, and h = log2(n+1)so
that search cost is O(log2n)
-
7/31/2019 5. Binary Search Trees
22/64
Prof. Amr Goneid, AUC 22
Effect of Insertion Order
The previous tree would look like a linked list if wehave inserted in the order 10,20,30,. Its heightwould be h = n and its search cost would be O(n)
O(n) O(log n)
-
7/31/2019 5. Binary Search Trees
23/64
Prof. Amr Goneid, AUC 23
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSTNew.html
Binary Search Tree Demo
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSTNew.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSTNew.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSTNew.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSTNew.html -
7/31/2019 5. Binary Search Trees
24/64
Prof. Amr Goneid, AUC 24
Linked Representation
The nodes in the BST will be implemented as alinked structure: left element right
16 45
32
40
32
16 45
40
t
-
7/31/2019 5. Binary Search Trees
25/64
Prof. Amr Goneid, AUC 25
Traversing a Binary Search Tree
Recursive inorder traversal of tree with root (t)
traverse ( t )
{if(t is not empty)
traverse (tleft);
visit (t);traverse (tright);
}
-
7/31/2019 5. Binary Search Trees
26/64
Prof. Amr Goneid, AUC 26
Find Minimum Key
Find the minimum key in a tree with root (t)
Minkey ( t )
{if(tleft is not empty) return MinKey(tleft);
else return key(t);
}
-
7/31/2019 5. Binary Search Trees
27/64
Prof. Amr Goneid, AUC 27
Other Traversal Orders
Pre-order (a.k.a. Depth-First traversal) can be implementedusing an iterative (non-recursive) algorithm. In this case, astack is used
If the stack is replaced by a queue and left pointers areexchanged by right pointers, the algorithm becomes Level-order traversal (a.k.a. Breadth-First traversal)
-
7/31/2019 5. Binary Search Trees
28/64
Prof. Amr Goneid, AUC 28
Iterative Preorder Traversal
void iterative_preorder ( )
{
t = root;
Let s be a stacks.push (t);
while(!s.stackIsEmpty())
{ s.pop(t); process(t->key);
if ( t right is not empty) s.push(t right);if ( t left is not empty) s.push(t left);
}
}
-
7/31/2019 5. Binary Search Trees
29/64
Prof. Amr Goneid, AUC 29
Pre-Order Traversal
Traversal order: {D,B,A,C,F,E,G}
D
B F
A C E G
1
2
34
5
6 7
-
7/31/2019 5. Binary Search Trees
30/64
Prof. Amr Goneid, AUC 30
Level Order Traversal
void levelorder ( )
{
t = root;
Let q be a queue;q.enqueue(t);
while(!q.queueIsEmpty())
{ q.dequeue(t); process(t->key);
if ( t left is not empty) q.enqueue(t left);if ( t right is not empty) q.enqueue(t right);
}
}
-
7/31/2019 5. Binary Search Trees
31/64
Prof. Amr Goneid, AUC 31
Level-Order Traversal
Traversal order: {D,B,F,A,C,E,G}
A C E G
B F
D1
2 3
45
6 7
-
7/31/2019 5. Binary Search Trees
32/64
Prof. Amr Goneid, AUC 32
4. Removal of Nodes from a BST
-
7/31/2019 5. Binary Search Trees
33/64
Prof. Amr Goneid, AUC 33
Deleting a ROOT Node
-
7/31/2019 5. Binary Search Trees
34/64
Prof. Amr Goneid, AUC 34
Deleting a ROOT Node
-
7/31/2019 5. Binary Search Trees
35/64
Prof. Amr Goneid, AUC 35
Deleting a ROOT Node (SpecialCase)
-
7/31/2019 5. Binary Search Trees
36/64
Prof. Amr Goneid, AUC 36
Deleting a ROOT Node (Alternative)
-
7/31/2019 5. Binary Search Trees
37/64
Prof. Amr Goneid, AUC 37
Deleting an Internal Node
-
7/31/2019 5. Binary Search Trees
38/64
Prof. Amr Goneid, AUC 38
Search for Parent of a Node
To delete a node, we need to find its parent.To search for the parent (p) of a node (x) with key (k)in tree (t):Set x = t; p = null; found = false;
While (not found) and (x is not empty)
{if k < key(x) descend left (i.e. set p = x; x = xleft)elseif k > key(x) descend right (i.e. set p = x;x = xright)else found = true
}
Notice that:P is null if (k) is in the root or if the tree is empty.If (k) is not found, p points to what should have been
its parent.
-
7/31/2019 5. Binary Search Trees
39/64
Prof. Amr Goneid, AUC 39
Algorithm to remove a Node
Letk = key to remove its nodet = pointer to root of treex = location where k is found
p = parent of a node
sx = inorder successor of xs = child of x
-
7/31/2019 5. Binary Search Trees
40/64
Prof. Amr Goneid, AUC 40
Algorithm to remove a Node
Remove (t,k){
Search for (k) and its parent;If not found, return;else it is found at (x) with parent at (p):
Case (x) has two children:Find inorder successor (sx) and its parent (p);Copy contents of (sx) into (x);Change (x) to point to (sx);
Now (x) has one or no children and (p) is its parent
-
7/31/2019 5. Binary Search Trees
41/64
Prof. Amr Goneid, AUC 41
Algorithm to remove a Node
Case (x) has one or no children:Let (s) point to the child of (x) or null if there
are no children;If p = null then set root to null;else if (x) is a left child of (p), set pleft = s;
else set pright = s;
Now (x) is isolated and can be deleteddelete (x);
}
-
7/31/2019 5. Binary Search Trees
42/64
Prof. Amr Goneid, AUC 42
Example: Delete Root
40
20
10 30
60
50 70
xp = null
-
7/31/2019 5. Binary Search Trees
43/64
Prof. Amr Goneid, AUC 43
Example: Delete Root
40
20
10 30
60
50 70
x
p
sx
-
7/31/2019 5. Binary Search Trees
44/64
Prof. Amr Goneid, AUC 44
Example: Delete Root
50
20
10 30
60
50 70
p
x
S = null
-
7/31/2019 5. Binary Search Trees
45/64
Prof. Amr Goneid, AUC 45
Example: Delete Root
50
20
10 30
60
50
70
x
null
delete
-
7/31/2019 5. Binary Search Trees
46/64
Prof. Amr Goneid, AUC 46
5. Binary Search Tree ADT
Elements:
A BST consists of a collection of elements that are allof the same type. An element is composed of two
parts: key of and data of Structure:
A node in a BST has at most two subtrees. The keyvalue in a node is larger than all the keys in its leftsubtree and smaller than all keys in its right subtree.Duplicate keys are not allowed.
-
7/31/2019 5. Binary Search Trees
47/64
Prof. Amr Goneid, AUC 47
Binary Search Tree ADT
Data members root pointer to the tree root
Basic Operations
binaryTree a constructor insert inserts an item
empty checks if tree is empty search locates a node given a
key
-
7/31/2019 5. Binary Search Trees
48/64
Prof. Amr Goneid, AUC 48
Binary Search Tree ADT
Basic Operations (continued) retrieve retrieves data given key traverse traverses a tree
(In-Order) preorder pre-order traversal levelorder Level-order traversal
remove Delete node given key graph simple graphical output
-
7/31/2019 5. Binary Search Trees
49/64
Prof. Amr Goneid, AUC 49
Node Specification// The linked structure for a node can be// specified as a Class in the private part of// the main binary tree class.class treeNode // Hidden from user{
public:keyType key; // keydataType data; // DatatreeNode *left; // left subtreetreeNode *right; // right subtree
}; // end of class treeNode declaration
//A pointer to a node can be specified by a type:typedef treeNode * NodePointer;
NodePointer root;
-
7/31/2019 5. Binary Search Trees
50/64
Prof. Amr Goneid, AUC 50
6. Template Class Specification
Because node structure is private, all references to pointersare hidden from user
This means that recursive functions with pointerparameters must be private.
A public (User available) member function will have to callan auxiliary private function to support recursion.
For example, to traverse a tree, the user public function willbe declared as:
void traverse ( ) const;
and will be used in the form:BST.traverse ( );
-
7/31/2019 5. Binary Search Trees
51/64
Prof. Amr Goneid, AUC 51
Template Class Specification
Such function will have to call a private traverse function:
void traverse2 (NodePointer ) const;
Therefore, the implementation of traverse will be:
template void binaryTree::traverse() const{
traverse2(root);}
Notice that traverse2 can support recursion via its pointerparameter
-
7/31/2019 5. Binary Search Trees
52/64
Prof. Amr Goneid, AUC 52
Template Class Specification
For example, if we use in-order traversal, then the privatetraverse function will be implemented as:
template void binaryTree ::traverse2
(NodePointer aRoot) const{
if (aRoot != NULL){ // recursive in-order traversaltraverse2 (aRoot->left);
cout key right);
}} // end of private traverse
-
7/31/2019 5. Binary Search Trees
53/64
Prof. Amr Goneid, AUC 53
Template Class Specification
All similar functions will be implemented usingthe same method. For example:
Public Function Private Function
insert (key,data) insert2 (pointer,key,data)search(key) search2 (pointer,key)
retrieve(key,data) retrieve2 (pointer,key,data)
traverse( ) traverse2 (pointer)
-
7/31/2019 5. Binary Search Trees
54/64
Prof. Amr Goneid, AUC 54
BinaryTree.h
// FILE: BinaryTree.h
// DEFINITION OF TEMPLATE CLASS BINARY SEARCH
// TREE
#ifndef BIN_TREE_H
#define BIN_TREE_H
// Specification of the class
template
class binaryTree
{
-
7/31/2019 5. Binary Search Trees
55/64
Prof. Amr Goneid, AUC 55
BinaryTree.hpublic:
// Public Member functions ...
// CREATE AN EMPTY TREE
binaryTree();
// INSERT AN ELEMENT INTO THE TREE
bool insert(const keyType &,
const dataType &);
// CHECK IF THE TREE IS EMPTY
bool empty() const;
// SEARCH FOR AN ELEMENT IN THE TREE
bool search (const keyType &) const;// RETRIEVE DATA FOR A GIVEN KEY
bool retrieve (const keyType &, dataType &)const;
-
7/31/2019 5. Binary Search Trees
56/64
Prof. Amr Goneid, AUC 56
BinaryTree.h
// TRAVERSE A TREE
void traverse() const;
// Iterative Pre-order Traversal
void preorder () const;// Iterative Level-order Traversal
void levelorder () const;
// GRAPHIC OUTPUT
void graph() const;// REMOVE AN ELEMENT FROM THE TREEvoid remove (const keyType &);.........
-
7/31/2019 5. Binary Search Trees
57/64
Prof. Amr Goneid, AUC 57
BinaryTree.h
private:
// Node Class
class treeNode
{
public:keyType key; // key
dataType data; // Data
treeNode *left; // left subtree
treeNode *right; // right subtree
}; // end of class treeNode declarationtypedef treeNode * NodePointer;// Data member ....
NodePointer root;
-
7/31/2019 5. Binary Search Trees
58/64
Prof. Amr Goneid, AUC 58
BinaryTree.h// Private Member functions ...
// Searches a subtree for a key
bool search2 ( NodePointer , const keyType &)const;
//Searches a subtree for a key and retrieves databool retrieve2 (NodePointer , const keyType & ,
dataType &) const;
// Inserts an item in a subtree
bool insert2 (NodePointer &, const keyType &,
const dataType &);
-
7/31/2019 5. Binary Search Trees
59/64
Prof. Amr Goneid, AUC 59
BinaryTree.h
// Traverses a subtreevoid traverse2 (NodePointer ) const;// Graphic output of a subtreevoid graph2 ( int , NodePointer ) const;
// LOCATE A NODE CONTAINING ELEMENT AND ITS// PARENTvoid parentSearch( const keyType &k,
bool &found,NodePointer &locptr,
NodePointer &parent) const;};#endif // BIN_TREE_H#include binaryTree.cpp
-
7/31/2019 5. Binary Search Trees
60/64
Prof. Amr Goneid, AUC 60
Implementation Files
Full implementation of the BST
class is found at:
http://www.cse.aucegypt.edu/~csci210/codes.zip
http://www.cse.aucegypt.edu/~csci210/codes.ziphttp://www.cse.aucegypt.edu/~csci210/codes.ziphttp://www.cse.aucegypt.edu/~csci210/codes.ziphttp://www.cse.aucegypt.edu/~csci210/codes.ziphttp://www.cse.aucegypt.edu/~csci210/codes.ziphttp://www.cse.aucegypt.edu/~csci210/codes.ziphttp://www.cse.aucegypt.edu/~csci210/codes.zip -
7/31/2019 5. Binary Search Trees
61/64
Prof. Amr Goneid, AUC 61
7. Other Search Trees
Binary Search Trees have worst caseperformance of O(n), and best caseperformance of O(log n)
There are many other search trees that arebalanced trees.
Examples are: AVL Trees, Red-Black trees
-
7/31/2019 5. Binary Search Trees
62/64
Prof. Amr Goneid, AUC 62
AVL Trees
Named after its Russian inventors:
Adel'son-Vel'skii and Landis (1962)
An AVL tree is a binary search tree inwhich
the heights of the right subtree and left subtree ofthe root differ by at most 1
the left subtree and the right subtree arethemselves AVL trees
-
7/31/2019 5. Binary Search Trees
63/64
Prof. Amr Goneid, AUC 63
AVL Tree
Notice that:
N(h) = N(h-1) + N(h-2) + 1
h-1
h-2h
-
7/31/2019 5. Binary Search Trees
64/64
AVL Tree
treeAVLtheofheightcaseworsttheisThis441
2
51
5
11
:numbersFibonacciforformula
eapproximattheusecanweandseriesFibonacciaisThis
12111{
Also
3
)Nlog(.hOr
)h(N
})h(N{})h(N{})h(N
h