Sunteți pe pagina 1din 28

Binary

Binary Search
Search Trees
Trees

Comp 122, Spring 2004


Binary Trees
 Recursive definition
1. An empty tree is a binary tree
2. A node with two child subtrees is a binary tree
3. Only what you get from 1 by a finite number of
applications of 2 is a binary tree.
56

26 200

Is this a binary tree? 28 190


18 213

12 24 27

Comp 122, Spring


rees - 2
2004
Binary Search Trees
 View today as data structures that can support
dynamic set operations.
» Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
 Can be used to build
» Dictionaries.
» Priority Queues.
 Basic operations take time proportional to the
height of the tree – O(h).

Comp 122, Spring


rees - 3
2004
BST – Representation
 Represented by a linked data structure of nodes.
 root(T) points to the root of tree T.
 Each node contains fields:
» key
» left – pointer to left child: root of left subtree.
» right – pointer to right child : root of right subtree.
» p – pointer to parent. p[root[T]] = NIL (optional).

Comp 122, Spring


rees - 4
2004
Binary Search Tree Property
 Stored keys must satisfy
the binary search tree
property. 56
  y in left subtree of x,
then key[y]  key[x]. 26 200
  y in right subtree of x,
then key[y]  key[x].
18 28 190 213

12 24 27

Comp 122, Spring


rees - 5
2004
Inorder Traversal
The binary-search-tree property allows the keys of a binary search
tree to be printed, in (monotonically increasing) order,
recursively.
Inorder-Tree-Walk
Inorder-Tree-Walk (x) (x)
1. ifif xx  NIL
1. NIL 56

2.
2. then then Inorder-Tree-Walk(left[p])
Inorder-Tree-Walk(left[p])
26 200

3.
3. print
print key[x]
key[x] 18 28 190 213

4.
4. Inorder-Tree-Walk(right[p])
Inorder-Tree-Walk(right[p]) 12 24 27

 How long does the walk take?


 Can you prove its correctness?
Comp 122, Spring
rees - 6
2004
Correctness of Inorder-Walk
 Must prove that it prints all elements, in order,
and that it terminates.
 By induction on size of tree. Size=0: Easy.
 Size >1:
» Prints left subtree in order by induction.
» Prints root, which comes after all elements in left
subtree (still in order).
» Prints right subtree in order (all elements come after
root, so still in order).

Comp 122, Spring


rees - 7
2004
Querying a Binary Search Tree
 All dynamic-set search operations can be supported in
O(h) time.
 h = (lg n) for a balanced binary tree (and for an
average tree built by adding nodes in random order.)
 h = (n) for an unbalanced tree that resembles a linear
chain of n nodes in the worst case.

Comp 122, Spring


rees - 8
2004
Tree Search
Tree-Search(x,
Tree-Search(x, k) k)
1.
1. ifif xx == NIL
NIL oror kk == key[x]
key[x]
2.
2. then then return
return xx
3.
3. ifif kk << key[x]
key[x]
4.
4. then then return
return Tree-Search(left[x],
Tree-Search(left[x], k)
k) 56
5.
5. else else return
return Tree-Search(right[x],
Tree-Search(right[x], k)k) 26 200

18 28 190 213
Running time: O(h)

Aside: tail-recursion 12 24 27

Comp 122, Spring


rees - 9
2004
Iterative Tree Search
Iterative-Tree-Search(x,
Iterative-Tree-Search(x, k) k) 56
1. while xx  NIL
1. while and kk  key[x]
NIL and key[x] 26 200
2.
2. do do ifif kk << key[x]
key[x]
3.
3. then xx 
then  left[x]
left[x] 18 28 190 213

4.
4. else xx 
else  right[x]
right[x]
5.
5. return
return xx
12 24 27

The iterative tree search is more efficient on most computers.


The recursive tree search is more straightforward.

Comp 122, Spring


rees - 10
2004
Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.

Tree-Minimum(x)
Tree-Minimum(x) Tree-Maximum(x)
Tree-Maximum(x)
1.
1. while left[x]  NIL
while left[x] NIL 1.
1. while right[x]  NIL
while right[x] NIL
2.
2. dodo xx  left[x]
left[x] 2.
2. do xx 
do  right[x]
right[x]
3.
3. return
return xx 3.
3. return
return xx

Q: How long do they take?

Comp 122, Spring


rees - 11
2004
Predecessor and Successor
 Successor of node x is the node y such that key[y] is the
