Lecture 33OutlineBinary TreesLinked Node Representation Binary Tree Node Struct Template 1 Binary Tree Node Struct Template 2Binary Tree Node Struct Template 3Building a Binary Tree 1Building a Binary Tree 2Tree Traversal 1Tree Traversal 2In-Order Traversalinorder_printPre- & Post-Order Traversalpreorder_print, postorder_printTraversal ExerciseDisplaying a Tree 1Displaying a Tree 2tree_printGeneral Binary Tree Algorithmtree_sizeCopying a Treetree_copyDestroying a Treetree_clearMonday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 1Lecture 33Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture33/*.*Homework 12 posted.Submission system is accepting Project 6 in two parts. They are due on Friday.Questions?Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 2OutlineBinary tree node struct templateBinary tree toolkit – build as we goTraversals – inorder_print, preorder_print, postorder_printtree_print – display node values as a treetree_size – return the number of nodes in a treetree_copy – return the root of a copytree_clear – delete all the nodes of a treeMonday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 3Binary TreesReview: a binary tree is a finite set of nodes. The set of nodes may be empty, called an empty tree. If the set is not empty, it meets the following rules:1. There is one special node called the root.2. Each node may be associated with up to two other different nodes, called its left child and its right child.3. Each node, except the root, has exactly one parent; the root has no parent.4. There is a path from every node following its parent back to the root.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 4Linked Node RepresentationWe can represent a tree node using a struct that has a field to hold the node value (data) and two tree node pointer fields (left_child and right_child).The pointer fields are used to link a node to the nodes of its children. An entire tree is represented by a pointer to its root node. The empty tree is represented using the null pointer.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 5Binary Tree Node Struct TemplateBTNode<T> struct template in bintree.h is similar to the linked list node class:template <typename T>struct BTNode { // Item type typedef typedef T value_type; // Constructor BTNode (const value_type & init_data = T(), BTNode *init_left=0, BTNode *init_right=0) { data = init_data; left_child = init_left; right_childe = init_right; }Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 6Binary Tree Node Struct Template // Fields value_type data; BTNode *left_child, *right_child;};Note this is the same types of fields as used in the doubly-linked list node. The difference will be in how the fields are used.12.1dataright_childleft_childMonday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 7Binary Tree Node Struct TemplateIn addition to the BTNode struct template, bintree.h has stubs for templates for some common functions that are used to access and manipulate binary trees.Call this the binary tree toolkit.Most the code that is missing is in the textbook (using a BTNode class); we will build the toolkit as we go.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 8Building a Binary TreeExamine file tree-examples.cpp. It is a driver program for testing the BTNode struct and binary tree toolkit templates. A makefile is provided.The first part is building a binary tree by hand using pointer variables to each node. The root pointer is r.A picture of the resulting tree is shown on the next slide.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 9Building a Binary Tree12.1dataright_childleft_childr14.6 9.3-4.8qptp = new BTNode<double> (-4.8);q = new BTNode<double> (14.6, p);r = new BTNode<double> (12.1);r->left_child = q; r->right_child = t;t = new BTNode<double> (9.3);Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 10Tree TraversalAs with any container, we often would like to "scan" every node in a tree. (I.e., visit each node in some systematic manner.) Since trees are non-linear, there are many choices.In fact, a binary tree is a recursive data structure. Each (left and right) child of a node is itself the root of a binary (sub)tree. The base case is an empty subtree.This means that recursion is used most often to access every node in the tree.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 11Tree TraversalThe classic traversals consist of three separate actions:'N' – process the root; i.e., perform some action on the root node value'L' – recursively descend into the left subtree'R' – recursively descend into the right subtree"Recursively descend " means to repeat the algorithm with the child as the root (of the subtree), and terminate when reaching an empty tree.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 12In-Order TraversalThe order in which the three actions are completed determines the different recursive scans. E.g., LNR is an in-order scan, i.e., visit the node in-between visiting the left and right subtrees.The general algorithm is:1. Traverse the left child subtree ("go left")2. Process the node value3. Traverse the right child subtree ("go right")Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 13inorder_printFor today's class, we will apply the traversal algorithms to printing out the node data values.inorder_print is a template function that receives a BTNode<T> pointer to the root of a (sub)tree and prints out the (sub)tree values using an in-order scan.Write the code for this function. Compile and run the driver program to test it.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 14Pre- & Post-Order TraversalBy changing the order of L, N, and R, there are 6 different possible scans.NLR is a pre-order scan, so called because the root node value is processed before the children subtrees are visited.LRN is a post-order scan, so called because the root node value is processed after the children subtrees are visited.Note: these three scans are left to right.Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 15preorder_print, postorder_printLike inorder_print, preorder_print and
View Full Document