Title: CS1102 Tut 7 Trees
1CS1102 Tut 7 Trees
- Max Tan
- tanhuiyi_at_comp.nus.edu.sg
- S15-03-07 Tel65164364http//www.comp.nus.edu.sg
/tanhuiyi
2Groups assignment
- Question 1 Group 3
- Question 2 Group 4
- Question 3 Group 1
- Question 4 Group 2
3First 15 minutes
4First 15 minutes
- Trees VS LinkedList
- Rotate Left and Rotate Right
5Question 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!
6Question 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
7Question 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
8Question 1
- What is the complexity?
- Height of the tree is logn
- addWord() is O(logn)
- getWordCount() is O(logn)
9Question 1
- Can we do better?
- Question states very clearly that addWord and
getWord count should be independent of n - Is that possible?
10Question 1
- Store letters in nodes instead of words!
- E.g. CATTCAT and length 3 words
Leaf nodes to store the repeated counts
11Question 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.
12Question 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!
13Question 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)
14Question 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!
15Question 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?
16Question 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?
17Question 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?
18Question 2
- Do you see the connection?
19Question 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?
20Question 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!
21Question 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
22Question 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
23Question 3
- Example
- Preorder 1 2 4 5 8 9 3 6 0 7
- Inorder 4 2 8 5 9 1 6 0 3 7
24Question 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!
25Question 4
26Question 4
27Question 4
- Step 1 83 is removed, Violation at 95, so rotate
left at 95
28Question 4
- Step 1 83 is removed, Violation at 95, so rotate
left at 95 (result)
29Question 4
- Step 2 Equivalent to insert inside, so we need
to rotate left about 34 first
30Question 4
- Step 2 Equivalent to insert inside, so we need
to rotate left about 34 first (result)
31Question 4
- Step 3 Violation at 67, so rotate right about 67
32Question 4
- Step 3 Violation at 67, so rotate right about 67
(result)
33Question 4
- Exercise Implement your own AVL tree in Java!