CS1102 Tut 6 Trees - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

CS1102 Tut 6 Trees

Description:

( You saw AVL trees) ... 6. First 15 minutes. Every node (except the root node) must ... Question 6. Can we construct a binary tree of height 4 with 19 nodes? ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 46
Provided by: dcsa
Category:
Tags: cs1102 | saw | trees | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 6 Trees


1
CS1102 Tut 6 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
  • Question 5 Group 3
  • Question 6 Group 4
  • Question 7 Group 1
  • Question 8 Group 2

3
Groups Assignment
  • Question 1 Group 3
  • Question 2 Group 1
  • Question 3 Group 2
  • Question 4 Group 3
  • Question 5 Group 1
  • Question 6 Group 2
  • Question 7 Group 3
  • Question 8 Group 1

4
First 15 minutes
  • If you need help with anything in CS1102, please
    make an appointment with me.
  • If you wait until 1 week before the exam, it will
    be too late
  • The toughest part of the module are up ahead.
    (You saw AVL trees)
  • The CS1102 module next sem will be harder then
    this sem, there is also a PE, but this doesnt
    concern you right ? RIGHT ????

5
First 15 minutes
  • A tree is defined recursively (I promised you
    more recursion two tutorials ago)


6
First 15 minutes
  • Every node (except the root node) must have
    exactly one parent
  • A node must be internal or leaf
  • An internal node has at least one children
  • A left node has no children.

7
First 15 minutes
  • NumOfNodes(Node n) 1 NumOfNodes(n.left)
    NumOfNodes(n.right)


8
First 15 minutes
  • Height(Node n) 1 Max(SumOfNode(n.left), S
    umOfNode(n.right))


9
First 15 minutes
  • Array based vs reference based

A binary tree holds a very nice property The
number of nodes in the next level is twice the
number of nodes in the previous level The number
of nodes in the next level is also 1 more then
the total number of nodes in the levels above it
0
1
2
3
4
5
6
7
8
Left child (2i 1) Right child (2i 2)
10
First 15 minutes Inorder
  • Inorder (Or you can read it as IN the same
    order)
  • Print LeftSubTree, print current, print
    RightSubTree

Output
11
First 15 minutes Preorder
  • Preorder (Or you can read it as BEFORE order)
  • Print LeftSubTree, print current, print
    RightSubTree

Output
12
First 15 minutes Postorder
  • Postorder (Or you can read it as AFTER order)
  • Print LeftSubTree, print current, print
    RightSubTree

Output
13
First 15 minutes
  • Take note that PRE and POST are NOT reverse of
    each other (use a simple example 1, 2, 3)

POST
PRE
14
First 15 minutes Inorder
  • Take note that PRE and POST are NOT reverse of
    each other (use a simple example 1, 2, 3)

15
Question 1
  • Draw the resulting binary search tree after
    performing each of the following operation
    consecutively on an initially empty binary search
    tree insert 8, insert 6, insert 12, insert 3,
    insert 10, insert 9, delete 12, delete 8, and
    delete 9.

16
Question 1
  • insert 8, insert 6, insert 12, insert 3, insert
    10, insert 9

8
6
12
3
10
9
17
Question 1
  • delete 12

8
8
6
10
6
12
3
3
9
10
9
18
Question 1
Where is the smallest node in the right
tree? The leftmost child of the right
subtree Question Can you take the biggest node
in the left tree instead? How would you find this
node?
  • delete 8

8
9
6
10
6
10
3
3
9
19
Question 1
  • delete 9

9
10
6
10
6
3
3
20
Question 2
  • Suppose you have a binary search tree T
    containing only distinct items. The pre-order
    sequence of T is 20, 10, 5, 7, 15, 12, 18, 30,
    25, 37, and 40. Can you reconstruct T from this
    traversal sequence? If yes, reconstruct T.

21
Question 2
  • The pre-order sequence of T is 20, 10, 5, 7, 15,
    12, 18, 30, 25, 37, and 40.
  • Definition of preorder
  • Print curr node
  • Print left subtree
  • Print right subtree

22
Question 2
  • The pre-order sequence of T is 20, 10, 5, 7, 15,
    12, 18, 30, 25, 37, and 40.
  • 20 10 5 7 15 12 18 30 25 37 40

root
Left subtree
Right subtree
23
Question 2
  • 20 10 5 7 15 12 18 30 25 37 40

