cs 1102 tutorial 7
TRANSCRIPT
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 1/33
CS1102 Tut 7 Trees
Max Tan
S15-03-07 Tel:65164364http://www.comp.nus.edu.sg/~tanhuiyi
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 2/33
Groups assignment
Question 1 – Group 3
Question 2 – Group 4
Question 3 – Group 1
Question 4 – Group 2
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 3/33
First 15 minutes
Any questions?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 4/33
First 15 minutes
Trees VS LinkedList
Rotate Left and Rotate Right
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 5/33
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!
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 6/33
Question 1
ADT Operations
addWord()
getWordCount()
We don’t know which are the words,
so we have to add all length l
consecutive words into the Set
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 7/33
Question 1
What is the easy way out?
A binary search tree! Treat each letter as a digit in anumber!
Also need to store the repetitions of each word
CAT, 2
ATT,1 TTC,1
TCA,1
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 8/33
Question 1
What is the complexity?
Height of the tree is logn
addWord() is O(logn)
getWordCount() is O(logn)
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 9/33
Question 1
Can we do better?
Question states very clearly that
addWord and getWord count shouldbe independent of n
Is that possible?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 10/33
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
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 11/33
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 thealphabet
So to fetch the word count, we need totraverse 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.
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 12/33
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 complexityto O(l ) but the array may be sparse!Space trade off for speed!
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 13/33
Question 2
Non recursive algorithm that does in-ordertree walk.
Recall –
inorder(T)if T is not empty then
inorder(T.left)print T.item
inorder(T.right)
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 14/33
Question 2
How do we do this iteratively?
We have to “remember” which node
we were previously at to determine theaction on the current node!
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 15/33
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?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 16/33
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?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 17/33
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?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 18/33
Question 2
Do you see the connection?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 19/33
Question 2
printIterative(root)
{
prev = null;
curr = root;
while(curr != null)
{
if(prev == current.parent){
next = current.left
if(next == null)
{
print current.valuenext = 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 leftsubtree?
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 20/33
Question 2
else if(prev == current.left)
{
print current.value
next = current.right
if next == null
next = current.parent
}
else
next = current.parent
prev = currentcurrent = 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!
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 21/33
Question 3
Construct a unique tree given an inorder
and preorder sequence
Recall how we get the inorder and preordersequence!
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
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 22/33
Question 3
So the first character in preorder is the
root!
All elements to the left of this characterin inorder is the LEFT subtree
All elements to the right of this
character in inorder is the RIGHTsubtree
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 23/33
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
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 24/33
Question 3
Algorithm
Root = First character in preorder sequence
Get the index of this character in inordersequence
All characters before this index lies in the LEFTsubtree
All characters after this index lies in the RIGHTsubtree
Construct left and right subtrees recursively!
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 25/33
Question 4
AVL trees and rotations
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 26/33
Question 4
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 27/33
Question 4
Step 1: 83 is removed, Violation at 95, sorotate left at 95
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 28/33
Question 4
Step 1: 83 is removed, Violation at 95, sorotate left at 95 (result)
67
34 96
25 48 95 98
12 42
46
55
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 29/33
Question 4
Step 2: Equivalent to insert inside, so weneed to rotate left about 34 first
67
34 96
25 48 95 98
12 42
46
55
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 30/33
Question 4
Step 2: Equivalent to insert inside, so weneed to rotate left about 34 first (result)
67
48 96
34 55 95 98
25 42
46 12
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 31/33
Question 4
Step 3: Violation at 67, so rotate right about67
67
48 96
34 55 95 98
25 42
46 12
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 32/33
Question 4
Step 3: Violation at 67, so rotate right about67 (result)
67
48
96
34
55
95 98
25 42
46 12
8/12/2019 Cs 1102 Tutorial 7
http://slidepdf.com/reader/full/cs-1102-tutorial-7 33/33
Question 4
Exercise: Implement your own AVL
tree in Java!