CS210- Lecture 10 June 21, 2005  Announcements

advertisement
CS210- Lecture 10
June 21, 2005
Announcements
 Assignment 3 has been posted
 Part 1 is due next Monday
 Part 2 is due next Tuesday
 Midterm is on June 30




Section 3.1 - 3.4
Chapter 4
Section 5.1 - 5.3, Section 5.5
Part of Chapter 6 that I will be able to cover till this
Thursday (06/23).
 I will return Assignment 1 on Thursday (06/23)
6/30/2016
CS210-Summer 2005, Lecture 10
1
Agenda



Binary Trees
Properties of Binary Trees
Binary Tree Traversals
6/30/2016
CS210-Summer 2005, Lecture 10
2
Linear data Structures

Here are some of the data structures
we have studied so far:





Arrays
Stacks, Queues and deques
Singly linked list and doubly linked list
These all have the property that their
elements can be adequately displayed
in a straight line.
Binary trees are one of the simplest
nonlinear data structures.
6/30/2016
CS210-Summer 2005, Lecture 10
3
Parts of a binary tree



A binary tree is composed of zero or more
nodes.
Each node can have at most two children.
Each node contains:




A value (some sort of data item)
A reference or pointer to a left child (may be null)
A reference or pointer to a right child (may be
null)
A binary tree may be empty (contain no
nodes)
6/30/2016
CS210-Summer 2005, Lecture 10
4
Parts of a Binary tree

If not empty, a binary tree has a root
node


Every node in the binary tree is reachable
from the root node by a unique path.
A node with neither a left child nor a
right child is called a leaf (external
node).
6/30/2016
CS210-Summer 2005, Lecture 10
5
Picture of a Binary Tree
a
c
b
f
e
d
g
h
i
j
k
k
6/30/2016
CS210-Summer 2005, Lecture 10
6
Binary search in an array


Look at array location mid = (low + high)/2
Searching for 5: mid = ( 0 + 6)/2 = 3
high = (mid -1)= 2
Using a binary
search tree
Low = (mid + 1) = 2
mid = (0 + 2)/2 = 1
mid = (2 + 2)/2 = 2
7
0
2
1
2
3
4
5
6
3
5
7
11 13 17
2
6/30/2016
13
3
CS210-Summer 2005, Lecture 10
5
11
17
7
The Binary Tree ADT

The binary tree is a specialization of a
tree that supports these additional
accessor methods:




Left(v): Return the left child of v.
Right(v): Return the right child of v.
hasLeft(v): Test whether v has a left child.
hasRight(v): Test whether v has a right
child.
6/30/2016
CS210-Summer 2005, Lecture 10
8
Binary Tree interface
public interface BinaryTree extends Tree{
public Position left(Position v) throws
InvalidPositionException,
BoundaryViolationException;
…
}
Assumptions on running times:
left(v), right(v), hasLeft(v), hasRight(v)
take O(1) time
children(v) take O(1) time because now there
can be at most two children.
6/30/2016
CS210-Summer 2005, Lecture 10
9
Properties of Binary Trees
Level
0
1
2
3
6/30/2016
CS210-Summer 2005, Lecture 10
10
Properties of Binary trees

Let





T - binary tree and let
n - number of nodes
ne – number of external nodes
ni – number of internal nodes
h – height of T

h + 1 <= n <= 2h+1 -1
1 <= ne <= 2h
h <= ni <= 2h – 1

log(n+1) – 1 <= h <= n -1


6/30/2016
CS210-Summer 2005, Lecture 10
11
Tree Traversals




A binary tree is defined recursively: it consists
of a root, a left subtree and a right subtree
To traverse (or walk) the binary tree is to visit
each node in the binary tree exactly once.
Tree traversals are naturally recursive.
Since a binary tree has three parts, there are
six possible ways to traverse the binary tree:



root, left, right : preorder (root, right, left)
left, root, right: inorder (right, root, left)
left, right, root: postorder (right, left, root)
6/30/2016
CS210-Summer 2005, Lecture 10
12
Traversals of a Binary tree

Preorder Traversal
Algorithm binaryPreorder(T, v):
perform the “visit” action for node v
if T.hasLeft(v) then
binaryPreorder (T, T.left(v))
if T.hasRight(v) then
binaryPreorder(T, T.right(v))
6/30/2016
CS210-Summer 2005, Lecture 10
13
Traversals of a Binary tree

Inorder Traversal
Algorithm inorder(T, v):
if T.hasLeft(v) then
inorder (T, T.left(v))
perform the “visit” action for node v
if T.hasRight(v) then
inorder(T, T.right(v))
6/30/2016
CS210-Summer 2005, Lecture 10
14
Traversals of a Binary tree

Postorder Traversal
Algorithm postorder(T, v):
if T.hasLeft(v) then
postorder (T, T.left(v))
if T.hasRight(v) then
postorder(T, T.right(v))
perform the “visit” action for node v
6/30/2016
CS210-Summer 2005, Lecture 10
15
Download