Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 172– Data Structures and Algorithms

Similar presentations


Presentation on theme: "CSC 172– Data Structures and Algorithms"— Presentation transcript:

1 CSC 172– Data Structures and Algorithms
Fall 2017 TuTh 3:25 pm – 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

2 Announcement Sorting dance Extra credit opportunity:
(Any sorting algorithm) Will replace your worst project score You need to have 10+ students Multiple workshop teams together?! You have to upload the video to YouTube and provide us the link by Dec 08 (include the list of students participated) Bonus points for the best dance! CSC172, Fall 2017

3 Binary Trees Extremely useful data structure Special cases include
Huffman tree Expression tree Decision tree (in machine learning) Heap data structure (later lecture) Binary Trees CSC172, Fall 2017

4 Binary Trees 5 Root left right 4 2 Depth 2 right 3 9 Height 3
9 Height 3 3, 7, 1, 9 are leaves 8 1 5, 4, 0, 8, 2 are internal nodes Height 1 7 CSC172, Fall 2017

5 Ancestors and Descendants
5 4 2 3 9 1, 0, 4, 5 are ancestors of 1 8 1 0, 8, 1, 7 are descendants of 0 7 CSC172, Fall 2017

6 Expression Trees - * / 3 4 + * 3 2 - 5 6 3 4*(3+2) – (6-3)*5/3
CSC172, Fall 2017

7 How to construct Expression Trees?
Infix 4*(3+2) – (6-3)*5/3 Shunting Yard Algorithm Postfix First Convert the Infix expression into Postfix using Shunting Yard Algorithm. * 6 3 – 5 * 3 / - CSC172, Fall 2017

8 How to construct Expression Trees?
* 6 3 – 5 * 3 / - 4 3 2 + * 6 3 - 5 * 3 / - Assume, each of these values are stored in an array list. CSC172, Fall 2017

9 How to construct Expression Trees?
4 3 2 + * 6 3 - 5 * 3 / - Stack You maintain a separate stack for the construction of Expression Tree. If current element from the array list is a number, create a BTNode (both children as null) and push it to the stack, if the current element element is an operator, create a new Node, pop the last two elements, and make them the right and left child respectively and then push the new BTNode (which is currently holding the tree structure) to the Stack. Continue. CSC172, Fall 2017

10 How to construct Expression Trees?
* 6 3 - 5 * 3 / - 4 + Stack 3 2 CSC172, Fall 2017

11 How to construct Expression Trees?
6 3 - 5 * 3 / - * Stack 4 + 3 2 CSC172, Fall 2017

12 How to construct Expression Trees?
- 5 * 3 / - * 6 3 Stack 4 + 3 2 CSC172, Fall 2017

13 5 * 3 / - * - Stack 3 4 + 6 3 2 CSC172, Fall 2017

14 How to construct Expression Trees?
* 3 / - * - 5 Stack 3 4 + 6 3 2 CSC172, Fall 2017

15 How to construct Expression Trees?
3 / - * * Stack 4 + - 5 3 6 3 2 CSC172, Fall 2017

16 How to construct Expression Trees?
/ - * * 3 4 + - 5 3 6 3 2 CSC172, Fall 2017

17 How to construct Expression Trees?
- * / * 3 4 + - 5 3 2 3 6 CSC172, Fall 2017

18 How to construct Expression Trees?
- * / * 3 4 + - 5 3 2 3 6 CSC172, Fall 2017

19 Finally! - * / 3 4 + * 3 2 - 5 6 3 4*(3+2) – (6-3)*5/3
CSC172, Fall 2017

20 Character Encoding Each character occupies 8 bits
UTF-8 encoding: Each character occupies 8 bits For example, ‘A’ = 0x41 A text document with 109 characters is 109 bytes long But characters were not born equal CSC172, Fall 2017

21 English Character Frequencies
CSC172, Fall 2017

22 Variable-Length Encoding: Idea
Encode letter E with fewer bits, say bE bits Letter J with many more bits, say bJ bits We gain space if where f is the frequency vector Problem: how to decode? CSC172, Fall 2017

23 One Solution: Prefix-Free Codes
c e b a CSC172, Fall 2017

24 Why only Binary tree? CSC172, Fall 2017

25 Any Tree can be “Encoded” as a Binary Tree
CSC172, Fall 2017

26 LMC-RS Representation
In this representation, every node has two pointers: LMC (Left-most-child) RS (Right Sibling) CSC172, Fall 2017

27 LMC-RS Representation
public class Node {     public int key;     public Node lmc, rs;     public Node(int item)     {         key = item;         lmc= rs= null;     } } key lmc rs CSC172, Fall 2017

