1 CSE 2341 Object Oriented Programming with C++ Note Set #21.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Computer Science C++ High School Level By Guillermo Moreno.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
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.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Trees. Trees Traversal Inorder  (Left) Root (Right) Preorder  Root (Left) (Right) Postorder  (Left) (Right) Root Root LeftRight.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
CSCS-200 Data Structure and Algorithms Lecture
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Chapter 19: Recursion.
Definition and Application of Binary Trees
Non Linear Data Structure
Problems with Linked List (as we’ve seen so far…)
Binary Search Trees.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Binary Tree Traversals
Operations on Binary Tree
Prepared by: S. Kondakci
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Pointers & Dynamic Data Structures
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Chapter 20: Binary Trees.
Yan Shi CS/SE 2630 Lecture Notes
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 CSE 2341 Object Oriented Programming with C++ Note Set #21

2 Overview Recursive linked list ops Binary Trees

3 Recursion w/ LL Recursion can be used to traverse a linked list Any recursive solution has an iterative counterpart –recursive is sometime more elegant though 2 functions : –countNodes(ListNode*); –showReverse(ListNode*);

4 Counting Nodes in a List int NumberList::numNodes() { return countNodes(head); } int NumberList::countNodes(ListNode* nodePtr) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } Must provide public member function. Recursive function must start at head, but head can’t be passed in from outside the class.

5 Counting Nodes in a List int NumberList::countNodes(ListNode* nodePtr) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } 534 Head Null

6 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null stack

7 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } stack

8 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + countNodes(NULL); else return 0; } stack

9 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + countNodes(NULL); else return 0; } int NumberList::countNodes(NULL) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } stack

10 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + 0; else return 0; } stack

11 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + 1; else return 0; } stack

12 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + 2; else return 0; } 534 Head Null stack finally returns 3 to numNodes

13 Displaying Nodes in Reverse Order int NumberList::displayBackwards() { return showReverse(head); } int NumberList::showReverse(ListNode* nodePtr) { if(nodePtr != NULL) { showReverse(nodePtr->next); cout value << “ “; } Must provide public member function. Recursive function must start at head, but head can’t be passed in from outside the class.

14 Counting Nodes in a List void NumberList::showReverse(ListNode* nodePtr) { if(nodePtr != NULL) { showReverse(nodePtr->next); cout value << “ “; } 534 Head Null

15 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null

16 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; }

17 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }

18 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; } void NumberList::showReverse(NULL) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }

19 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }

20 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; }

21 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null

22 Binary Trees

23 Binary Tree –non linear linked structure –each node may point to 2 other nodes –every node has exactly 1 predecessor tree pointer

24 Binary Tree tree pointer p f a t z null

25 Binary Tree Terminology tree pointer p f a t z null Left Subtree Leaf Node Root Node

26 Basis: TreeNode struct TreeNode { int value; TreeNode* left; TreeNOde* right; };

27 IntBinaryTree Class class IntBinaryTree { private: struct TreeNode { int value; TreeNode *left; TreeNode *right; }; TreeNode *root; void insert(TreeNode *&, TreeNode *&); void destroySubTree(TreeNode *); void deleteNode(int, TreeNode *&); void makeDeletion(TreeNode *&); void displayInOrder(TreeNode *); void displayPreOrder(TreeNode *); void displayPostOrder(TreeNode *);

28 IntBinaryTree Class Continued public: IntBinaryTree()// Constructor { root = NULL; } ~IntBinaryTree()// Destructor { destroySubTree(root); } void insertNode(int); bool searchNode(int); void remove(int); void displayInOrder() {displayInOrder(root); } void displayPreOrder() {displayPreOrder(root); } void displayPostOrder() {displayPostOrder(root); } };

29 IntBinaryTree Class INSERT void IntBinaryTree::insertNode(int num) { TreeNode *newNode;// Pointer to a new node. // Create a new node and store num in it. newNode = new TreeNode; newNode->value = num; newNode->left = newNode->right = NULL; // Insert the node. insert(root, newNode); } Public member function to insert an integer. Calls the private member function insert.

30 IntBinaryTree Class INSERT void IntBinaryTree::insert(TreeNode*& nodePtr, TreeNode*& newNode) { if (nodePtr == NULL) nodePtr = newNode; // Insert the node. else if (newNode->value value) insert(nodePtr->left, newNode);//Search left branch else insert(nodePtr->right, newNode);//Search right branch } Private member function to insert an integer.

31 Binary Trees Next Week –Traversals in order pre order post order –Searching –Destroying –Deleting one node

32 Fini ?