1 / 26

Overview of Binary Trees: Properties, Methods, and Representations

This chapter provides an overview of binary trees, including their properties, methods, and various representations. It discusses ordered and proper binary trees, accessor and query methods, tree ADT exceptions, and applications of binary trees.

marshak
Download Presentation

Overview of Binary Trees: Properties, Methods, and Representations

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. CPCS 204: Data Structures & AlgorithmsChapter 7 Trees Instructor: Nouf Ghanimi

  2. overview 7.3 Binary Trees

  3. Ordered Trees • A tree is ordered if there is a linear ordering defined for the children of each node; • That’s, we can identify the children of a node as being the first, second, third, and so on. • Such an ordering is usually shown by arranging siblings left to right, according to their ordering. • Ordered trees typically indicate the linear order among siblings by listing them in the correct order. • A famous example of ordered trees is the family tree.

  4. Binary Trees A binary tree is an ordered tree with the following properties: Every node has at most two children (called proper if 2 or 0 children). Each child node is labeled as being either a left child or a right child. A left child precedes a right child in the ordering of children of a node.

  5. Methods of a Tree ADT • Accessor Methods We use positions to abstract nodes.The real power of node positions in a tree comes from the accessor methods of the tree ADT that return and accept positions, such as the following: • root(): Return the position of the tree’sroot; an error occurs if the tree is empty. • parent(p): Return the position of the parent of p; an error occurs if p is the root. • children(p): Return an iterable collection containing the children of node p.

  6. Methods of a Tree ADT (Cont.) • Generic methods • size(): Return the number of nodes in the tree. • isEmpty(): Test whether the tree has any nodes or not. • Iterator(): Return an iterator of all the elements stored at nodes of the tree. • positions(): Return an iterable collection of all the nodes of the tree.

  7. Methods of a Tree ADT (Cont.) 3. Query methods In addition to the above fundamental accessor methods, the tree ADT also supports the following Boolean query methods: • isInternal(p): Test whether node p is an internal node • isExternal(p): Test whether node p is an external node • isRoot(p): Test whether node p is the root node These methods make programming with treeeasier and more readable, since we can use them in the conditionals of if -statements and while -loops, rather than using a non-intuitive conditional.

  8. Methods of a Tree ADT (Cont.) • Update Method The tree ADT also supports the following update method: • replace(p, e): Replace with e and return the element stored at nodep.

  9. Tree ADT Exceptions An interface for the tree ADT uses the following exceptions to indicate error conditions: • InvalidPositionException: This error condition may be thrown by any method taking a position as an argument to indicate that the position is invalid. • BoundaryViolationException: This error condition may be thrown by method parent() if it’s called on the root. • EmptyTreeException: This error condition may be thrown by method root() if it’s called on an empty tree.

  10. The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position left(p) position right(p) boolean hasLeft(p) boolean hasRight(p) BinaryTree ADT

  11. Binary Tree representation Linked list Array

  12. D B A C E Linked Structure for Binary Trees • A node is represented by an object storing • Element • Parent node • Left child node • Right child node • Node objects implement the Position ADT       

  13. Array-Based Representation of Binary Trees • nodes are stored in an array • let rank(node) be defined as follows: • rank(root) = 1 • if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) • if node is the right child of parent(node), rank(node) = 2*rank(parent(node))+1

  14. b Array Representation 1 a 2 3 c 4 5 6 7 d f e g 8 9 10 11 h i j k tree[] a b c d e f g h i j k 0 5 10

  15. Binary Tree Applications decision processes arithmetic expressions searching

  16. decision processes Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: Want a fast meal? No Yes How about coffee? On expense account? Yes No Yes No Starbucks Spike’s Al Forno Café Paragon

  17. +   2 - 3 b a 1 arithmetic expressions Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: (2  (a - 1) + (3  b))

  18. Properties of Proper Binary Trees • Notation n number of nodes e number of external nodes i number of internal nodes h height1 • Properties: • e = i +1 • n =2e -1 • h  i • h  (n -1)/2 • e 2h • h log2e • h log2 (n +1)-1

  19. Minimum Number Of Nodes • Minimum number of nodes in a binary tree whose height is h => h+1. • At least one node at each of d levels. minimum number of nodes is h+1

  20. Maximum Number Of Nodes Maximum number of nodes = 1 + 2 + 4 + 8 + … + 2h n <= 2h+1 - 1

  21. Height 3 full binary tree. Full Binary Tree • A full binary tree of a given height h has 2h+1 – 1 nodes.

  22. Internal Nodes & External Nodes in a Proper Binary Tree e = i + 1 Case 1: If T has only one node v . Case 2: Otherwise (T has more than one node).

  23. Binary Tree traversal Preorder Postorder Inorder Euler Tour

  24. +   2 - 3 2 5 1 Evaluate Arithmetic Expressions • Specialization of a postorder traversal • recursive method returning the value of a subtree • when visiting an internal node, combine the values of the subtrees AlgorithmevalExpr(v) ifisExternal (v) returnv.element () else x evalExpr(leftChild (v)) y evalExpr(rightChild (v))  operator stored at v returnx  y

  25. Euler Tour Traversal • Visiting the node more than one time. • Generic traversal of a binary tree • Includes a special cases the preorder, postorder and inorder traversals • Walk around the tree and visit each node three times: • on the left (preorder) • from below (inorder) • on the right (postorder) +   L R B 2 - 3 2 5 1

  26. +   2 - 3 b a 1 Print Arithmetic Expressions Algorithm printExpression(T,v) if T.isInternal (v) then print "(" if T.hasLeft (v) then printExpression (T,T.left(v)) if T.isInternal (v) then print the operator stored at v else print the value stored at v if T.hasRight (v) then printExpression (T,T.right(v)) if T.isInternal (v) then print ")" ((2  (a - 1)) + (3  b))

More Related