Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Advertisements

Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Programming Practice 3 - Dynamic Data Structure
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
 2003 Prentice Hall, Inc. All rights reserved Stacks Upcoming program –Create stack from list insertAtFront, removeFromFront –Software reusability.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
(c) University of Washington20-1 CSC 143 Java Trees.
Chapter 20 Custom Templatized Data Structures
Chapter 12 – Data Structures
Trees Chapter 15.
Data Structure and Algorithms
Recursive Objects (Part 4)
12 C Data Structures.
Chapter 22 Custom Generic Data Structures
Binary Search Tree (BST)
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees CMSC 202, Version 5/02.
20.5 Stacks Upcoming program Create stack from list
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree

Topics Templated Tree  insertNode  inoder Traversal  preorder Traversal  postorder Traversal

Linked lists, stacks and queues are linear data structures. A tree is a nonlinear, two-dimensional data structure.  arrays arrange data linearly, binary trees can be envisioned as storing data in two dimensions Tree nodes contain two or more links.  trees whose nodes all contain two links (none, one or both of which may be null). Trees

Refer to nodes A, B, C and D. The root node (node B ) is the first node in a tree. Each link in the root node refers to a child (nodes A and D ). The left child (node A ) is the root node of the left subtree (which contains only node A ), and the right child (node D ) is the root node of the right subtree (which contains nodes D and C ). The children of a given node are called siblings (e.g., nodes A and D are siblings). A node with no children is a leaf node (e.g., nodes A and C are leaf nodes). Trees (cont.)

A binary search tree (with no duplicate node values)  the values in any left subtree are less than the value in its parent node  the values in any right subtree are greater than the value in its parent node. Trees (cont.)

TreeNode Class Template

TreeNode Class Template (cont.)

The TreeNode class template definition declares Tree as its friend.  This makes all member functions of a given specialization of class template Tree friends of the corresponding specialization of class template TreeNode, They can access the private members of TreeNode objects of that type.  TreeNode template parameter NODETYPE is used as the template argument for Tree in the friend declaration TreeNode s specialized with a particular type can be processed only by a Tree specialized with the same type (e.g., a Tree of int values manages TreeNode objects that store int values). TreeNode & Tree Class Template (cont.)

TreeNode ’s private data  the node’s data value  pointer leftPtr (to the node’s left subtree)  pointer rightPtr (to the node’s right subtree). The constructor sets data to the value supplied as a constructor argument  sets pointers leftPtr and rightPtr to zero (thus initializing this node to be a leaf node). Member function getData returns the data value. TreeNode Class Template (cont.)

Tree Class Template

Tree Class Template (cont.)

Class template Tree has as private data rootPtr  a pointer to the tree’s root node.  public member functions insertNode (that inserts a new node in the tree)  preOrderTraversal, inOrderTraversal and postOrderTraversal, each of which walks the tree in the designated manner. Each of these member functions calls its own recursive utility function to perform the appropriate operations on the internal representation of the tree  access the underlying private data to perform these functions.  recursion requires us to pass in a pointer that represents the next subtree to process. Tree Class Template (cont.)

The Tree constructor initializes rootPtr to zero to indicate that the tree is initially empty. The Tree class’s utility function insertNodeHelper is called by insertNode to recursively insert a node into the tree.  A node can only be inserted as a leaf node in a binary search tree. If the tree is empty, a new TreeNode is created, initialized and inserted in the tree. If the tree is not empty, the program compares the value to be inserted with the data value in the root node.  If the insert value is smaller, the program recursively calls insertNodeHelper to insert the value in the left subtree.  If the insert value is larger, the program recursively calls insertNodeHelper to insert the value in the right subtree. Tree Class Template (cont.)

If the value to be inserted is identical to the data value in the root node, the program prints the message "dup"  returns without inserting the duplicate value into the tree. insertNode passes the address of rootPtr to insertNodeHelper so it can modify the value stored in rootPtr (i.e., the address of the root node). To receive a pointer to rootPtr (which is also a pointer)  insertNodeHelper ’s first argument is declared as a pointer to a pointer to a TreeNode. Tree Class Template (cont.)

