Binary Trees 4 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation Applications: Organization charts File systems Programming environments Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada
Binary Trees 19 Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: (a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree T L and the right subtreeT R. The nodes in T consist of node R and all the nodes in T L and T R.
Binary Trees 21 Binary Tree A binary tree is a tree with the following properties: Each internal node has two children The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching A B C FG D E H I
Binary Trees 22 Examples of Binary Trees Expression tree Non-leaf (internal) nodes contain operators Leaf nodes contain operands Huffman tree Represents Huffman codes for characters appearing in a file or stream Huffman code may use different numbers of bits to encode different characters ASCII or Unicode uses a fixed number of bits for each character
Binary Trees 23 Examples of Huffman Tree Code for b = 100000 Code for w = 110001 Code for s = 0011 Code for e = 010
Binary Trees 60 Selected Samples of Binary Trees
Binary Trees 61 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 ( a 1) (3 b)) 2 a1 3b
Binary Trees 62 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? How about coffee?On expense account? StarbucksSpike’sAl FornoCafé Paragon Yes No YesNoYesNo
Binary Trees 69 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) cs16/ homeworks/ todo.txt 1K programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 456 8
Binary Trees 72 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 Algorithm evalExpr(v) if isExternal (v) return v.element () else x evalExpr(leftChild (v)) y evalExpr(rightChild (v)) operator stored at v return x y 2 51 32
Binary Trees 74 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Make Money Fast! 1. MotivationsReferences2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 678 9 Algorithm preOrder(v) visit(v) for each child w of v preorder (w)
Binary Trees 77 Binary Tree Traversals Preorder: Visit root, traverse left, traverse right Inorder: Traverse left, visit root, traverse right Postorder: Traverse left, traverse right, visit root
Binary Trees 78 Visualizing Tree Traversals Can visualize traversal by imagining a mouse that walks along outside the tree If mouse keeps the tree on its left, it traces a route called the Euler tour: Preorder: record node first time mouse is there Inorder: record after mouse traverses left subtree Postorder: record node last time mouse is there
Binary Trees 79 Visualizing Tree Traversals (2) Preorder: a, b, d, g, e, h, c, f, i, j
Binary Trees 80 Visualizing Tree Traversals (3) Inorder: d, g, b, h, e, a, i, f, j, c
Binary Trees 81 Visualizing Tree Traversals (4) Postorder: g, d, h, e, b, i, j, f, c, a
Binary Trees 82 Traversals of Expression Trees Inorder traversal can insert parentheses where they belong for infix form Postorder traversal results in postfix form Prefix traversal results in prefix form Infix form (x + y) * ((a + b) / c) Postfix form: x y + a b + c / * Prefix form: * + x y / + a b c
Binary Trees 93 Rotated Form first_child (left) links: black next_sibling (right) links: red
Binary Trees 94 General (Non-Binary) Trees Nodes can have any number of children
Binary Trees 95 General (Non-Binary) Trees (2) A general tree can be represented using a binary tree Left link is to first child Right link is to next younger sibling
Binary Trees 96 Summary Slide 1 §- trees -hierarchical structures that place elements in nodes along branches that originate from a root. -Nodes in a tree are subdivided into levels in which the topmost level holds the root node. §-Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure. -Tree terminology with which you should be familiar: parent | child | descendents | leaf node | interior node | subtree.
Binary Trees 97 subtree Tree Terminology Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grand-grandchild, etc. A B DC GH E F IJ K Subtree: tree consisting of a node and its descendants
Binary Trees 98 Summary Slide 2 §- Binary Trees -Most effective as a storage structure if it has high density §-ie: data are located on relatively short paths from the root. §-A complete binary tree has the highest possible density -an n-node complete binary tree has depth int(log 2 n). -At the other extreme, a degenerate binary tree is equivalent to a linked list and exhibits O(n) access times.
Binary Trees 99 Summary Slide 3 §- Traversing Through a Tree -There are six simple recursive algorithms for tree traversal. -The most commonly used ones are: 1)inorder (LNR) 2)postorder (LRN) 3)preorder (NLR). -Another technique is to move left to right from level to level. §-This algorithm is iterative, and its implementation involves using a queue.