1 / 61

Trees Chapter 8: Tree Traversals and Tree Terminology

Learn how to represent hierarchical information using trees, use recursion to process trees, and understand different ways of traversing a tree. Also, learn about binary trees, binary search trees, and heaps.

Download Presentation

Trees Chapter 8: Tree Traversals and Tree Terminology

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees Chapter 8

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

  3. 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 Chapter 8: Trees

  4. 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 Chapter 8: Trees

  5. 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 Chapter 8: Trees

  6. 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) Chapter 8: Trees

  7. 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, TL and TR, where TL and TR are binary trees • Note that this is a recursive definition Chapter 8: Trees

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

  9. Examples of Binary Trees Chapter 8: Trees

  10. Huffman Tree Decoding • For example, to convert these bits to letters… 10001010011110101010100010101110100011 g o _ e a g l e s Chapter 8: Trees

  11. 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 Chapter 8: Trees

  12. 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 Chapter 8: Trees

  13. 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 Chapter 8: Trees

  14. Tree Traversals • Recursive definitions of tree traversal • Preorder: Visit root node, traverse TL, traverse TR • Inorder: Traverse TL, visit root node, traverse TR • Postorder: Traverse TL, Traverse TR, visit root node Chapter 8: Trees

  15. 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 Chapter 8: Trees

  16. 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 Chapter 8: Trees

  17. Preorder Traversal • Mouse makes Euler tour and… • Record each node as mouse first encounters it a b d g e h c f i j Chapter 8: Trees

  18. Inorder Traversal • Mouse makes Euler tour and… • Record each node when return from its left subtree d g b h e a i f j c Chapter 8: Trees

  19. Postorder Traversal • Mouse makes Euler tour and… • Record each node as the mouse last encounters it g d h e b i j f c a Chapter 8: Trees

  20. 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 / * Chapter 8: Trees

  21. Node<E> 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 Chapter 8: Trees

  22. BinaryTree<E> Class Chapter 8: Trees

  23. BinaryTree<E> Class Chapter 8: Trees

  24. Binary Search Tree Definition • A set of nodes T is a binary search tree (BST) if either • T is empty, or • 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 Chapter 8: Trees

  25. 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. . . . Chapter 8: Trees

  26. Binary Search Tree Example Chapter 8: Trees

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

  28. Searching a Binary Search Tree • Search for “kept” Chapter 8: Trees

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

  30. Class TreeSet and Interface Search Tree Chapter 8: Trees

  31. BinarySearchTree Class Chapter 8: Trees

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

  33. Insertion into BST Chapter 8: Trees

  34. Insertion into BST publicboolean add(E item) { root = add(root, item); return addReturn; } Chapter 8: Trees

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

  36. 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… Chapter 8: Trees

  37. Removing from BST • Suppose we want to remove “penguin” Chapter 8: Trees

  38. Removing from BST • Suppose we want to remove “kept” Chapter 8: Trees

  39. Removing from BST • Suppose we want to remove “is” Chapter 8: Trees

  40. Removing from BST • Suppose we want to remove “house” Chapter 8: Trees

  41. 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… Chapter 8: Trees

  42. Removing from BST • Suppose we want to remove “house” Chapter 8: Trees

  43. Removing from BST • Suppose we want to remove “rat” Chapter 8: Trees

  44. 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 Chapter 8: Trees

  45. Removing from BST Chapter 8: Trees

  46. Removing from BST Chapter 8: Trees

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

  48. Inserting an Item into a Heap Chapter 8: Trees

  49. Removing an Item from a Heap Chapter 8: Trees

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

More Related