CS - PowerPoint PPT Presentation

About This Presentation
Title:

CS

Description:

1) empty is always a BST ;; 2) (make-node d lft rgt) is a BST if and only if: ... If we are searching an empty tree at this point, return false ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 71
Provided by: ccGa
Category:
Tags: empty

less

Transcript and Presenter's Notes

Title: CS


1
1321
  • CS

2
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Lecture 12
  • Oct 2, 2001
  • Fall Semester

3
Todays Menu
  • Last timetrees
  • Binary Search Trees
  • Defining
  • Searching
  • Inserting items
  • Deleting items

4
Last time
Last time we explored another example of a
Dynamic Data Types that we named a tree.
5
Treesthe terminology
Trees consist of nodes that contain data as well
as references/links/pointers to one or more
nodes. The first or top node in a tree is
called the root node. A parent node points
(references/links) to child nodes. Nodes that
have two empty children are leaf nodes.
6
Root
Leaf
Leaf
empty
empty
empty
empty
empty
7
Parent
Child
empty
empty
empty
empty
empty
8
Tree Recursion
As it turns out, using a mix of selector calls,
we could visit the data in a tree as we wish. We
can do this in most any order. There are three
common patterns of recursive calls youll have to
know pre-order, in-order, and post-order
recursion.
A fourth type, called level-order traversal,
well leave for another day.
9
Tree Recursion
preorder-tree-traversal visit the root of the
tree and do what you're going to do with it
(print it, collect it, stop if it's what you're
looking for, add it to a list, etc.) call
preorder-tree-traversal on the left subtree of
the root call preorder-tree-traversal on the
right subtree of the root
10
Tree Recursion
post-order-tree-traversal call
post-order-tree-traversal on the left subtree of
the root call post-order-tree-traversal on the
right subtree of the root visit the root of the
tree and do what you're going to do with it
(print it, collect it, stop if it's what you're
looking for, whatever)
11
Tree Recursion
In-order-tree-traversal call in-order-tree-trave
rsal on the left subtree of the root visit the
root of the tree and do what you're going to do
with it (print it, collect it, stop if it's what
you're looking for, whatever) call
in-order-tree-traversal on the right subtree of
the root
12
Traversal Summary
13
A new type of Tree
One of the more famous types of trees in Computer
Science builds off the idea of Binary Trees. As
you recall, a binary tree is a tree structure in
which each node has at MOST two child nodes
linked directly off the parent node
14
empty
empty
empty
empty
By itself, a binary tree has no inherent order to
it. There is no apparent relationship between
the data in the parent node and the data in the
child nodes.
15
Binary Search Trees
What if we imposed a relationship between the
data? This is the idea behind the definition of a
Binary Search Tree. When we consider a node of
a binary search tree, we state the following
  • All data contained in child nodes on one side of
    our node will be less than the data in the parent
    node.
  • All data contained in child nodes on the other
    side of our node will be greater than the data in
    the parent node
  • These properties will be consistent for all nodes
    in our tree.

16
Translated
  • The way that we normally translate this statement
    is
  • When we look at a node in our BST (binary search
    tree)
  • All data to the (conceptual) left subtree of the
    current node will be less than the current data
    (data at this node)
  • All data to the (conceptual) right subtree of the
    current node will be greater than the current
    data (data at this node)

17
Visually
RIGHT
LEFT
NOTE This is just an example. BSTs are not
limited to just numbers.
18
Examples
Were going to be looking at a couple of
definitions for these examplesone for BTs
(binary trees) and one for BSTs (binary search
trees). Both trees will contain only numbers for
simplicitys sake
Data Analysis and Definition (define-struct
node (data left right)) A BT is either
1) empty 2) a node structure
(make-node num lt rt) where num is a
number and lt rt are BTs
19
a BST is a BT 1) empty is
always a BST 2) (make-node d lft rgt)
is a BST if and only if
a) lft rgt are BSTs b)
all d numbers in lft are smaller than d
c) all d numbers in rgt are larger
than d
Note that all were doing here is clarifying the
difference between a BT and a BST. Nothing
extra was added to the definition.
20
Data definition for BST (HW11)
  • A binary-search-tree(BST) is either
  • 1. empty or
  • 2. (make-node soc pn lft rgt) is a BST where
  • (a) soc is a number,
  • (b) pn is a symbol,
  • (c) lft and rgt are BSTs,
  • (d) all ssn numbers in lft are smaller
    than soc, and
  • (e) all ssn numbers in rgt are larger
    than soc.

