CSC 172– Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Starting at Binary Trees
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
Data Structure By Amee Trivedi.
CS 201 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Expression Tree The inner nodes contain operators while leaf nodes contain operands. a c + b g * d e f Start of lecture 25.
Tree.
Data Structures Binary Trees 1.
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Week 6 - Wednesday CS221.
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
CMSC 341 Introduction to Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
TREES General trees Binary trees Binary search trees AVL trees
Binary Trees.
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Find in a linked list? first last 7  4  3  8 NULL
Binary Tree Traversal Methods
Data Structures and Algorithms for Information Processing
Binary Trees.
Trees CMSC 202, Version 5/02.
Data Structures – Week #5
Binary Tree Traversal Methods
CMSC 202 Trees.
Design and Analysis of Algorithms
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Binary Trees.
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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

Announcement Sorting dance Extra credit opportunity: https://www.youtube.com/watch?v=CjozYYZppqM (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

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

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

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

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

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. 4 3 2 + * 6 3 – 5 * 3 / - CSC172, Fall 2017

How to construct Expression Trees? 4 3 2 + * 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

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

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

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

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

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

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

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

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

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

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

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

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

English Character Frequencies CSC172, Fall 2017

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

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

Why only Binary tree? CSC172, Fall 2017

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

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

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

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

FULL vs. COMPLETE BINARY TREE CSC172, Fall 2017

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

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

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

Let’s see the examples again CSC172, Fall 2017

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

Tree using java CSC172, Fall 2017

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

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

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

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

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

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

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

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

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

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

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

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

Number of trees with a given inorder sequence Catalan numbers https://en.wikipedia.org/wiki/Catalan_number CSC172, Fall 2017

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

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

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

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

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