cs 1102 tutorial 7

33
CS1102 Tut 7 Trees Max Tan [email protected]  S15-03-07 Tel:65164364 http://www.comp.nus.edu.sg/~tanhuiyi

Upload: gobara-dhan

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cs 1102 Tutorial 7

8/12/2019 Cs 1102 Tutorial 7

http://slidepdf.com/reader/full/cs-1102-tutorial-7 1/33

CS1102 Tut 7 Trees

Max Tan

[email protected] 

S15-03-07 Tel:65164364http://www.comp.nus.edu.sg/~tanhuiyi

Page 2: Cs 1102 Tutorial 7

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

Page 3: Cs 1102 Tutorial 7

8/12/2019 Cs 1102 Tutorial 7

http://slidepdf.com/reader/full/cs-1102-tutorial-7 3/33

First 15 minutes

 Any questions?

Page 4: Cs 1102 Tutorial 7

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

Page 5: Cs 1102 Tutorial 7

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!

Page 6: Cs 1102 Tutorial 7

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

Page 7: Cs 1102 Tutorial 7

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 

Page 8: Cs 1102 Tutorial 7

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)

Page 9: Cs 1102 Tutorial 7

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?

Page 10: Cs 1102 Tutorial 7

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

t  c 

c  a 

2  1  1  1 Leaf nodes to store

the repeated counts

Page 11: Cs 1102 Tutorial 7

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.

Page 12: Cs 1102 Tutorial 7

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! 

Page 13: Cs 1102 Tutorial 7

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)

Page 14: Cs 1102 Tutorial 7

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!

Page 15: Cs 1102 Tutorial 7

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?

Page 16: Cs 1102 Tutorial 7

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?

Page 17: Cs 1102 Tutorial 7

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?

Page 18: Cs 1102 Tutorial 7

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?

Page 19: Cs 1102 Tutorial 7

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?

Page 20: Cs 1102 Tutorial 7

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!

Page 21: Cs 1102 Tutorial 7

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

Page 22: Cs 1102 Tutorial 7

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

Page 23: Cs 1102 Tutorial 7

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

Page 24: Cs 1102 Tutorial 7

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!

Page 25: Cs 1102 Tutorial 7

8/12/2019 Cs 1102 Tutorial 7

http://slidepdf.com/reader/full/cs-1102-tutorial-7 25/33

Question 4

 AVL trees and rotations

Page 26: Cs 1102 Tutorial 7

8/12/2019 Cs 1102 Tutorial 7

http://slidepdf.com/reader/full/cs-1102-tutorial-7 26/33

Question 4

Page 27: Cs 1102 Tutorial 7

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

Page 28: Cs 1102 Tutorial 7

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 

Page 29: Cs 1102 Tutorial 7

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 

Page 30: Cs 1102 Tutorial 7

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 

Page 31: Cs 1102 Tutorial 7

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 

Page 32: Cs 1102 Tutorial 7

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 

Page 33: Cs 1102 Tutorial 7

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!