20
24
Question 2
20
10
30
5
15
25
37
40
7
12
18
25
Question 3
  • Suppose you have a binary tree T containing only
    distinct items. The pre-order sequence of T is 8,
    7, 5, 28, 4, 9, 18, 17, 16, and the in-order
    sequence of T is 5, 7, 4, 28, 9, 8, 18, 16, and
    17. Can you reconstruct T from these two
    traversal sequences? If yes, reconstruct it.

26
Question 3
  • The pre-order sequence of T is
  • 8, 7, 5, 28, 4, 9, 18, 17, 16
  • The in-order sequence of T is
  • 5, 7, 4, 28, 9, 8, 18, 16, and 17

27
Question 3
  • Pre 8, 7, 5, 28, 4, 9, 18, 17, 16
  • In 5, 7, 4, 28, 9, 8, 18, 16, 17

8
Root!
28
Question 3
  • Pre 8, 7, 5, 28, 4, 9, 18, 17, 16
  • In 5, 7, 4, 28, 9, 8, 18, 16, 17

8
Left subtree
29
Question 3
  • Pre 8, 7, 5, 28, 4, 9, 18, 17, 16
  • In 5, 7, 4, 28, 9, 8, 18, 16, 17

8
right subtree
30
Question 3
  • Pre 7, 5, 28, 4, 9
  • In 5, 7, 4, 28, 9,

Recursively solve for the left and right subtrees!
31
Question 3
8
7
18
5
28
17
4
9
16
32
Question 4
  • Is the following statement true or false?
  • The leaf nodes of a binary tree occur in the same
    relative positions in pre-order, post-order,
    in-order, and level-order traversals.

33
Question 4
  • Use examples

Inorder 5 10 12 15 18 20 25 30 37 40 Preorder
20 10 5 15 12 18 30 25 37 40 Postorder 5 12 18
15 10 25 40 37 30 20 Levelorder 20, 10, 30, 5,
15, 25, 37, 12, 18, 40
20
10
30
5
15
25
37
False!
40
12
18
34
Question 5
  • If you insert an item into a binary search tree
    then delete it from the tree, will you always get
    back the original tree? How about first delete an
    item from a binary search tree then insert it
    back, will you always get back the original tree?

35
Question 5
  • If you insert an item into a binary search tree
    then delete it from the tree, will you always get
    back the original tree?

When you insert a node, it automatically becomes
a leaf node. When you delete it, the original
structure of the tree is obtained.
36
Question 5
  • How about first delete an item from a binary
    search tree then insert it back, will you always
    get back the original tree?

When you delete a node, which is non-leaf node
(e.g. the root) the root node is replaced. After
you try to insert it back into the tree, the node
becomes a leaf node. Hence you will not get back
the original tree.
37
Question 6
  • Can we construct a binary tree of height 4 with
    19 nodes?

38
Question 6
  • Can we construct a binary tree of height 4 with
    19 nodes?

Number of nodes in a full binary tree of height
4 25 1 15 So, answer is NO!
39
Question 7
  • How many null links are there in a binary tree of
    n nodes?

Nodes with null links
40
Question 7
  • Total number of edge references 2n (Each node
    has 2 edges or links)
  • Total number of edges in a Binary Tree n 1
    (definition of tree)
  • Hence number of null links
  • 2n (n-1)
  • n 1

41
Question 8
  • Write an algorithm to sort an input list of
    integers by constructing a binary search tree.
    What are the complexities of the worst case and
    the average case of this Treesort method?

42
Question 8
  • Add elements from the array into a BST.
  • Output the elements in an inorder traversal.

43
Question 8
  • Complexity analysis
  • Inserting an element into a BST is log n in the
    average case and n in the worst case (perfectly
    balanced against perfectly imbalanced)
  • Inserting n elements is thus nlogn (average) and
    n2 (worst)
  • Printing in inorder requires traversing n
    elements hence in n complexity.
  • Hence time complexity for treesort is nlogn in
    average and n2 in worst case

44
  • End

45
First 15 minutes
  • Lets look at the big picture

ADT
Array
A list is a special kind of tree where you only
use a single branch
Graphs
Others ?
Lists
Trees
A tree is a special kind of graph where each node
can have at most 1 incoming node AND does not
have cycles
Queue
Stack
AVL
Heap
Red Black
Binomal
Fibonacci
Write a Comment
User Comments (0)
About PowerShow.com