28 Tree Walks/Traversals
There are many ways to traverse a binary tree (reverse) In order (reverse) Post order (reverse) Pre order Level order = breadth first Tree Walks/Traversals CSC172, Fall 2017

29 FULL vs. COMPLETE BINARY TREE
CSC172, Fall 2017

30 Full Binary Tree 5 2 4 3 Each node is either
Each node is either (1) an internal node with exactly two non-empty children or (2) a leaf 8 1 CSC172, Fall 2017

31 Complete Binary Tree 5 4 2 3 1 9 8 12 7 6 13 CSC172, Fall 2017

32 Complete Binary Tree has a restricted shape obtained by starting at the root and filling the tree by levels from left to right. In the complete binary tree of height d, all levels except possibly level d−1 are completely full. The bottom level has its nodes filled in from the left side. CSC172, Fall 2017

33 Let’s see the examples again
CSC172, Fall 2017

34 Full vs. Complete Binary Tree
5 4 2 3 8 1 5 4 2 3 1 9 8 12 7 6 13 Full but not complete Complete but not full CSC172, Fall 2017

35 Tree using java CSC172, Fall 2017

36 A BTNode in Java public class Node { public int key;
    public Node left, right;     public Node(int item)     {         key = item;         left = right = null;     } } key left right CSC172, Fall 2017

37 Inorder Traversal Inorder-Traverse(BTNode root)
Inorder-Traverse(root.left) Visit(root) Inorder-Traverse(root.right) Also called the (left, node, right) order CSC172, Fall 2017

38 Inorder Printing in C++
void inorder_print(BTNode root) { if (root != null) { inorder_print(root.left); printNode(root); inorder_print(root.right); } “Visit” the node CSC172, Fall 2017

39 In Picture 5 3 4 2 4 8 7 3 9 1 5 8 1 9 2 7 CSC172, Fall 2017

40 Run Time nl = # of nodes on the left sub-tree
Suppose “visit” takes O(1)-time, say c sec nl = # of nodes on the left sub-tree nr = # of nodes on the right sub-tree Note: n - 1 = nl + nr T(n) = T(nl) + T(nr) + c Induction: T(n) ≤ cn, i.e. T(n) = O(n) T(n) ≤ cnl + cnr + c = c(n-1) + c = cn CSC172, Fall 2017

41 Reverse Inorder Traversal
RevInorder-Traverse(root.right) Visit(root) RevInorrder-Traverse(root.left) The (right, node, left) order CSC172, Fall 2017

42 The other 4 traversal orders
Preorder: (node, left, right) Reverse preorder: (node, right, left) Postorder: (left, right, node) Reverse postorder: (right, left, node) We’ll talk about level-order later CSC172, Fall 2017

43 What is the preorder output for this tree?
5 2 4 3 9 8 1 5 4 3 8 7 1 2 9 7 CSC172, Fall 2017

44 What is the postorder output for this tree?
5 2 4 3 9 8 1 3 7 8 1 4 9 2 5 7 CSC172, Fall 2017

45 Questions to Ponder void inorder_print(BTNode root) {
if (root != NULL) { inorder_print(root.left); printNode(root); inorder_print(root.right); } Write the above routine without the recursive calls? Use a stack Don’t use a stack CSC172, Fall 2017

46 Reconstruct the tree from inorder+postorder
3 4 8 7 1 5 9 2 Preorder 5 4 3 8 7 1 2 9 5 CSC172, Fall 2017

47 Questions to Ponder Can you reconstruct the tree given its postorder and preorder sequences? How about inorder and reverse postorder? How about other pairs of orders? How many trees are there which have the same in/post/pre-order sequence? (suppose keys are distinct) CSC172, Fall 2017

48 Number of trees with a given inorder sequence
Catalan numbers CSC172, Fall 2017

49 What is a traversal order good for?
Many things E.g., Evaluate(root) of an expression tree If root is an operand, return the operand Else A = Evaluate(root.left) B = Evaluate(root.right) Return A root.key B root.key is one of the operator What traversal order is the above? CSC172, Fall 2017

50 Level-Order Traversal
5 2 4 3 9 8 1 5 4 2 3 9 8 1 7 7 CSC172, Fall 2017

51 How to do level-order traversal?
5 5 2 2 4 4 3 3 9 9 8 8 1 1 A (FIFO) Queue 7 7 CSC172, Fall 2017

52 Level-Order Print in Java
void levelorder_print(BTNode root) { // Implement } CSC172, Fall 2017

53 Acknowledgement A lot of the slides are taken from various sources including the Internet, Dr. Hung Ngo’ slides, and various other sources. CSC172, Fall 2017


Download ppt "CSC 172– Data Structures and Algorithms"

Similar presentations


Ads by Google