21
So what does this buy us?
Well, lets look at searching for values in a BT
versus a BST. Lets start with searching in an
ordinary Binary Tree of numbers Were looking
for the number 7 in the following tree
22
(No Transcript)
23
Lets say that the basic algorithm is the
following 1) If we found the number, return
true 2) If we run out of tree, return false
3) Otherwise, search the left subtree for the
value 4) If we dont find it there, try the
right
24
So we start off
Nope, 30 doesnt equal 7
25
Nope.
26
Not even
27
Ran out of stuff To try here, lets Try the
right-hand side
28
Nope And if we try to recur left or right, we
find out that weve run out of places to check
in this sub-tree
29
So we come back up and try the right subtree of
60 With no luck
30
So we go back to the other subtree of 30, where
we started out
31
This process keeps repeating until we either
find the value or run out of places to try
completely
32
This process keeps repeating until we either
find the value or run out of places to try
completely
33
Finally, we find what we were looking for
34
Ok, now a BST
This time were going to look for the value 100,
starting from the 35 node.
35
As before, we check to see if our current node is
the value were looking for (100). Again, we
fail. But somethings different this time
36
This time were dealing with a BST. And we know
something about BSTs. All data that is smaller
than the current data is stored in the left
subtree. All the data thats larger than the
current subtree is in the right subtree
37
So. Do we have to go and explore the entire left
subtree if were looking for 100 and weve hit 35?
38
Nopewe just search the right subtree
39
Again, do we have to search to the left?
40
Nowe just go right And weve found it
41
So our algorithm seems to be
  • If we are searching an empty tree at this point,
    return false
  • If the value were looking at seems to be the
    right one, return true
  • Otherwise, if what were looking for is less than
    the current nodes value, recur with the left
    subtree.
  • Otherwise, recur with the right subtree

42
What if we wanted to do other things like add to
and delete from a BST?
Youll find that as long as were dealing with
BSTs, the same general set of rules will hold
43
Inserting into a BST
Inserting an item into a BST is very much so like
the problem of inserting an item into a sorted
list. We want to maintain the BST property of
our BST when were all through! So lets ask a
couple of questions
44
Could we insert an item at the root of a BST?
18
45
Could we insert an item at the root of a BST?
18
We run into problems if we just arbitrarily
insert items at the root of our BST. Wed have
to guarantee that we still have a BST when were
done. As is shown here, wed have to
really re-arrange our tree to make this back into
a BST And thats too much trouble
46
What about inserting somewhere in the middle?
47
What about inserting somewhere in the middle?
This is even more complex than the first
case How would we re-arrange our tree?
48
Inserting at leaf node
This is pretty much the only easy way to insert
items into a BST. Instead of re-arranging our
tree because of a random insertion, we merely
create new leaf nodes off our branches (Replace
an empty with a non-empty new node!)
49
The algorithm
  • If our tree is empty, we should insert the value
    here by creating a new node in the tree
  • Otherwise, we need to make sure that we find the
    correct place to insert a new leaf node and
    maintain the property of the BST.
  • Is the current value of our tree bigger than the
    value were trying to insert? If it is, we
    should look for an empty place to the left of the
    current node
  • Otherwise, we should look for an empty location
    to the right
  • Repeat that until we find an empty to replace

50
Visually
Value to add
Tree to insert into
38
51
Visually
Value to add
Tree to insert into
38
Have we found an empty place to park our new
value?
52
Visually
Value to add
Tree to insert into
38
Nope. 35 lt 38, so we should look for our empty
location to the right of 35.
53
Visually
Value to add
Tree to insert into
38
76 isnt empty either. And its bigger than 38.
So we need to go left
54
Visually
Value to add
Tree to insert into
38
Weve hit the same situationgo left again
55
Visually
Value to add
Tree to insert into
38
Theres nothing after 40which means weve found
the right place to insert our item
56
Visually
Value to add
Tree to insert into
38
38
57
Deleting from a BST
Just as we strove to maintain the BST properties
of our tree when adding an item to a BST, we also
need to maintain the properties of our BST when
we delete items. This is actually a fairly
complex problem. It is moremanageable if we
break the problem down into different cases.
First, lets assume that were already at the
node that we want to delete from our tree
58
The case of just one node
35
empty
empty
empty
59
The case of just one child
76
35
40
100
76
empty
40
100
60
The other case
This is perhaps the hardest case to solve. We
want to replace 35 with the most viable candidate
in our BST and still maintain the BST property
61
The other case
Believe it or not, there are two viable
candidates in this tree
62
The other case
The largest value on the left side of the tree,
or.
63
The other case
The smallest value on the right side of the
tree. Lets discuss why
64
The largest and the smallest
No matter how you construct your tree, no matter
how many different values you put into it, the
value that is just larger than the root nodes
value will be the smallest (leftmost) value in
the roots right subtree. Also, the value
closest to the roots value, but just less than
it will be the largest (rightmost) value in the
left subtree of the root node. Using either of
those as a replacement for the root node will not
cause the BST ordering to be upset.
65
The algorithm.
The algorithm for finding the largest value of
the left subtree is to go left once and then
recur as far down the right subtree as possible.
66
The algorithm.
The algorithm for finding the smallest value of
the right subtree is to go right once and then
recur as far down the left subtree as possible.
67
Continued
Once we find the correct value, we replace our
original value (35) with the new one (lets say
we opted for the smallest of the right subtree,
40). We copy the value 40 onto our root node.
68
Continued
Now we need to delete the other 40 in the tree.
69
Continued
Now we need to delete the other 40 in the tree.
Note that we have to move the 50 up to maintain
the BST!
70
Continued
The 50 is now moved up, original 40 was replaced.
40
76
20
25
1
50
100
10
Write a Comment
User Comments (0)
About PowerShow.com