Download presentation

Presentation is loading. Please wait.

Published byPamela Malone Modified over 5 years ago

1
Trees

2
Tree Terminology Chapter 8: Trees 2 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

3
Tree Terminology (continued) Chapter 8: Trees 3 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

4
Tree Terminology (continued) Chapter 8: Trees 4 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

5
Binary Trees Chapter 8: Trees 5 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
Some Types of Binary Trees Chapter 8: Trees 6 Expression tree Each node contains an operator or an operand Huffman tree Represents Huffman codes for characters that might appear in a text file Huffman code uses different numbers of bits to encode letters as opposed to ASCII or Unicode Binary search trees All elements in the left subtree precede those in the right subtree

7
Some Types of Binary Trees (continued) Chapter 8: Trees 7

8
Fullness and Completeness Chapter 8: Trees 8 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

9
General Trees Chapter 8: Trees 9 Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

10
Tree Traversals Chapter 8: Trees 10 Often we want to determine the nodes of a tree and their relationship Can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three kinds of tree traversal Inorder Preorder Postorder

11
Tree Traversals (continued) Chapter 8: Trees 11 Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

12
Visualizing Tree Traversals Chapter 8: Trees 12 You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour Preorder traversal if we record each node as the mouse first encounters it Inorder if each node is recorded as the mouse returns from traversing its left subtree Postorder if we record each node as the mouse last encounters it

13
Traversals of Binary Search Trees and Expression Trees Chapter 8: Trees 13 An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value 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

14
The Node Class Chapter 8: Trees 14 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 Object A binary tree node must have links to both its left and right subtrees

15
The BinaryTree Class Chapter 8: Trees 15

16
The BinaryTree Class (continued) Chapter 8: Trees 16

17
Overview of a Binary Search Tree Chapter 8: Trees 17 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

18
Searching a Binary Tree Chapter 8: Trees 18

19
Class TreeSet and Interface Search Tree Chapter 8: Trees 19

20
BinarySearchTree Class Chapter 8: Trees 20

21
Insertion into a Binary Search Tree Chapter 8: Trees 21

22
Removing from a Binary Search Tree Chapter 8: Trees 22

23
Removing from a Binary Search Tree (continued) Chapter 8: Trees 23

24
Heaps and Priority Queues Chapter 8: Trees 24 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

25
Removing an Item from a Heap Chapter 8: Trees 25

26
Implementing a Heap Chapter 8: Trees 26 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

27
Inserting into a Heap Implemented as an ArrayList Chapter 8: Trees 27

28
Inserting into a Heap Implemented as an ArrayList (continued) Chapter 8: Trees 28

29
Priority Queues Chapter 8: Trees 29 The heap is used to implement a special kind of queue called a priority queue The heap is not very useful as an ADT on its own Will not create a Heap interface or code a class that implements it Will incorporate its algorithms when we implement a priority queue class and Heapsort Sometimes a FIFO queue may not be the best way to implement a waiting line A priority queue is a data structure in which only the highest-priority item is accessible

30
Insertion into a Priority Queue Chapter 8: Trees 30

31
The PriorityQueue Interface Chapter 8: Trees 31 Effectively the same as the Queue interface provided in chapter six

32
Design of a HeapPriorityQueue Class Chapter 8: Trees 32

33
HeapPQwithComparator Chapter 8: Trees 33

34
Huffman Trees Chapter 8: Trees 34 A Huffman tree can be implemented using a binary tree and a PriorityQueue A straight binary encoding of an alphabet assigns a unique binary number to each symbol in the alphabet Unicode for example The message “go eagles” requires 144 bits in Unicode but only 38 using Huffman coding

35
Huffman Trees (continued) Chapter 8: Trees 35

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