smallest key greater than key[x].
 The successor of the largest key is NIL.
 Search consists of two cases.
» If node x has a non-empty right subtree, then x’s successor is
the minimum in the right subtree of x.
» If node x has an empty right subtree, then:
• As long as we move to the left up the tree (move up through right
children), we are visiting smaller keys.
• x’s successor y is the node that x is the predecessor of (x is the maximum
in y’s left subtree).
• In other words, x’s successor y, is the lowest ancestor of x whose left
child is also an ancestor of x.
Comp 122, Spring
rees - 12
2004
Pseudo-code for Successor
Tree-Successor(x)
Tree-Successor(x)
right[x]NIL
 ififright[x] NIL
2.2. then
thenreturn
returnTree-Minimum(right[x])
Tree-Minimum(right[x])
3.3. yyp[x]
p[x]
whileyyNIL
4.4. while NILand
andxx==right[y]
right[y]
doxx
5.5. do yy 56
6.6. yyp[y]
p[y]
26 200
7.7. return
returnyy
18 28 190 213
Code for predecessor is symmetric.

Running time: O(h) 12 24 27

Comp 122, Spring


rees - 13
2004
BST Insertion – Pseudocode
 Change the dynamic set Tree-Insert(T,
Tree-Insert(T,z)z)
represented by a BST. 1. yy
1. NILNIL
 Ensure the binary- 2. xx
2. root[T]
root[T]
search-tree property 3. whilexxNIL
3. while NIL
holds after change. 4.
4. doyy
do xx
 Insertion is easier than 5.
5. ififkey[z]
key[z]<<key[x]
key[x]
deletion. 6.
6. thenxx
then left[x]
left[x]
56 7.
7. elsexx
else right[x]
right[x]
26 200
8. p[z]
8. p[z] yy
9.
9. ififyy==NIL
NIL
28 190 213
10.
10. then root[t]
thenroot[t] zz
18
11.
11. elseelseififkey[z]
key[z]<<key[y]
key[y]
12.
12. then left[y]
then left[y] zz
12 24 27 13.
13. else right[y]
elseright[y] zz
Comp 122, Spring
rees - 14
2004
Analysis of Insertion
 Initialization: O(1) Tree-Insert(T,
Tree-Insert(T,z)z)
1. yy
1. NILNIL
 While loop in lines 3-7 2. xx
2. root[T]
root[T]
searches for place to 3. whilexxNIL
3. while NIL
insert z, maintaining 4.
4. doyy
do xx
parent y. 5.
5. ififkey[z]
key[z]<<key[x]
key[x]
This takes O(h) time. 6.
6. thenxx
then left[x]
left[x]
7.
7. elsexx
else right[x]
right[x]
 Lines 8-13 insert the 8. p[z]
8. p[z] yy
value: O(1) 9.
9. ififyy==NIL
NIL
10.
10. then root[t]
thenroot[t] zz
 TOTAL: O(h) time to 11.
11. elseelseififkey[z]
key[z]<<key[y]
key[y]
insert a node. 12.
12. then left[y]
then left[y] zz
13.
13. else right[y]
elseright[y] zz
Comp 122, Spring
rees - 15
2004
Exercise: Sorting Using BSTs
Sort (A)
for i  1 to n
do tree-insert(A[i])
inorder-tree-walk(root)

» What are the worst case and best case running


times?
» In practice, how would this compare to other
sorting algorithms?

Comp 122, Spring


rees - 16
2004
Tree-Delete (T, x)
if x has no children  case 0
then remove x
if x has one child  case 1
then make p[x] point to child
if x has two children (subtrees)  case 2
then swap x with its successor
perform case 0 or case 1 to delete it

 TOTAL: O(h) time to delete a node


Comp 122, Spring
rees - 17
2004
Deletion – Pseudocode
Tree-Delete(T,
Tree-Delete(T,z)z)
/*
/*Determine
Determinewhichwhichnode
nodeto tosplice
spliceout:
out:either
eitherzzor
orz’s
z’ssuccessor.
successor.*/*/
 ififleft[z]
left[z]==NIL
NILororright[z]
right[z]==NIL NIL
 thenyy
then zz
 elseyy
