260 likes | 277 Views
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.
E N D
CPCS 204: Data Structures & AlgorithmsChapter 7 Trees Instructor: Nouf Ghanimi
overview 7.3 Binary Trees
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.
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.
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.
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.
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.
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.
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.
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
Binary Tree representation Linked list Array
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
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
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
Binary Tree Applications decision processes arithmetic expressions searching
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
+ 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))
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
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
Maximum Number Of Nodes Maximum number of nodes = 1 + 2 + 4 + 8 + … + 2h n <= 2h+1 - 1
Height 3 full binary tree. Full Binary Tree • A full binary tree of a given height h has 2h+1 – 1 nodes.
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).
Binary Tree traversal Preorder Postorder Inorder Euler Tour
+ 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
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
+ 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))