1 / 15

Trees

Trees. Nonlinear Containers. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. root. edge. node. edge. node. edge. leaf. Tree Terminology. root. parent node. child node. leaf (no children). sub-tree.

prisca
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. Nonlinear Containers Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. root edge node edge node edge leaf

  3. Tree Terminology root parent node child node leaf(no children) sub-tree Binary Tree: two child nodes Oct-tree: eight child nodes, etc. Huffman Tree: binary tree of codes of characters that might appear in the encoded message

  4. Tree Terminology, cont’d Complete Tree Full Tree Incomplete Tree

  5. Tree Traversal (Recursive) If the tree is empty (i.e. leaf) then return Else for each child node traverse sub-tree end for End if

  6. Binary Tree Preorder Traversal a Preorder: process root node, traverse left sub-tree, traverse right sub-tree Node processing order: a b d e c b c d e

  7. Binary Tree Inorder Traversal a Inorder: traverse left sub-tree, process root node, traverse right sub-tree Node processing order: d b e a c b c d e

  8. Binary Tree Postorder Traversal a Postorder: traverse left sub-tree, traverse right sub-tree, process root node Node processing order: d e b c a b c d e

  9. Binary Tree Node Root Node data left right data data left right left right Left Child Node Right Child Node

  10. Exercise: Arithmetic Express. Tree Task: Build a tree for an arithmetic expression and evaluate it. What kind of tree? Binary, full Why Binary? Because arithmetic operations require two operands, e.g. a+b Why Full? Because arithmetic operations require exactly two operands

  11. Arithmetic Expression Tree + 10+3*4/(2-1) Nodes: operations +,-,*,/ Leafs: operands nodes * 10 leafs 3 / 4 - 2 1

  12. Tree Construction Algorithm For simplicity assume single-digit operands, no parenthesis, well-formed expressions e.g.:1+5*3-4/2 Set treeRoot = NULL Set currentNode = treeRoot Scan expression from left If you find 0..9 then Create newLeaf node, set its value to 0..9 If the currentNode = NULL thentreeRoot = currentNode = newLeafelse currentNode->Right = newLeaf If you find + or – (or *,/ andcurrentNode->Right = NULL) then Create newRoot node, set its value the operation found Set newRoot->Left =treeRoot Set treeRoot = newRoot Set currentNode = newRoot If you find * or / then create newChild node, set its value to * or / Set newChild->Left = currentNode->Right Set currentNode ->Right = newChild Set currentNode = newChild Until you reach the end of string

  13. Expression Evaluation Algorithm Recursive process: ExpressionValue = ValueOf(treeRoot->Left) {treeRoot->Operation} ValueOf(treeRoot->Right) op left right

  14. Declare TreeNode Structure template<typename T> struct TreeNode { TreeNode(const T& value, TreeNode<T>* left = NULL, TreeNode<T>* right = NULL) { Value = value; Left = left; Right = right; } T Value; TreeNode<T>* Left; TreeNode<T>* Right; bool IsLeaf() const { return Left == NULL && Right == NULL; } };

  15. Assignment Read chapter 8, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.

More Related