Member functions  inOrderTraversal,  preOrderTraversal  postOrderTraversal  traverse the tree and print the node values. Consider the following binary search tree. Tree Class Template Traversal

Tree Class Template preorder Traversal preOrder traversal is: root, left, right

Function preOrderTraversal invokes utility function preOrderHelper to perform the preorder traversal of the binary tree. The steps for an preorder traversal are:  Process the value in the node.  Traverse the left subtree with a preorder traversal. (This is performed by the call to preOrderHelper.)  Traverse the right subtree with a preorder traversal. (This is performed by the call to preOrderHelper.) The value in each node is processed as the node is visited. After the value in a given node is processed, the values in the left subtree are processed. Then the values in the right subtree are processed. The preorder traversal of the tree in example Figure is Preorder Traversal

Tree Class Template inOrder Traversal inOrder traversal is: left, root, right

Function inOrderTraversal invokes utility function inOrderHelper to perform the inorder traversal of the binary tree. The steps for an inorder traversal are:  Traverse the left subtree with an inorder traversal. (This is performed by the call to inOrderHelper )  Process the value in the node—i.e., print the node value.  Traverse the right subtree with an inorder traversal. (This is performed by the call to inOrderHelper.) The value in a node is not processed until the values in its left subtree are processed, because each call to inOrderHelper immediately calls inOrderHelper again with the pointer to the left subtree. inOrder Traversal

The inorder traversal of the tree in example Figure is Note that the inorder traversal of a binary search tree prints the node values in ascending order. The process of creating a binary search tree actually sorts the data  this process is called the binary tree sort. inOrder Traversal

Tree Class Template postOrder Traversal postOrder traversal is: left, right, root

Function postOrderTraversal invokes utility function postOrderHelper to perform the postorder traversal of the binary tree. The steps for a postorder traversal are:  Traverse the left subtree with a postorder traversal. (This is performed by the call to postOrderHelper.)  Traverse the right subtree with a postorder traversal. (This is performed by the call to postOrderHelper.)  Process the value in the node. The value in each node is not printed until the values of its children are printed. The postOrderTraversal of the tree in example Figure is postOrder Traversal

Tree Class Template Test Program

Test Program (cont.)

BST Applications: Duplicate Elimination The binary search tree facilitates duplicate elimination. An attempt to insert a duplicate value will be recognized  a duplicate will follow the same “go left” or “go right” decisions on each comparison as the original value did. The duplicate will eventually be compared with a node in the tree containing the same value. The duplicate value may simply be discarded at this point.

Binary Tree Search Searching a binary tree for a value that matches a key value is fast. If the tree is balanced, then each branch contains about half the number of nodes in the tree.  Each comparison of a node to the search key eliminates half the nodes.  This is called an O(log n) algorithm (Big O notation). A binary search tree with n elements would require a maximum of log 2 n comparisons either to find a match or to determine that no match exists. Searching a (tightly packed) 1,000,000 element binary search tree requires no more than 20 comparisons  2 20 > 1,000,000.

Other Binary Tree Operations The level order traversal of a binary tree visits the nodes of the tree row-by-row starting at the root node level.  On each level of the tree, the nodes are visited from left to right.  The level order traversal is not a recursive algorithm.

Exercise Implement the level order binary tree traversal using a common data structure we have discussed in the class.  Write the pseudo code of this algorithm.

Level Order Binary Tree Traversal Use the Queue data structure to control the output of the level order binary tree traversal. Algorithm  Insert / enqueue the root node in the queue  While there are nodes left in the queue, Get/dequeue the node in the queue Print the node’s value If the pointer to the left child of the node is not null  Insert/enqueue the left child node in the queue If the pointer to the right child of the node is not null  Insert/enqueue the right child node in the queue

Other Common Tree Data Strictures Binary search trees (BSTs)  Support O(log 2 N) operations  Balanced trees AVL trees, Splay trees B-trees for accessing secondary storage

Conclusions Accessing elements in a linear linked list can be prohibitive especially for large amounts of input. Trees are simple data structures for which the running time of most operations is O(log N) on average. For example, if N = 1 million:  Searching an element in a linear linked list requires at most O(N) comparisons (i.e., 1 million comparisons)  Searching an element in a binary search tree requires O(log 2 N) comparisons (  20 comparisons)