n cs202 fall 2012 lecture 17 – 10/30 trees10 tree example 11 cs202 fall 2012 lecture – 11/1...
Post on 15-Oct-2020
0 Views
Preview:
TRANSCRIPT
11/5/12
1
1
CS202 Fall 2012 Lecture 17 – 10/30
Trees Prof. Tanya Berger-Wolf
www.cs.uic.edu/~202
2
Array - Linked list – Queue - Stack runtime
Operation add to front add to back add at given index clear get find index of an object remove front element remove back element remove at given index set size toString
Array list O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(1) O(1) O(n)
Linked list O(1) O(1) O(n) O(1) O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(n)
Queue
O(1)
O(1)
O(1)
O(1) O(n)
Stack
O(1)
O(1)
O(1)
O(1) O(n)
3
Application: word search
Write an application that reads in the text of all the web pages in a website (say, www.cs.uic.edu) and then lets the user type words, and tells whether those words are contained in those pages or not.
1. How would we implement this with a List? 2. Would this be a good or bad implementation? 3. Does the ordering of the elements in the List affect the
algorithm? Could we use this information to our advantage?
4
List implementation
lists are bad for certain operations 1. insertion at arbitrary index:
add(index, element)
2. searching for an element: contains(element), indexOf(element)
3. removal from arbitrary index: remove(index)
all these operations are O(n) on lists! (bad)
a better data structure to implement this ADT is called a balanced binary search tree;
let's examine trees now…
11/5/12
2
5
Trees
tree: a set of linked nodes; a node may link to more than one other node 1. an extension / generalization of linked lists
a tree has a starting node called a root ; all other nodes are reachable from the root by the links between them
a node in a tree that does not link to other nodes is called a leaf
Goal: use a tree to build a collection that has O(log n) time for many useful operations
6
Tree diagram
7
Visualizing trees
every node links to a set of subtrees root of each subtree is a child of root r. r is the parent of each subtree.
r
T1
T2 T3
8
Tree terminology
leaf: node with no children siblings: two nodes with the same parent.
path: a sequence of nodes n1, n2, … , nk such that ni is the parent of ni+1 for 1 ≤ i < k 1. the length of a path is the number of edges in the path, or 1 less
than the number of nodes in it
depth or level: length of the path from root to the current node (depth of root = 0)
height: length of the longest path from root to any leaf
11/5/12
3
9
Tree path length and depth
10
Tree example
11
CS202 Fall 2012 Lecture – 11/1
Trees
12
Trees in computer science
family genealogy
organizational charts 1. corporate, government, military
folders/files on a computer
AI: decision trees
compilers: parse tree a = (b + c) * d;
d +
* a
=
c b
11/5/12
4
13
Trees in file systems
C:\
hw2
tcss342
hw1 proj1
MyMail
school
D101
pers
one.java calc.java test.java
each folder or file is a node 1. subfolders = children
14
Tree implementation
class TreeNode {
public Object element;
public TreeNode firstChild;
public TreeNode nextSibling;
}
C:\
hw2
tcss342
hw1 proj1
MyMail
school
D101
pers
one.java calc.java test.java
15
Tree traversals
traversal: visiting every node in a tree to process every element in it
three common traversal orderings (each one begins at the root):
1. preorder traversal: the current node is processed, then the node's child subtrees are traversed, in order
2. in-order traversal: the node's first child's subtree is traversed, then the current node itself is processed, then the node's remaining subtrees are traversed
3. postorder traversal: the node's child subtrees are traversed in order, and lastly the current node is processed
16
Preorder traversal
11/5/12
5
17
Preorder traversal example
procedure preorderTraverse(r) output(r) for each child c of r from left to right, preorderTraverse(c)
Output: a b e j k n o p f c d g l m h i 18
In-order traversal
19
In-order traversal example
Output: a b e j k n o p f c d g l m h i
procedure inorderTraverse(r) inorderTraverse(first child of r) output(r) for each child c of r from left to right, excluding first child, inorderTraverse(c)
20
Postorder traversal
11/5/12
6
21
Postorder traversal example
procedure postorderTraverse(r)
for each child c of r from left to right,
postorderTraverse(c)
output(r)
Output: j n o p k e f b c l m g h i d a 22
Binary trees
binary tree: a tree where all nodes have at most two children
public class BinaryTree<E> { private TreeNode root;
...
private static class TreeNode { public E element; public TreeNode left; public TreeNode right; } }
7 6
3 2
1
5 4
3
4
2
7
1
5
6
23
Binary tree traversals
three common binary tree traversal orderings (each one begins at the root):
1. preorder traversal: the current node is processed, then the node's left subtree is traversed, then the node's right subtree is traversed (CURRENT-LEFT-RIGHT)
2. in-order traversal: the node's left subtree is traversed, then the current node itself is processed, then the node's right subtree is traversed (LEFT-CURRENT-RIGHT)
3. postorder traversal: the node's left subtree is traversed, then the node's right subtree is traversed, and lastly the current node is processed (LEFT-RIGHT-CURRENT)
24
Binary tree preorder traversal order: C F T B R K G
11/5/12
7
25
Binary tree in-order traversal
order: B T R F K C G
26
Binary tree postorder traversal order: B R T K F G C
27
Infix, prefix, and postfix notation representation of math expressions as a binary tree 1. operators have their left and right operands as subtrees 2. literal values are stored as leaves
notations 1. prefix: Polish notation 2. infix: standard notation 3. postfix: reverse Polish notation
28
Expression example
11/5/12
8
29
Expression tree example
30
Evaluating
Evaluate this postfix expression: 7 2 3 * - 4 ^ 2 5 / +
31
Binary search trees
binary search tree (BST): a binary tree where every node n satisfies the following properties: 1. every element in n's left subtree has a value less than n's
element value 2. every element in n's right subtree has a value greater than n's
element value 3. n's left and right subtrees are binary search trees
BSTs' elements are stored in sorted order, which is helpful for searching
32
Binary search tree examples
Which of the following two trees are BSTs?
3
11 7 1
8 4
5
5
11 4 1
8 3
6
11/5/12
9
33
Why isn't this a BST?
4
18 10 7 2
11 5
8
20
21
15
34
BST operations
a BST allows us to use a tree to implement a set collection with operations like the following: 1. contains(element) 2. add(element) 3. getHeight 4. getMin, getMax 5. removeMin, removeMax 6. remove(element) 7. printInOrder, printPreOrder, printPostOrder
35
Searching a BST
We must be able to search a binary search tree for a given element's value, to implement the contains operation
Basic idea: compare the element e to be found to the element in the current node of the tree 1. if they are equal, we are done 2. if e is smaller, examine the left subtree 3. if e is greater, examine the right subtree
4. when can we stop searching?
BST methods are best implemented recursively
36
Implementing add
Basic idea: to add element e, find the node n that should be e 's parent, and set n 's child to be a new node containing e
to find parent node n: 1. if e is smaller, examine the left subtree 2. if e is greater, examine the right subtree 3. if we've hit a dead end, we are done; add here
11/5/12
10
37
Implementing add
Traverse from root to expected parent; place a new tree node as parent's left or right child
38
Implementing getMin/getMax
To find the maximum element in the BST, we follow right children until we reach null
To find the minimum element in the BST, we follow left children until we reach null
3
11 7 1
8 4
5
39
Implementing remove
Removing from a BST is challenging!
Removing an item disrupts the tree structure 1. We can't just delete a node like we could with a linked list. 2. The tree node could have one or two children, so it's not
immediately clear what should become of them.
Basic idea: Find the node that is to be removed. Then "fix" the tree so that it is still a binary search tree.
Three cases: 1. node to be removed has no children 2. node to be removed has one child subtree 3. node to be removed has two child subtrees
40
Implementing remove
no children: just set parent's child reference to null one child: replace the removed node with its subtree
3
11 7 1
8 4
5
3
11 7 1
8 4
5
11/5/12
11
41
Implementing remove
two children: replace the node with its successor (leftmost element of right subtree), then remove the successor from the tree
3
11 7 1
8 4
5
3
11 7 1
8 4
7
42
Tree size, height, runtime
for a binary tree t , of size n : 1. what is the maximum height of t ? n (why?) 2. what is the minimum height of t ? log n (can you prove it?) 3. what is the average height of t ? log n
for operations add, contains, remove, getMin, getMax, removeMin, removeMax: 1. what is their runtime proportional to? (height of the tree) 2. based on the numbers above, what is the average Big-Oh for
common tree operations? (log n)
top related