Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root...
Transcript of Binary Search Trees Search Trees.pdf · Building a Binary Search Tree 11 Tree created from root...
Data Structures and Algorithms
1
Binary Search Trees
أشجار البحث الثنائية
Binary Search Trees
2
The Binary Search Tree (BST)
Deleting Nodes from a BST
Binary Search Tree ADT
Other Search Trees
4. Binary Search Trees
3
BST
4. The Binary Search Tree (BST)
4
A Binary Search Tree (BST) is a Dictionary
implemented 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
BST
5
A BST is a binary tree that stores keys or key-data pairs in
its 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 subtreeof (v) and (w) is any node in the right subtree of (v) then:
key(u) < key(v) < key(w)
v
u w
Examples Of BST
6
Examples Of BST
7These are NOT BSTs.
Searching Algorithm
8
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 root’s key)
search left sub-tree
else
search right sub-tree
Searching Algorithm
(Pseudo Code)
9
Searches for the item with same key as k
in 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);
}
Searching for a key
10
Search for the node containing e:
Maximum number of comparisons is tree height, i.e. O(h)
Building a Binary Search Tree
11
Tree created from root downward
Item 1 stored in root
Next item is attached to left tree if value is smaller or
right tree if value is larger
To insert an item into an existing tree, we must first
locate the item’s parent and then insert
Algorithm for Insertion
12
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
Insertion (Pseudo Code)
13
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);
}
Example: Building a Tree
Insert: 40,20,10,50,65,45,30
14
Effect of Insertion Order
15
The shape of the tree depends on the
order of 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)
Effect of Insertion Order
16
The previous tree would look like a linked list if we have inserted in the order 10,20,30,…. Its height would be h = n and its search cost would be O(n)
O(n)O(log n)
Linked Representation
17
The nodes in the BST will be implemented as a linked
structure: left element right
16 45
32
40
32
16 45
40
t
Traversing a Binary Search Tree
18
Recursive inorder traversal of tree with root (t)
traverse ( t )
{
if (t is not empty)
traverse (tleft);
visit (t);
traverse (tright);
}
Find Minimum Key
19
Find the minimum key in a tree with root (t)
Minkey ( t )
{
if (tleft is not empty) return MinKey(tleft);
else return key(t);
}
Other Traversal Orders
20
Pre-order (a.k.a. Depth-First traversal) can be implemented
using an iterative (non-recursive) algorithm. In this case, a
stack is used
If the stack is replaced by a queue and left pointers are
exchanged by right pointers, the algorithm becomes Level-order
traversal (a.k.a. Breadth-First traversal)
باستخدام (a.k.a. Depth-First traversal))يمكن تنفيذ االجتياز قبل الترتيب
.في هذه الحالة ، يتم استخدام مكدس(. غير عودية)خوارزمية تكرارية
ينى ، تصبح إذا تم استبدال المكدس بطابور وتم تبديل المؤشرات اليسرى بال مؤشرات اليم
(k.a. Breadth-First traversal)الخوارزمية اجتياز المستوى
Iterative Preorder Traversal
االجتياز أثناء الترتيب التكراري
21
void iterative_preorder ( )
{
t = root;
Let s be a stack
s.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);
}
}
Pre-Order Traversal
22
Traversal order: {D,B,A,C,F,E,G}
D
B F
A C E G
1
2
34
5
6 7
Level Order Traversal
اجتياز مستوى الترتيب
23
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);
}
}
Level-Order Traversal
24
Traversal order: {D,B,F,A,C,E,G}
A C E G
B F
D
1
2 3
45
6 7
5. Deleting Nodes from a BST
25
Deleting a ROOT Node
26
Deleting a ROOT Node
27
Deleting a ROOT Node (Special Case)
28
Deleting a ROOT Node (Alternative)
29
Deleting an Internal Node
30
Search for Parent of a Node
31
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)
else
if 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.
Algorithm to remove a Node
32
Let
k = key to remove its node
t = pointer to root of tree
x = location where k is found
p = parent of a node
sx = inorder successor of x
s = child of x
Algorithm to remove a Node
33
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
Algorithm to remove a Node
34
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 deleted
delete (x);
}
Example: Delete Root
35
40
20
10 30
60
50 70
xp = null
Example: Delete Root
36
40
20
10 30
60
50 70
x
p
sx
Example: Delete Root
37
50
20
10 30
60
50 70
p
x
S = null
Example: Delete Root
38
50
20
10 30
60
50
70
x
null
delete
6. Self-Balancing Binary Search
Trees
39
Binary Search Trees have
worst case performance of O(n), and
best case performance of O(log n)
There are many other search trees that are
balanced trees.
Examples are: AVL Trees, Red-Black trees
AVL Trees
40
An AVL tree is a self balancing binary search tree in which
the heights of the right subtree and left subtree of the root differ by at
most 1
the left subtree and the right subtree are themselves AVL trees
rebalancing is done when insertion or deletion causes violation of the
AVL condition.
AVL Tree
41
Notice that:
N(h) = N(h-1) + N(h-2) + 1
h-1
h-2h
AVL Tree
42
tree AVLthe ofheight caseworst the is This
)(log)log(44.1
2
51
5
11)(
:numbers Fibonaccifor formula
eapproximat the use can we and series Fibonacci a is This
}1)2({}1)1({}1)({
Also
3
NONhOr
hN
hNhNhN
h