You are on page 1of 33

CS1102 Tut 7 Trees

Max Tan
tanhuiyi@comp.nus.edu.sg
S15-03-07 Tel:65164364
http://www.comp.nus.edu.sg/~tanhuiyi
Groups assignment
Question 1 Group 3
Question 2 Group 4
Question 3 Group 1
Question 4 Group 2
First 15 minutes
Any questions?
First 15 minutes
Trees VS LinkedList
Rotate Left and Rotate Right
Question 1
Given a long string S, design an ADT
to facilitate the query of the occurrence
of words of length l.
Sounds familiar?
MultiSet ADT in your first lab!
Question 1
ADT Operations
addWord()
getWordCount()
We dont know which are the words,
so we have to add all length l
consecutive words into the Set
Question 1
What is the easy way out?
A binary search tree! Treat each letter as a digit in a
number!
Also need to store the repetitions of each word
CAT, 2
ATT,1 TTC,1
TCA,1
Question 1
What is the complexity?
Height of the tree is logn
addWord() is O(logn)
getWordCount() is O(logn)
Question 1
Can we do better?
Question states very clearly that
addWord and getWord count should
be independent of n
Is that possible?
Question 1
Store letters in nodes instead of words!
E.g. CATTCAT and length 3 words
c
a
t
a
t
t
t
t c
c a
2 1 1 1
Leaf nodes to store
the repeated counts
Question 1
What is the complexity?
Height of the tree is at most l + 2
Number of children per node is at most s
where s is the number of characters in the
alphabet
So to fetch the word count, we need to
traverse the tree letter by letter until we
reach the leaf.
For each letter, we have to determine the
child. There are at most s children.
Question 1
Hence,
addWord() is O(sl)
getWordCount() is O(sl)
Independent of n!
We can also use an array to store the
children to reduce the time complexity
to O(l) but the array may be sparse!
Space trade off for speed!
Question 2
Non recursive algorithm that does in-order
tree walk.
Recall

inorder(T)
if T is not empty then
inorder(T.left)
print T.item
inorder(T.right)
Question 2
How do we do this iteratively?
We have to remember which node
we were previously at to determine the
action on the current node!
Question 2
What if the previous
node traversed was
node 3?
Which step of the
recursive function are
we at?
3
6 7
Current
node
Previous
node
inorder(T)
if T is not empty then
inorder(T.left)
print T.item
inorder(T.right)
What should be the next
node?
Question 2
What if the previous
node traversed was
node 6?
Which step of the
recursive function are
we at?
3
6 7
Previous
node
Current
node
inorder(T)
if T is not empty then
inorder(T.left)
print T.item
inorder(T.right)
What should be the next
node?
Question 2
What if the previous
node traversed was
node 7?
Which step of the
recursive function are
we at?
3
6 7
Previous
node
Current
node
inorder(T)
if T is not empty then
inorder(T.left)
print T.item
inorder(T.right)
What should be the next
node?
Question 2
Do you see the connection?
Question 2
printIterative(root)
{
prev = null;
curr = root;
while(curr != null)
{
if(prev == current.parent)
{
next = current.left
if(next == null)
{
print current.value
next = current.right
if next == null
next = current.parent
}
}
Previous node was parent
node
So since we are just at the
current node, we want to print
the left subtree first
What if there is no left
subtree?
Question 2
else if(prev == current.left)
{
print current.value
next = current.right
if next == null
next = current.parent
}
else
next = current.parent
prev = current
current = next
}
}
Previous node was left
subtree
Since we have traversed left
subtree, we can safely print
the current node before
traversing right!
Previous node was right
subtree.
So backtrack upwards!
Question 3
Construct a unique tree given an inorder
and preorder sequence
Recall how we get the inorder and preorder
sequence!
Preorder Print current node first, then print
left subtree then print right subtree
Inorder Left subtree is printed first, then
current node then right subtree
Question 3
So the first character in preorder is the
root!
All elements to the left of this character
in inorder is the LEFT subtree
All elements to the right of this
character in inorder is the RIGHT
subtree
Question 3
Example :
Preorder : 1 2 4 5 8 9 3 6 0 7
Inorder : 4 2 8 5 9 1 6 0 3 7
Root! Left subtree! Right subtree
Question 3
Algorithm
Root = First character in preorder sequence
Get the index of this character in inorder
sequence
All characters before this index lies in the LEFT
subtree
All characters after this index lies in the RIGHT
subtree
Construct left and right subtrees recursively!

Question 4
AVL trees and rotations
Question 4
Question 4
Step 1: 83 is removed, Violation at 95, so
rotate left at 95
Question 4
Step 1: 83 is removed, Violation at 95, so
rotate left at 95 (result)
67
34 96
25 48 95 98
12 42
46
55
Question 4
Step 2: Equivalent to insert inside, so we
need to rotate left about 34 first
67
34 96
25 48 95 98
12 42
46
55
Question 4
Step 2: Equivalent to insert inside, so we
need to rotate left about 34 first (result)
67
48 96
34 55 95 98
25 42
46 12
Question 4
Step 3: Violation at 67, so rotate right about
67
67
48 96
34 55 95 98
25 42
46 12
Question 4
Step 3: Violation at 67, so rotate right about
67 (result)
67
48
96
34
55
95 98
25 42
46 12
Question 4
Exercise: Implement your own AVL
tree in Java!

You might also like