1 / 20

Trees

Trees. Trees. Set of elements or nodes one node as root every other node has a single predecessor (parent) and zero or more successors (children) nodes without successors are called leaves Nodes are connected by edges The depth of a node is the number of edges on the path to the root

larryyoung
Download Presentation

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

  2. Trees • Set of elements or nodes • one node as root • every other node has a single predecessor (parent) and zero or more successors (children) • nodes without successors are called leaves • Nodes are connected by edges • The depth of a node is the number of edges on the path to the root • The height of the tree is the number of edges on the longest path from root to any leaf • A complete tree has all leaves at same depth, all other nodes have the same number of children

  3. Example root height of tree = 3  edge leaf leaf leaf leaf leaf leaf leaf leaf

  4. Questions • What is the height of the tree? • What is the depth of node n? • What is the depth of node p? • Is the tree complete? n p

  5. Questions • What is the height of the tree? • What is the depth of node n? • What is the depth of node p? • What is the depth of node q? p q n

  6. Subtrees • The subtree rooted at x is the tree of all descendants of x that has node x as root q p subtree rooted at q subtree rooted at p

  7. Binary trees • A binary tree is a structure defined on a finite set of nodes that either • contains no nodes • is composed of three disjoint sets of nodes • a root node • a binary tree called its left subtree • a binary tree called its right subtree

  8. Left and right subtrees • In a binary tree • the left subtree of x is the tree rooted at the left child of x • the right subtree of x is the tree rooted at the right child of x x leftsubtree rightsubtree

  9. Binary trees p q w x y z left subtree of root right subtree of root

  10. Binary trees p q w x y z right subtreeof p right subtreeof q left subtreeof p left subtreeof q subtree rooted at p subtree rooted at q

  11. Binary trees p q w x y z

  12. Questions • In a complete binary tree of height 3, • how many nodes are in the tree? • how many leaves are in the tree? • In a complete binary tree of height 4, • how many nodes are in the tree? • how many leaves are in the tree? • How many nodes in a complete binary tree of height h? • How many leaves in a complete binary tree of height h? • How many nodes in a binary tree of height h? • How many leaves in a binary tree of height h?

  13. Complete binary tree of height 3 • 15 nodes • 8 leaves

  14. Complete binary tree of height 4 • 31 nodes • 16 leaves

  15. Binary trees In a binary tree of height h • the total number of nodes is at most 20 +21 +…+2h = 2h+1 –1 • the number of leaf nodes is at most 2h If the tree is complete • there are exactly 2h+1 – 1 nodes • there are exactly 2h leaves binary tree completebinary tree

  16. Decision trees • A decision tree begins with a decision you need to make • start with an initial decision node • ask a question • Structure for investigating options and the possible outcomes of choosing those options • result of a decision can be another decision • outcome is a terminal node • Tree should have unique paths from the decision node to each of the terminal nodes. • help you to choose between several courses of action

  17. A decision tree He received the Physics Prize in 1921. yes Would you like to read about Einstein? Try the Medicine Prize in 1962. yes no Would you like to read about a scientist? Look up the Peace Prize in 1991. yes no Would you prefer a humanitarian? Try the Prize in Literature, 1970. no

  18. A decision tree for insertion sort a b? sort three elements a,b,c yes no b c? a c? yes no yes no a c? b c? a  b  c b  a  c yes no yes no c  a  b b  c  a a  c  b c  b  a

  19. Insertion sort of 4 elements a  b? sort four elements a,b,c,d yes no best casepath a  c? b  c? no yes b  c? c  d? … worst casepath no a  d? a  b  c  d … … no b  d? no … c  d? d  c  b  a

  20. Decision-tree model • Decision trees are used to model comparison sorts • insertion sort, mergesort, quicksort • Each possible permutation of n elements must appear as a leaf in the tree • at least n! leaves • Worst-case number of comparisons = length of longest path from root to leaf • Insertion sort worst case: • number of comparisons: 0+1+ … + n-1 = ½ (n2-n) • Length of longest path in decision tree for 4 elements = ½ (42-4) = 6

More Related