Download presentation

Presentation is loading. Please wait.

1
ITEC200 – Week08 Trees

2
www.ics.mq.edu.au/ppdp 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as depth, completeness, leaf, etc Implement various algorithms to traverse trees Analyse and augment implementations of trees Describe, analyse and augment implementations of binary search trees Describe, analyse and augment implementations of Heaps and Priority Queues Explain and interpret Huffman trees

3
www.ics.mq.edu.au/ppdp 3 Tree Terminology A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the top of a tree is called its root The links from a node to its successors are called branches The successors of a node are called its children The predecessor of a node is called its parent Animal Mammal Dog Poodle Fish SharkHorse

4
www.ics.mq.edu.au/ppdp 4 Tree Terminology (continued) Each node in a tree has exactly one parent except for the root node, which has no parent Nodes that have the same parent are siblings A node that has no children is called a leaf node A generalization of the parent-child relationship is the ancestor-descendent relationship A subtree of a node is a tree whose root is a child of that node The level of a node is a measure of its distance from the root Animal Mammal Dog Poodle Fish SharkHorse

5
www.ics.mq.edu.au/ppdp 5 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the following is true –T is empty –Its root node has two subtrees, TL and TR, such that TL and TR are binary trees

6
www.ics.mq.edu.au/ppdp 6 Fullness and Completeness Trees grow from the top down Each new value is inserted in a new leaf node A binary tree is full if every node has two children except for the leaves A binary tree of height h is complete if it is full to level h – 1 and all unfilled nodes are on the right

7
www.ics.mq.edu.au/ppdp 7 Tree Traversals Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

8
www.ics.mq.edu.au/ppdp 8 Visualizing Tree Traversals Preorder traversal if we record each node as we first encounter it Inorder traversal if we record each node as we return from traversing its left subtree Postorder traversal if we record each node as we last encounter it

9
www.ics.mq.edu.au/ppdp 9 Application: Traversals of Expression Trees An inorder traversal of an expression tree inserts parenthesis where they belong (infix form) A postorder traversal of an expression tree results in postfix form

10
www.ics.mq.edu.au/ppdp 10 Binary Search Trees Binary search trees: All elements in the left subtree precede those in the right subtree

11
www.ics.mq.edu.au/ppdp 11 Binary Search Trees (cont) Binary search tree definition: A set of nodes T is a binary search tree if either of the following is true: * T is empty * Its root has two subtrees such that each is a binary search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value

12
www.ics.mq.edu.au/ppdp 12 Implementing Binary Search Trees BinaryTree consists of a node, and a left and right sub-tree Just as for a linked list, a node consists of a data part and links to successor nodes The data part is a reference to type E A binary tree node must have links to both its left and right subtrees

13
www.ics.mq.edu.au/ppdp 13 The BinaryTree Class

14
www.ics.mq.edu.au/ppdp 14 The BinaryTree Class (continued)

15
www.ics.mq.edu.au/ppdp 15 Searching a Binary Tree

16
www.ics.mq.edu.au/ppdp 16 Class TreeSet and Interface SearchTree

17
www.ics.mq.edu.au/ppdp 17 BinarySearchTree Class

18
www.ics.mq.edu.au/ppdp 18 Insertion into a Binary Search Tree

19
www.ics.mq.edu.au/ppdp 19 Removing from a Binary Search Tree

20
www.ics.mq.edu.au/ppdp 20 Removing from a Binary Search Tree (continued)

21
www.ics.mq.edu.au/ppdp 21 Heaps and Priority Queues In a heap, the value in a node is les than all values in its two subtrees A heap is a complete binary tree with the following properties –The value in the root is the smallest item in the tree –Every subtree is a heap

22
www.ics.mq.edu.au/ppdp 22 Inserting an Item into a Heap

23
www.ics.mq.edu.au/ppdp 23 Removing an Item from a Heap

24
www.ics.mq.edu.au/ppdp 24 Implementing a Heap Because a heap is a complete binary tree, it can be implemented efficiently using an array instead of a linked data structure First element for storing a reference to the root data Use next two elements for storing the two children of the root Use elements with subscripts 3, 4, 5, and 6 for storing the four children of these two nodes and so on

25
www.ics.mq.edu.au/ppdp 25 Priority Queues The heap is used to implement a special kind of queue called a priority queue A priority queue is a data structure in which only the highest-priority item is accessible

26
www.ics.mq.edu.au/ppdp 26 The PriorityQueue Class Java provides a PriorityQueue class that implements the Queue interface given in Chapter 6. Peek, poll, and remove methods return the smallest item in the queue rather than the oldest item in the queue.

27
www.ics.mq.edu.au/ppdp 27 Huffman Trees A Huffman tree can be implemented using a binary tree and a PriorityQueue

28
www.ics.mq.edu.au/ppdp 28 Where to from here… Work through Chapter 8 of the Koffman & Wolfgang Text Conceptual Questions and Practical Exercises Submit all preliminary work Be prompt for your online class

29
www.ics.mq.edu.au/ppdp 29 Acknowledgements These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 8 PowerPoint presentation by Elliot B. Koffman and Paul A. T. Wolfgang

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google