Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Trees Chapter 8

2 Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the difference between binary trees, binary search trees, and heaps To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays

3 Chapter 8: Trees3 Chapter Objectives To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced storage requirements

4 Chapter 8: Trees4 Tree Terminology A tree consists of a collection of elements or nodes Each node linked to its successors The node at the top of a tree is the root Links from a node to its successors are branches The successors of a node are its children The predecessor of a node is its parent

5 Chapter 8: Trees5 Tree Terminology 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 a leaf node A generalization of the parent-child relationship is the ancestor-descendent relationship

6 Chapter 8: Trees6 Tree Terminology 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 The level of a node is also known as its depth Since trees are usually written with root at the top Root is at level 1 (or depth 1)

7 Chapter 8: Trees7 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either T is empty, or The root node has two subtrees, T L and T R, where T L and T R are binary trees Note that this is a recursive definition

8 Chapter 8: Trees8 Some Types of Binary Trees Expression tree Each node contains an operator or an operand Recall infix and postfix notation Huffman tree Represents “Huffman codes” for characters Huffman code uses variable number of bits to encode letters (ASCII has fixed number of bits per character) Huffman coding is more efficient Binary search trees Elements in left subtree precede those in right subtree

9 Chapter 8: Trees9 Examples of Binary Trees

10 Chapter 8: Trees10 Huffman Tree Decoding For example, to convert these bits to letters… 10001010011110101010100010101110100011 go_eagles

11 Chapter 8: Trees11 Fullness and Completeness Trees grow from the top down Recall that the top node is the root Each new value is inserted as a new leaf node A binary tree is full if every node has two children… …except for the leaves

12 Chapter 8: Trees12 General Trees In general, a tree can have any number of subtrees Binary tree is a special case A general tree can be represented by a binary tree

13 Chapter 8: Trees13 Tree Traversals We want to determine nodes of tree and relationship Do this by walking thru tree in some prescribed order Visit each node as it is encountered This process is called tree traversal Three kinds of tree traversal Inorder Preorder Postorder

14 Chapter 8: Trees14 Tree Traversals Recursive definitions of tree traversal Preorder: Visit root node, traverse T L, traverse T R Inorder: Traverse T L, visit root node, traverse T R Postorder: Traverse T L, Traverse T R, visit root node

15 Chapter 8: Trees15 Visualizing Tree Traversals Can visualize tree traversal by imagining a mouse (not the computer kind) walking along the edge of the tree If mouse always keeps tree to his left, he traces a route known as the Euler tour

16 Chapter 8: Trees16 Euler Tour and Tree Traversals Assuming mouse makes an Euler Tour Preorder traversal is obtained if we… Record each node as the mouse first encounters it Inorder traversal is obtained if we… Record each node as mouse returns from traversing its left subtree Postorder traversal is obtained if we… Record each node as the mouse last encounters it

17 Chapter 8: Trees17 Preorder Traversal Mouse makes Euler tour and… Record each node as mouse first encounters it abdgehcfij

18 Chapter 8: Trees18 Inorder Traversal Mouse makes Euler tour and… Record each node when return from its left subtree dgbheaifjc

19 Chapter 8: Trees19 Postorder Traversal Mouse makes Euler tour and… Record each node as the mouse last encounters it gdhebijfca

20 Chapter 8: Trees20 Prefix, Infix, and Postfix Consider this tree: Preorder traversal results in prefix form * + x y / + a b c Inorder traversal gives infix form of expression x + y * a + b / c Postorder traversal results in postfix form x y + a b + c / *

21 Chapter 8: Trees21 Node Class 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

22 Chapter 8: Trees22 BinaryTree Class

23 Chapter 8: Trees23 BinaryTree Class

24 Chapter 8: Trees24 Binary Search Tree Definition A set of nodes T is a binary search tree (BST) if either 1.T is empty, or 2.The root of T has two subtrees, each of which is a binary search tree, and the value of the root is greater than all values of the left subtree and less than all values in the right subtree