else Tree-Successor[z]
Tree-Successor[z]
/*
/*Set
Setxxto toaanon-NIL
non-NILchild
childofofx,x,or
orto
toNIL
NILififyyhas
hasno
nochildren.
children.*/
*/
4. left[y]NIL
4. ifif left[y] NIL
5.
5. thenxx
then left[y]
left[y]
6.
6. elsexx
else right[y]
right[y]
/*
/*yyisisremoved
removedfrom
fromthe
thetree
treeby
bymanipulating
manipulatingpointers
pointersof
of p[y]
p[y]
and
andxx*/*/
7. ififxxNIL
7. NIL
8.
8. then p[x]
thenp[x] p[y]
p[y]
/*
/*Continued
Continuedon onnext
nextslide
slide*/*/
Comp 122, Spring
rees - 18
2004
Deletion – Pseudocode
Tree-Delete(T,
Tree-Delete(T,z)z)(Contd.
(Contd.from
fromprevious
previousslide)
slide)
9.
9. ififp[y]
p[y]==NILNIL
10.
10. then root[T]
thenroot[T] xx
11.
11. elseifif yy
else left[p[i]]
left[p[i]]
12.
12. then left[p[y]]
thenleft[p[y]] xx
13.
13. else right[p[y]]
elseright[p[y]] xx
/*
/*IfIfz’s
z’ssuccessor
successorwas wasspliced
splicedout,
out,copy
copyits
itsdata
datainto
intozz*/
*/
14. ififyyzz
14.
15.
15. then key[z]
then key[z] key[y]
key[y]
16.
16. copy
copyy’s
y’ssatellite
satellitedata
datainto
intoz.z.
17.
17. return
returnyy

Comp 122, Spring


rees - 19
2004
Correctness of Tree-Delete
 How do we know case 2 should go to case 0 or case
1 instead of back to case 2?
» Because when x has 2 children, its successor is the
minimum in its right subtree, and that successor
has no left child (hence 0 or 1 child).
 Equivalently, we could swap with predecessor
instead of successor. It might be good to alternate to
avoid creating lopsided tree.

Comp 122, Spring


rees - 20
2004
Binary Search Trees
 View today as data structures that can support
dynamic set operations.
» Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
 Can be used to build
» Dictionaries.
» Priority Queues.
 Basic operations take time proportional to the
height of the tree – O(h).

Comp 122, Spring


rees - 21
2004
Red-black trees: Overview
 Red-black trees are a variation of binary search
trees to ensure that the tree is balanced.
» Height is O(lg n), where n is the number of nodes.
 Operations take O(lg n) time in the worst case.

Comp 122, Spring


rees - 22
2004
Red-black Tree
 Binary search tree + 1 bit per node: the attribute
color, which is either red or black.
 All other attributes of BSTs are inherited:
» key, left, right, and p.

 All empty trees (leaves) are colored black.


» We use a single sentinel, nil, for all the leaves of
red-black tree T, with color[nil] = black.
» The root’s parent is also nil[T ].

Comp 122, Spring


rees - 23
2004
Red-black Tree – Example
26

17 41

30 47

38 50

nil[T]
Comp 122, Spring
rees - 24
2004
Red-black Properties
1. Every node is either red or black.
2. The root is black.
3. Every leaf (nil) is black.
4. If a node is red, then both its children are
black.

5. For each node, all paths from the node to


descendant leaves contain the same number of
black nodes.

Comp 122, Spring


rees - 25
2004
Height of a Red-black Tree
 Height of a node:
» Number of edges in a longest path to a leaf.
 Black-height of a node x, bh(x):
» bh(x) is the number of black nodes (including nil[T ])
on the path from x to leaf, not counting x.
 Black-height of a red-black tree is the black-height
of its root.
» By Property 5, black height is well defined.

Comp 122, Spring


rees - 26
2004
Height of a Red-black Tree
h=4
 Example: 26 bh=2

 Height of a node: h=3


17 h=1 41 bh=2
» Number of edges in a bh=1
longest path to a leaf.
h=2
 Black-height of a node h=2 30
47 bh=1
bh=1
bh(x) is the number of h=1
bh=1
black nodes on path from 38 h=1 50
x to leaf, not counting x. bh=1

nil[T]
Comp 122, Spring
rees - 27
2004
Hysteresis : or the value of lazyness
 Hysteresis, n. [fr. Gr. to be behind, to lag.]
a retardation of an effect when the forces acting
upon a body are changed (as if from viscosity or
internal friction); especially: a lagging in the
values of resulting magnetization in a magnetic
material (as iron) due to a changing magnetizing
force

Comp 122, Spring


rees - 28
2004

S-ar putea să vă placă și