1 / 36

Trees

Trees. Chapter 15. Trees. Lists, stacks, and queues are linear in their organization of data Items are one after another. Trees organize data in a nonlinear, hierarchical form Item can have more than one immediate successor. Terminology. Use trees to represent relationships

Télécharger la présentation

Trees

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 15

  2. Trees • Lists, stacks, and queues are linear in their organization of data • Items are one after another. • Trees organize data in a nonlinear, hierarchical form • Item can have more than one immediate successor

  3. Terminology • Use treesto represent relationships • Diagram represents the recursive calls for the fibonacci sequence as a tree Vertex or node Edges

  4. Terminology • Trees are hierarchical in nature • Means a parent-child relationship between nodes

  5. Terminology • More tree hierarchies

  6. Terminology

  7. Kinds of Trees • General tree • Set T of one or more nodes • T is partitioned into disjoint subsets • Binary tree • Set of T nodes – either empty or partitioned into disjoint subsets • Single node r, the root • Two (possibly empty) sets – left and right subtrees

  8. Kinds of Trees • Binary trees that represent algebraic expressions – different tree traversal algorithms generate infix, prefix or postfix representations of the expression. Review - Can we figure out the traversal strategies?

  9. Kinds of Trees • A binary search tree of names. What do we notice about the relationship of the values stored in the tree? Which tree traversal algorithm will give us the values in sorted order? All nodes in left subtree contain values < parent node. All nodes in right subtree contain values > parent value. Inorder traversal gives sorted order.

  10. The Height of Trees • Level of a node, n • If n is root, level 1 • If n not the root, level is 1 greater than level of its parent • Height of a tree • Number of nodes on longest path from root to a leaf • T empty, height 0 • T not empty, height equal to max level of nodes

  11. The Height of General Trees • Binary trees with the same number of nodes but different heights • These are not binary search trees

  12. Full, Complete, and Balanced Binary Trees • Afull binary tree of height 2 – all parents have two (k) children and all leaves are at the same level.

  13. Full, Complete, and Balanced Binary Trees • A complete binary tree (remember our Heap?) – tree is filled from left to right, only lower two levels of leaves differ in height: leaves at height h and height h-1.

  14. The Maximum and Minimum Heights of a Binary Tree • Binary tree with n nodes • Max height is n • To minimize height of binary tree of n nodes • Fill each level of tree as completely as possible • A complete tree meets this requirement

  15. The Maximum and Minimum Heights of a Binary Tree • Binary trees of height 2 • A binary tree of height h will have a minimum of ____ nodes? • And a maximum of _____ nodes? (Think about a power of 2?)

  16. The Maximum and Minimum Number of Nodes in a Binary Tree • Maximum - Counting the nodes in a full binary tree of height h

  17. The ADT Binary Tree • Operations of ADT binary tree • Add, remove a node • Set, retrieve data • Test for empty • Traversal operations that visit every node • Traversal can visit nodes in several different orders

  18. Traversals of a Binary Tree • Pseudo-code for general form of a recursive traversal algorithm (this is a preorder traversal – root, then left, then right).

  19. Traversals of a Binary Tree • Options for when to visit the root • Preorder: before it traverses both subtrees • Inorder: after it traverses left subtree, before it traverses right subtree • Postorder: after it traverses both subtrees • Note traversal is O(n)

  20. Traversals of a Binary Tree • Three traversals of a binary tree

  21. Traversals of a Binary Tree • Preorder traversal algorithm

  22. Traversals of a Binary Tree • Inorder traversal algorithm

  23. Traversals of a Binary Tree • Postorder traversal algorithm

  24. Binary Tree Operations • UML diagram for the class BinaryTree

  25. Interface Template for the ADT Binary Tree • An interface template for the ADT binary tree

  26. Interface Template for the ADT Binary Tree • An interface template for the ADT binary tree

  27. Interface Template for the ADT Binary Tree • An interface template for the ADT binary tree

  28. Interface Template for the ADT Binary Tree • An interface template for the ADT binary tree

  29. The ADT Binary Search Tree • Recursive definition of a binary search tree • n’s value is greater than all values in its left subtree TL. • n’s value is less than all values in its right subtree TR. • Both TL and TR are binary search trees.

  30. The ADT Binary Search Tree • A binary search tree of names

  31. Searching a Binary Search Tree • Search algorithm for a binary search tree? Lets try to construct the algorithm….

  32. Searching a Binary Search Tree – for Finn • Empty subtree where the search algorithm terminates when looking for Finn

  33. Traversals of a Binary Search Tree • Inorder traversal of a binary search tree visits tree nodes in sorted search-key order

  34. Efficiency of Binary Search Tree Operations • Max number of comparisons for retrieval, addition, or removal • The height of the tree • Adding entries in sorted order • Produces maximum-height binary search tree • Adding entries in random order • Produces near-minimum-height binary search tree

  35. Efficiency of Binary Search Tree Operations • The Big O for the retrieval, addition, removal, and traversal operations of the ADT binary search tree. Worst case occurs when tree is unbalanced and skewed to left or right.

  36. End

More Related