25 Chapter 8: Trees25 The House That Jack Built This is the house that Jack built. This is the malt That lay in the house that Jack built. This is the rat, That ate the malt That lay in the house that Jack built..

26 Chapter 8: Trees26 Binary Search Tree Example

27 Chapter 8: Trees27 Searching a Binary Search Tree Suppose we search for “kept”

28 Chapter 8: Trees28 Searching a Binary Search Tree Search for “kept”

29 Chapter 8: Trees29 Searching a Binary Search Tree Suppose we search for “penguin”

30 Chapter 8: Trees30 Class TreeSet and Interface Search Tree

31 Chapter 8: Trees31 BinarySearchTree Class

32 Chapter 8: Trees32 BST Find Method private E find(Node localRoot, E target) { if (localRoot == null) return null; int compResult = target.compareTo(localRoot.data); if (compResult == 0) return localRoot.data; else if (compResult < 0) return find(localRoot.left, target); else return find(localRoot.right, target); }

33 Chapter 8: Trees33 Insertion into BST

34 Chapter 8: Trees34 Insertion into BST public boolean add(E item) { root = add(root, item); return addReturn; }

35 Chapter 8: Trees35 Insertion into BST private Node add(Node localRoot, E item) { if (localRoot == null) { addReturn = true; return new Node (item); } else if (item.compareTo(localRoot.data) == 0) { addReturn = false; return localRoot; } else if (item.compareTo(localRoot.data) < 0) { localRoot.left = add(localRoot.left, item); return localRoot; } else { localRoot.right = add(localRoot.right, item); return localRoot; }

36 Chapter 8: Trees36 Removing from BST Several cases to consider… If target is not found in BST Cannot remove it! If target is a leaf node in BST Remove target leaf node (easy) If target is not a leaf node in BST, but only has one child Remove target and child takes place of parent If target is not a leaf node in BST, and has 2 children Remove target and find replacement parent…

37 Chapter 8: Trees37 Removing from BST Suppose we want to remove “penguin”

38 Chapter 8: Trees38 Removing from BST Suppose we want to remove “kept”

39 Chapter 8: Trees39 Removing from BST Suppose we want to remove “is”

40 Chapter 8: Trees40 Removing from BST Suppose we want to remove “house”

41 Chapter 8: Trees41 Removing from BST Now what? What node should be parent to “cow” and “house” From definition of BST… …value of the root is greater than all values of the left subtree and less than all values in the right subtree…

42 Chapter 8: Trees42 Removing from BST Suppose we want to remove “house”

43 Chapter 8: Trees43 Removing from BST Suppose we want to remove “rat”

44 Chapter 8: Trees44 Removing from BST Largest item in left subtree is known as… …inorder predecessor Smallest item in right subtree is known as… …inorder successor Either of these will work as replacement parent Algorithm in book uses inorder predecessor

45 Chapter 8: Trees45 Removing from BST

46 Chapter 8: Trees46 Removing from BST

47 Chapter 8: Trees47 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

48 Chapter 8: Trees48 Inserting an Item into a Heap

49 Chapter 8: Trees49 Removing an Item from a Heap

50 Chapter 8: Trees50 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

51 Chapter 8: Trees51 Inserting into a Heap Implemented as an ArrayList

52 Chapter 8: Trees52 Inserting into a Heap Implemented as an ArrayList (continued)

53 Chapter 8: Trees53 Priority Queues 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

54 Chapter 8: Trees54 Insertion into a Priority Queue

55 Chapter 8: Trees55 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.

56 Chapter 8: Trees56 Design of a KWPriorityQueue Class

57 Chapter 8: Trees57 Huffman Trees 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

58 Chapter 8: Trees58 Huffman Trees (continued)

59 Chapter 8: Trees59 Huffman Trees (continued)

60 Chapter 8: Trees60 Chapter Review A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node

61 Chapter 8: Trees61 Chapter Review (continued) A heap is a complete binary tree in which the data in each node is less than the data in both its subtrees Insertion and removal in a heap are both O(log n) A Huffman tree is a binary tree used to store a code that facilitates file compression


Download ppt "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."

Similar presentations


Ads by Google