CS1102 Tut 7 Trees - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

CS1102 Tut 7 Trees

Description:

Question 1 Group 3. Question 2 Group 4. Question 3 ... So backtrack upwards! Question 3. Construct a unique tree given an inorder and preorder sequence ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 34
Provided by: soc128
Category:
Tags: cs1102 | trees | tut | upwards

less

Transcript and Presenter's Notes

Title: CS1102 Tut 7 Trees


1
CS1102 Tut 7 Trees
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
Groups assignment
  • Question 1 Group 3
  • Question 2 Group 4
  • Question 3 Group 1
  • Question 4 Group 2

3
First 15 minutes
  • Any questions?

4
First 15 minutes
  • Trees VS LinkedList
  • Rotate Left and Rotate Right

5
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!

6
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

7
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

8
Question 1
  • What is the complexity?
  • Height of the tree is logn
  • addWord() is O(logn)
  • getWordCount() is O(logn)

9
Question 1
  • Can we do better?
  • Question states very clearly that addWord and
    getWord count should be independent of n
  • Is that possible?

10
Question 1
  • Store letters in nodes instead of words!
  • E.g. CATTCAT and length 3 words

Leaf nodes to store the repeated counts
11
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.

12
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!

13
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)

14
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!

15
Question 2
  • What if the previous node traversed was node 3?
  • Which step of the recursive function are we at?

Previous node
Current node
inorder(T) if T is not empty
then inorder(T.left) print T.item inorder(T.r
ight)
What should be the next node?
16
Question 2
  • What if the previous node traversed was node 6?
  • Which step of the recursive function are we at?

Current node
Previous node
inorder(T) if T is not empty
then inorder(T.left) print T.item inorder(T.r
ight)
What should be the next node?
17
Question 2
  • What if the previous node traversed was node 7?
  • Which step of the recursive function are we at?

Current node
Previous node
inorder(T) if T is not empty
then inorder(T.left) print T.item inorder(T.r
ight)
What should be the next node?
18
Question 2
  • Do you see the connection?

19
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?
20
Question 2
Previous node was left subtree
  • 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

Since we have traversed left subtree, we can
safely print the current node before traversing
right!
Previous node was right subtree. So backtrack
upwards!
21
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

22
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

23
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

24
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!

25
Question 4
  • AVL trees and rotations

26
Question 4
27
Question 4
  • Step 1 83 is removed, Violation at 95, so rotate
    left at 95

28
Question 4
  • Step 1 83 is removed, Violation at 95, so rotate
    left at 95 (result)

29
Question 4
  • Step 2 Equivalent to insert inside, so we need
    to rotate left about 34 first

30
Question 4
  • Step 2 Equivalent to insert inside, so we need
    to rotate left about 34 first (result)

31
Question 4
  • Step 3 Violation at 67, so rotate right about 67

32
Question 4
  • Step 3 Violation at 67, so rotate right about 67
    (result)

33
Question 4
  • Exercise Implement your own AVL tree in Java!
Write a Comment
User Comments (0)
About PowerShow.com