Unformatted text preview:

Lecture 32OutlineBinary TreesLinked Node Representation Binary Tree Node Class Template 1 Binary Tree Node Class Template 2Binary Tree Node Class Template 3Building a Binary Tree 1Building a Binary Tree 2Tree Traversal 1Tree Traversal 2In-Order TraversalInOrderPrintPre- & Post-Order TraversalPreOrderPrint, PostOrderPrintTraversal ExerciseDisplaying a Tree 1Displaying a Tree 2TreePrintGeneral Binary Tree AlgorithmTreeSizeCopying a TreeTreeCopyDestroying a TreeTreeClearMonday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 1Lecture 32Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture32/*.*Reminder: Practical Exam 2 is this Wednesday, November 10, 5-7pm. Regular class time (11am) is an optional exam review session. An exam review sheet has been posted. Previous exam problems were handed out in class.Submission system is accepting Project 6 in two parts. They are due on Friday.Questions?Monday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 2OutlineBinary tree node class templateBinary tree toolkit – build as we goTraversals – InOrderPrint, PreOrderPrint, PostOrderPrintTreePrint – display node values as a treeTreeSizeTreeCopyTreeClearMonday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 3Binary TreesReview: 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, November 8 CS 215 Fundamentals of Programming II - Lecture 32 4Linked Node RepresentationWe can represent a tree node using a class that has an attribute to hold the node value (data) and two tree node pointer attributes (leftChild and rightChild).The pointer attributes are used to link a node to the nodes of its children. An entire tree is used represented as a pointer to the root node. The empty tree is represented using the null pointer.Monday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 5Binary Tree Node Class TemplateBTNode<T> class template in bintree.h is similar to the linked list node class:template <typename T>class BTNode { public: // Item type typedef typedef T value_type; // Constructor BTNode (const value_type & initData = T(), BTNode *initLeft=0, BTNode *initRight=0) {...} // Mutators to set attributes void SetData (const value_type & newData) {...} void SetLeft (Node *newLink){...} void SetRight (Node *newLink){...}Monday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 6Binary Tree Node Class Template // Accessors to attributes, const and non-const value_type Data () const {...} const BTNode *Left() const {...} BTNode *Left () {...} const BTNode *Right() const {...} BTNode *Right () {...} // Member function to determine if node is a leaf bool IsLeaf () const {...} private: value_type dataItem; BTNode *leftChild, *rightChild;}; 12.1dataItemrightChildleftChildMonday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 7Binary Tree Node Class TemplateIn addition to the BTNode class 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; we will build the toolkit as we go.First, write the code for the IsLeaf member function.Monday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 8Building a Binary TreeExamine file tree-examples.cpp. It is a driver program for testing the BTNode class 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, November 8 CS 215 Fundamentals of Programming II - Lecture 32 9Building a Binary Tree12.1dataItemrightChildleftChildr14.6 9.3-4.8qptp = new BTNode<double> (-4.8);q = new BTNode<double> (14.6, p);r = new BTNode<double> (12.1);r->SetLeft(q); r->SetRight(t);t = new BTNode<double> (9.3);Monday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 10Tree TraversalAs 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, November 8 CS 215 Fundamentals of Programming II - Lecture 32 11Tree TraversalThe 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, November 8 CS 215 Fundamentals of Programming II - Lecture 32 12In-Order TraversalThe 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. Travers the right child subtree ("go right")Monday, November 8 CS 215 Fundamentals of Programming II - Lecture 32 13InOrderPrintFor today's class, we will apply the traversal algorithms to printing out the node data values.InOrderPrint is a template function that receives a BTNode 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, November 8 CS 215 Fundamentals of Programming II - Lecture 32 14Pre- & Post-Order TraversalBy changing the order of L, N, and R, there are 6 different possible scans.NLR is a pre-order scan, so called because


View Full Document

UE CS 215 - Lecture 32

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 32
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 32 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 32 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?