150 likes | 323 Views
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.
E N D
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 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
Tree Terminology, cont’d Complete Tree Full Tree Incomplete Tree
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
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
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
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
Binary Tree Node Root Node data left right data data left right left right Left Child Node Right Child Node
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
Arithmetic Expression Tree + 10+3*4/(2-1) Nodes: operations +,-,*,/ Leafs: operands nodes * 10 leafs 3 / 4 - 2 1
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
Expression Evaluation Algorithm Recursive process: ExpressionValue = ValueOf(treeRoot->Left) {treeRoot->Operation} ValueOf(treeRoot->Right) op left right
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; } };
Assignment Read chapter 8, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.