DOC PREVIEW
UE CS 215 - Lecture 33

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 33Log 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 2OutlineBinary tree node struct templateBinary tree toolkit – build as we goTraversals – inorder_print, preorder_print, postorder_printtree_print – display node values as a treetree_size – return the number of nodes in a treetree_copy – return the root of a copytree_clear – delete all the nodes of a treeMonday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 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, April 4 CS 215 Fundamentals of Programming II - Lecture 33 4Linked Node RepresentationWe 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 TemplateBTNode<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 TemplateIn 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 TreeExamine 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 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, April 4 CS 215 Fundamentals of Programming II - Lecture 33 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, April 4 CS 215 Fundamentals of Programming II - Lecture 33 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. Traverse the right child subtree ("go right")Monday, April 4 CS 215 Fundamentals of Programming II - Lecture 33 13inorder_printFor 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 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 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_printLike inorder_print, preorder_print and


View Full Document

UE CS 215 - Lecture 33

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 33
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 33 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 33 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?