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

L BST

Uploaded by

atharva3010
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)
13 views

L BST

Uploaded by

atharva3010
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/ 30

Binary Search Trees

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?


18 28 190 213

12 24 27

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

btrees - 3
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).

btrees - 4
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

btrees - 5
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 (x)
1. if x  NIL 56

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

3. print key[x] 18 28 190 213

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

 How long does the walk take?


 Can you prove its correctness?
btrees - 6
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).

btrees - 7
Tree Traversals
There are mainly three ways to traverse a tree:
1) Inorder Traversal
2) Postorder Traversal
3) Preorder Traversal

btrees - 8
9

Inorder Traversal: A E H J M T Y
Visit second
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Visit left subtree first Visit right subtree last


btrees - 9
Inorder Traversal
 Visit the nodes in the left subtree, then visit
the root of the tree, then visit the nodes in
the right subtree
Inorder(tree)
If tree is not NULL
Inorder(Left(tree))
Visit Info(tree)
Inorder(Right(tree))

Warning: "visit" implies do something with the value at


the node (e.g., print, save, update etc.).

btrees - 10
11

Preorder Traversal: J E A H T M Y
Visit first
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Visit left subtree second Visit right subtree last


btrees - 11
Preorder Traversal
 Visit the root of the tree first, then visit the
nodes in the left subtree, then visit the nodes
in the right subtree
Preorder(tree)
If tree is not NULL
Visit Info(tree)
Preorder(Left(tree))
Preorder(Right(tree))

btrees - 12
13

Postorder Traversal: A H E M Y T J
Visit last
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Visit left subtree first Visit right subtree second


btrees - 13
Postorder Traversal
 Visit the nodes in the left subtree first, then
visit the nodes in the right subtree, then visit
the root of the tree
Postorder(tree)
If tree is not NULL
Postorder(Left(tree))
Postorder(Right(tree))
Visit Info(tree)

btrees - 14
Tree
Traversals:
another
example

btrees - 15
Function PrintTree
 We use "inorder" to print out the node values.
 Keys will be printed out in sorted order.
 Hint: binary search could be used for sorting!

ADJMQRT
btrees - 16
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.

btrees - 17
Tree Search
Tree-Search(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return Tree-Search(left[x], k)
56
5. else return Tree-Search(right[x], k)
26 200

18 28 190 213
Running time: O(h)

12 24 27

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

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

The iterative tree search is more efficient on most computers.


The recursive tree search is more straightforward.

btrees - 19
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-Maximum(x)
1. while left[x]  NIL 1. while right[x]  NIL
2. do x  left[x] 2. do x  right[x]
3. return x 3. return x

Q: How long do they take?

btrees - 20
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).

btrees - 21
Pseudo-code for Successor
Tree-Successor(x)
 if right[x]  NIL
2. then return Tree-Minimum(right[x])
3. y  p[x]
4. while y  NIL and x = right[y]
5. do x  y
56
6. y  p[y]
26 200
7. return y

18 28 190 213
Code for predecessor is symmetric.

Running time: O(h) 12 24 27

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

btrees - 25
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


btrees - 26
Deletion – Pseudocode
Tree-Delete(T, z)
/* Determine which node to splice out: either z or z’s successor. */
 if left[z] = NIL or right[z] = NIL
 then y  z
 else y  Tree-Successor[z]
/* Set x to a non-NIL child of y, or to NIL if y has no children. */
4. if left[y]  NIL
5. then x  left[y]
6. else x  right[y]
/* y is removed from the tree by manipulating pointers of p[y]
and x */
7. if x  NIL
8. then p[x]  p[y]
/* Continued on next slide */
btrees - 27
Deletion – Pseudocode
Tree-Delete(T, z) (Contd. from previous slide)
9. if p[y] = NIL
10. then root[T]  x
/* If z’s successor was spliced out, copy its data into z */
14. if y  z
15. then key[z]  key[y]
16. Tree-Delete(T, y);
17. return y

btrees - 28
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.

btrees - 29
btrees - 30 Comp 122, Spring 2004

You might also like