Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Introduction to Trees.

Similar presentations


Presentation on theme: "COMP 103 Introduction to Trees."— Presentation transcript:

1 COMP 103 Introduction to Trees

2 RECAP-TODAY RECAP TODAY Linked List, Linked Stack, Linked Queue
Other linked collections TODAY Introduction to Trees Reading: Chapter 16 in textbook

3 Other linked collections
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?

4 Linear vs. Hierarchical Structures
So far we’ve looked at unordered structures and linear structures: Unordered structures have no structural order between items: Set, Bag, Map Linear structures have items arranged in order one after another 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….

5 Trees 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)

6 Family Trees: ancestral tree
Justine Jeremy Julie John Julia Jesse Jordan Jenna Jack Jacob Jules James Jada Jenny Jackie Jasmine Jean seniority

7 Organisational hierarchy
Jane Jeremy Julie Justin John Jules Jenny Jesse Jordan Jacky Jenna Juan Jacob Julia James Jada Jared Jake Joseph Jasmine seniority

8 Taxonomy Animals Reptiles Mammals Primates Felines Canines Turtles
Snakes Lizards Tiger Leopard Lion Cat

9 Program Structure 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);

10 Tracing program execution
archWall(10, 300, 80, 40) aw(50,220,40,20) drawArch aw(10, 220, 40, 20) drawArch aw(10,180,20,10) aw(30,180,20,10) drawArch aw(50,180,20,10) aw(70,180,20,10) drawArch drawArch drawArch drawArch

11 Noughts and Crosses (Tic Tac Toe)
X X X X X O X O X X X X O O O

12 Decision Tree

13 Tree Terminology 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 (!) 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. 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. Children may be ordered/unordered Unique!!

14 Components of Trees A C B E F G J K M Node, root, leaf, child, parent, edge, subtree, depth, height, level, branch, siblings, ancestors, descendants, cousins, …

15 Common Constraints on Trees
Number of children per node (“arity”) 2: Binary 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 Ordering constraints: 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 is a unary tree?

16 What can you do with a tree?
Construct/modify/print it: Add nodes, remove nodes (where?) Parsing: read a string and turn it into a tree, eg. program, expression Search it: 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

17 Traversing trees 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

18 Trees and Recursion 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)

19 Binary Trees Each node may have a left child and/or a right child Notice that tree size is limited by the depth of the tree. For binary tree, nodes at level k = n, for N-ary tree, k = n ^ (k-1). Size for binary is: … + n = 2^n – 1 and size for N-ary is (n^k-1)/(n-1) where k is number of full levels. 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?

20 Representing tree data in Java
What kind of data structure can we use to represent a tree? Linked Structures, just like Linked List Nodes: Binary Tree Nodes: General Tree Nodes Arrays ?! we'll come to this later on... G T K M Z A G M K G K M


Download ppt "COMP 103 Introduction to Trees."

Similar presentations


Ads by Google