0% found this document useful (0 votes)
20 views

slides_algo-ds-trees-basics-typed

Uploaded by

2hmed3mr3656
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

slides_algo-ds-trees-basics-typed

Uploaded by

2hmed3mr3656
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Structures

Binary Search
Tree Basics
Design and Analysis
of Algorithms I
Balanced Search Trees:
Supported Operations
Raison d’etre : like sorted array + fast (logarithmic) inserts + deletes !
OPERATIONS RUNNING TIME
SEARCH θ(log(n))
SELECT Also O(log(n)) Up from
supported O(1)
MIN/MAX by hash
O(log(n))
PRED/SUCC tables O(log(n))
RANK O(log(n))
OUTPUT IN SORTED ORDER O(n)
Also
INSERT supported O(log(n)) new
DELETE by heaps O(log(n))
Binary Search Tree Structure
-- exactly one node per key Root
-- most basic version :
each node has
-- left child pointer Leaves
-- right child pointer
-- parent pointer Toward the root

SEARCH TREE PROPERTY : All keys > x


( should hold at every node of the
search tree ) All keys < x

Tim Roughgarden
The Height of a BST Height = 2

Note : many possible trees for a set of


keys. (aka depth) longest
root-leaf path
Note : height could be anywhere from
to

Best case,
Worst case,
perfectly
a chain
balanced
Height = 4

Tim Roughgarden
Searching and Inserting
To Search for key k in tree T
-- start at the root
-- traverse left / right child pointers as needed
If k < key at If k > key at
current node current node

-- return node with key k or NULL, as appropriate


To Insert a new key k into a tree T Exercise
preserves
:

-- search for k (unsuccessfully) search tree


property!
-- rewire final NULL ptr to point to new node with key k
Tim Roughgarden
Min, Max, Pred, And Succ
To compute the minimum (maximum) key of a tree
- Start at root
- Follow left child pointers (right ptrs,
for maximum) untill you cant anymore
(return last key found)
To compute the predecessor of key k
- Easy case : If k’s left subtree nonempty, return max Exercise :
prove this
key in left subtree Happens first time you “turn left” works
- Otherwise : follow parent pointers until you get to a
key less than k.
Tim Roughgarden
In-Order Traversal
TO PRINT OUT KEYS IN INCREASING ORDER

-Let r = root of search tree, with subtrees TL and TR


- recurse on TL All smaller
[by recursion (induction) prints out keys of TL keys
in increasing order ] RUNNING TIME All smaller
keys
-Print out r’s key O(1) time, n recursive
calls => O(n) total
-Recurse on TR
[prints out keys of TR in increasing order]

Tim Roughgarden
Deletion
TO DELETE A KEY K FROM A SEARCH TREE

- SEARCH for k

EASY CASE (k’s node has no children)

-Just delete k’s node from tree, done

MEDIUM CASE (k’s node has one child)


( unique child assumes position
previously held by k’s node )
Tim Roughgarden
Deletion (con’d)
DIFFICULT CASE (k’s node has 2 children)

-Compute k’s predecessor l


[ i.e., traverse k’s (non-NULL) left child ptr, then
right child ptrs until no longer possible ]
- SWAP k and l ! RUNNING
NOTE : in it’s new position, k has no right child ! TIME :
=> easy to delete or splice out k’s new node θ(height)

Exercise : at end, have a valid search tree !


Tim Roughgarden
Select and Rank
Idea : store a little bit of extra info at each tree node
about the tree itself (i.e., not about the data)

Example Augmentation : size(x) = # of tree nodes in


subtree rooted at x.
Note : if x has children y and z,
then size(y) + size(z) + 1
Population in Right subtree x itself
left subtree

Also : easy to keep sizes up-to-date during an Insertion or


Deletion (you check!) Tim Roughgarden
Select and Rank (con’d)
HOW TO SELECT Ith ORDER STATISTIC FROM
AUGMENTED SEARCH TREE (with subtree sizes)
- start at root x, with children y and z
- let a = size(y) [a = 0 if x has no left child ]
- if a = i-1, return x’s key
- if a >= I, recursively compute ith order statistic of Smaller Larger
search tree rooted at y keys keys

- if a < i-1 recursively compute (i-a-1)th order statistic


of search tree rooted at z
RUNNING TIME = θ(height). [ EXERCISE : how to implement RANK ?
Tim Roughgarden

You might also like