Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COMP 103 Introduction to Trees RECAP-TODAY 2 RECAP Linked List, Linked Stack, Linked Queue Other linked collections TODAY Introduction to Trees Reading: Chapter 16 in textbook Other linked collections 3 LinkedStack and LinkedQueue All the key operations are O(1) Set Can store sorted or unsorted What is the cost of add, remove, contains, size, equals in each case? Bag Same as set, but add a count to each node. Map Same as set, but store value as well as key in each node Or store pointed to a key-value pair What is the cost of get, put, remove? Linear vs. Hierarchical Structures 4 So far we’ve looked at unordered structures and linear structures: Unordered structures have no structural order between items: Linear structures have items arranged in order one after another Set, Bag, Map List, Stack, Queue… Some data have a hierarchical or ‘tree-like’ structure Hierarchical structure have items arranged in levels, with items ‘above’ or ‘below’ the other, such as Family tree Organisational Charts Taxonomies Filing systems (paper-based, computer-based) Models of shapes for computer graphics Programs/decision processes…. And so on…. Trees 5 We're going to look into the data structure and the crucial algorithms for trees Applications of tree structures: Representing data with a natural hierarchical structure Using trees as data structures to implement other collections: sets, maps and priority queues (and another sorting algorithm!) Note: some data has an even more complex network-like structure: • Communications networks • Geographical maps (real world, or in computer games) Family Trees: ancestral tree 6 seniority Justine Jeremy Julie Julia John Jules James Jada Jasmine Jesse Jacob Jenny Jordan Jackie Jesse Jack Jenny Jenna Jean Organisational hierarchy 7 seniority Jane Jeremy John Julia James Jasmine Jada Julie Jules Jacob Jordan Jenny Jesse Jared Justin Jake Jenna Jacky Juan Joseph Taxonomy 8 Animals Reptiles Mammals Primates Tiger Felines Leopard Canines Lion Cat Turtles Snakes Lizards Program Structure 9 private void reportMachineQueues(int tick) { System.out.printf("%4d: ", tick); for (Queue<Job> queue : machineQueues) { if (queue.isEmpty()) System.out.print(" - "); else System.out.printf("%3s ", queue.peek().getID()); } System.out.println(); if (verbose) for (int m=0; m<NumberOfMachines; m++) { Queue<Job> queue = machineQueues.get(m); System.out.printf(" M%d: %d jobs: ", m, queue.size()); boolean first = true; for (Job job : queue) { if (first) { System.out.print(job); first=false; } else System.out.printf("\n %s", job); } System.out.println(); } } Tracing program execution 10 archWall(10, 300, 80, 40) drawArch aw(10, 220, 40, 20) drawArch drawArch aw(10,180,20,10) drawArch aw(50,220,40,20) aw(30,180,20,10) drawArch aw(50,180,20,10) drawArch aw(70,180,20,10) drawArch Noughts and Crosses (Tic Tac Toe) 11 X X X X O X OX X X O X O X O Decision Tree 12 Tree Terminology 13 A tree is a collection of items in a strict hierarchical structure. Each item is in a node of the tree. The root is at the top (!) Children may be ordered/unordered The leaves are at the bottom. Each node may have child nodes – except a leaf, which has none. Each node has one parent - except the root, which has none. An edge joins a node to its parent – may be labelled. A subtree is a node plus all its descendents. The depth of a node is its distance from the root. The height or depth of a tree is the depth of the lowest leaf. Unique!! Level = set/list of nodes at the same depth. Branch = sequence of nodes on a path from root to a leaf. This is more correctly called a rooted, directed, labelled tree. Components of Trees 14 A C B E K F G J M Node, root, leaf, child, parent, edge, subtree, depth, height, level, branch, siblings, ancestors, descendants, cousins, … Common Constraints on Trees 15 Number of children per node (“arity”) Ordering constraints: 2: Binary tree What is a unary tree? 3: Ternary tree N: N-ary tree (5-ary, 100-ary, …) Strict N-ary: every non-leaf has exactly N children; otherwise at most N General Tree: No limit on number of children Children of a node ordered (List of children). Children of a node unordered (Set/Bag of children). Item in each node is less than items in child nodes. Item at each node is smaller that all items in its left subtree and greater than all items in its right subtree. Balancing: Subtrees are the same height (all leaves are on same level) Subtrees heights differ by at most one (leaves within one level of each other) What can you do with a tree? 16 Construct/modify/print it: Search it: Add nodes, remove nodes (where?) Parsing: read a string and turn it into a tree, eg. program, expression Find a node with a given value/property Find a path to (or from) a node Compute some property of the tree or its data: Find the height, width, arity, … Sum, max, average, … of values stored Find a given person’s mother, or all of their grandchildren Find the manager with the most (direct/indirect) underlings Traversing trees 17 Many tree algorithms involve doing something at each node in a tree Different algorithms require nodes to be visited in different orders Breadth-first/level-order: Visit nodes in order of increasing depth Root, then all nodes with depth 1, all with depth 2, … May be in specified order within each level Depth-first: Visit one child and all of its descendants before its siblings May visit root before, after or between its children May visit children in specified order Trees and Recursion 18 Recursively defined data structure: A tree is a node, along with a set/list of subtrees (empty for leaf) Can a tree be empty? Recursion is very natural for trees – important! If you don’t use recursion you often need to simulate it (later) Binary Trees 19 Each node may have a left child and/or a right child • What’s the maximum number of nodes in a binary tree of height k ? • What’s the minimim height of a binary tree with n nodes? Representing tree data in Java 20 What kind of data structure can we use to represent a tree? G Linked Structures, just like Linked List Nodes: Binary Tree Nodes: General Tree Nodes G K G T K M K Arrays ?! we'll M come to this later on... M Z A