Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu.

Similar presentations


Presentation on theme: "CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu."— Presentation transcript:

1 CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu

2 Reviews  Ascending sort  1, 2, 3, 4, …  Descending sort  10, 9, 8, …  Greedy approach O(n 2 )  Selection sort  Insertion sort  Divide-and-conquer approach O(n log n)  Merge sort  Quick sort –2–2

3 What and How  A computing problem is described by “what” and “how”, for example,  Sorting is about “what”  Merge sort is about “how”  Given “what”, there could be many “how”.  Primary goal of algorithm design  Design an efficient the “how” that exactly solves the “what”  “Efficient”?  Space complexity: How much system resource it consumes  Time complexity: How fast it solves –3–3

4 Search x from a set S = {x 1, …, x n }  Takes n comparisons at worst  Will there be a way to reduce it?  Yes => Use “tree” structure => Use additional memory space to reduce the time complexity  Memory or Disks become cheaper but time is expensive most cases –4–4

5 Tree and Binary tree  A tree is composed of many nodes  root node, leaf node, non-leaf node  A binary tree is a tree that has two or less children nodes at every node –5–5 root leaf nonleaf leaf nonleaf

6 Binary tree  Preorder: [4,2,3,5,6,8,9]  Inorder: [2,3,4,6,5,8,9]  Postorder: [3,2,6,9,8,5,4] –6–6

7 Binary search tree  A special case of binary tree  Left children nodes contain smaller value than the parent node, the parent node contain smaller value than the right children nodes –7–7

8 Search using binary search tree  Search x from a binary search tree t  If t = Leaf, t does not contain x.  If t = Node(l,y,r) and x = y, t contains x in the root.  If t = Node(l,y,r) and x < y, search x in l.  If t = Node(l,y,r) and x > y, search x in r.  Searching x from a binary search tree takes O(log n) where searching x from a list takes O(n) –8–8

9 Search using binary search tree –9–9 Type of search:

10 Inserting into a binary search tree  Inserting x into a tree t  If t = Leaf, then create Node (Leaf, x, Leaf)  If t = Node (l, y, r) and x = y, then return t  If t = Node (l, y, r) and x < y, then return Node(l’, y, r) when l’ is the tree with x inserted.  If t = Node (l, y, r) and x > y, then return Node(l, y, r’) when r’ is the tree with x inserted. – 10

11 Inserting into a binary search tree – 11 Type of insert:

12 Deleting from a binary search tree  Deleting x from a tree t  If t=Leaf, then return Leaf  If t=Node(l,y,r) and x<y, then return Node(l’,y,r) when l’ is the l with x deleted  If t=Node(l,y,r) and x>y, then return Node(l,y,r’) when r’ is the r with x deleted  If t=Node(l,y,r) and x=y, then things become complicated… – 12

13 Deleting from a binary search tree  If t=Node(l,y,r) and x=y,  When l is a Leaf – 13

14 Deleting from a binary search tree  If t=Node(l,y,r) and x=y,  When l is a tree, – 14

15 Deleting from a binary search tree – 15 delete_max type:

16 Deleting from a binary search tree – 16 delete type:


Download ppt "CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu."

Similar presentations


Ads by Google