CS2420: Lecture 17 Vladimir Kulyukin Computer Science Department Utah State University.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
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());
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS2420: Lecture 18 Vladimir Kulyukin Computer Science Department Utah State University.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
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.
Binary Search Trees Chapter 6.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
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.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Data Structure & Algorithm 09 – Binary Search Tree JJCAO.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree Data Structures.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Search: Binary Search Trees Dr. Yingwu Zhu. Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n Assume == and.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
David Stotts Computer Science Department UNC Chapel Hill.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
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.
Chapter 12 – Data Structures
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
BST Trees
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
Presentation transcript:

CS2420: Lecture 17 Vladimir Kulyukin Computer Science Department Utah State University

Outline Trees (Chapter 4)

Tree Traversals InOrder traversal PreOrder traversal PostOrder traversal Level traversal

InOrder Traversal If the current node is not empty, –Traverse the left sub-tree; –Traverse the current node; –Traverse the right sub-tree.

InOrder Traversal in C++ template void processNode(CBinaryTreeNode *node) { // some code that processes the node somehow; } // We assume that we have implemented the getter and setter methods // for CBinaryTreeNode. template class CBinaryTreeNode { public: CBinaryTreeNode *getLeftChild() const { …} CBinaryTreeNode *getRightChild() const { … } void setLeftChild() { … } void setRightChild() { … } };

InOrder Traversal in C++ template void CBinaryTree ::inOrder(CBinaryTreeNode *node) { if ( node != NULL ) { inOrder(node->getLeftChild()); processNode(node); inOrder(node->getRightChild()); }

PreOrder Traversal If the current node is not empty, –Traverse the current node; –Traverse the left sub-tree; –Traverse the right sub-tree.

PreOrder Traversal in C++ template void CBinaryTree ::preOrder(CBinaryTreeNode *node) { if ( node != NULL ) { processNode(node); preOrder(node->getLeftChild()); preOrder(node->getRightChild()); }

PostOrder Traversal If the current node is not empty, –Traverse the left sub-tree; –Traverse the right sub-tree; –Traverse the current node.

PostOrder Traversal in C++ template void CBinaryTree ::postOrder(CBinaryTreeNode *node) { if ( node != NULL ) { postOrder(node->getLeftChild()); postOrder(node->getRightChild()); processNode(node); }

Level Traversal Initialize a queue of binary tree nodes. Add the root to the queue. While the queue is not empty –Pop the node from the queue; –Process the node; –If the popped node’s left child is not empty, add it to the queue; –If the popped node’s right child is not empty, add it to the queue; –Decide what to do with the popped node.

Deleting a Binary Tree template void CBinaryTree ::deleteTree(CBinaryTreeNode *node) { if ( node != NULL ) { deleteTree(node->GetLeftChild()); deleteTree(node->GetRightChild()); delete node; }

Basic Questions 3 and 4 from Lecture 1 How can I store/organize data efficiently? How can I retrieve/search data efficiently?

Collections and Dictionaries A Collection/Dictionary has a set of N elements each of which is indexed under a possibly unique key. The task of a collection/dictionary data structure is to insert/delete/retrieve an element given its key in the most efficient way.

Collections Lists, arrays, queues, and stacks are collections. List –Insertion: O(N); Deletion: O(N); Search: O(N). Array –Insertion: O(N); Deletion: O(N); Search: O(1). Queue –Insertion: O(1); Limited Deletion: O(1); Search: Not Available. Stack –Insertion: O(1): Limited Deletion: O(1); Search: Not Available.

Binary Search Tree: A Collection A Binary Search Tree (BST) is an empty binary tree. A BST is a non-empty binary tree such that –The keys in the left sub-tree are smaller than the root’s key; –The keys in the right sub-tree are larger than the root’s key; –The left and right sub-trees are BSTs.

Binary Search Tree ≠ Binary Tree Binary Search Tree ≠ Binary Tree. Every binary search tree is a binary tree. But not every binary tree is a binary search tree.

Example A BST Not A BST

BST with Duplicates If the key comparison is relaxed to =, the BST will contain duplicates. Two implementations of BST with duplicates: –Linked nodes; –Duplicate counters.

BT Node: Version 1 KEY DATA LEFT CHILD PTR RIGHT CHILD PTR

BT Node: Version 1.A KEY DATA LEFT CHILD PTR RIGHT CHILD PTR Key and Data are of the same type.

BT Node: Version 1.A template class CBinaryTreeNode { friend void ProcessNode(CBinaryTreeNode *node); protected: T m_Key; T m_Data; CBinaryTreeNode * m_LeftChild; CBinaryTreeNode * m_RightChild; public: CBinaryTreeNode * GetLeftChild() const; CBinaryTreeNode * GetRightChild() const; void SetLeftChild(CBinaryTreeNode *node); void SetRightChild(CBinaryTreeNode *node); T GetData() const { return m_Data; } T GetKey() const { return m_Key; } };

BT Node: Version 1.B KEY DATA LEFT CHILD PTR RIGHT CHILD PTR Key and Data are different types.

BT Node: Version 1.B template class CBinaryTreeNode { friend void ProcessNode(CBinaryTreeNode *node); protected: Key m_Key; Data m_Data; CBinaryTreeNode * m_LeftChild; CBinaryTreeNode * m_RightChild; public: CBinaryTreeNode * GetLeftChild() const; CBinaryTreeNode * GetRightChild() const; void SetLeftChild(CBinaryTreeNode *node); void SetRightChild(CBinaryTreeNode *node); Data GetData() const { return m_Data; } Key GetKey() const { return m_Key; } };

BT Node: Version 2 KEY = DATA LEFT CHILD PTR RIGHT CHILD PTR

BT Node: Version 2 template class CBinaryTreeNode { protected: T m_Data; CBinaryTreeNode * m_LeftChild; CBinaryTreeNode * m_RightChild; public: CBinaryTreeNode(); CBinaryTreeNode(const T& data); CBinaryTreeNode(const T& data, CBinaryTreeNode *left, CBinaryTreeNode *right); CBinaryTreeNode * GetLeftChild() const; CBinaryTreeNode * GetRightChild() const; void SetLeftChild(CBinaryTreeNode *node); void SetRightChild(CBinaryTreeNode *node); T GetData() const { return m_Data; } };

Binary Search Tree Interface template class CBinarySearchTree : public CBinaryTree { protected: CBinaryTreeNode *m_Current; int m_Size; // number of nodes in the tree. public: // method 1 bool Find(T& item); // method 2 CBinaryTreeNode * FindNode(const T &item, CBinaryTreeNode * &parent) const; // method 3 void Insert(const T& item); // method 4 CBinaryTreeNode * Delete(const T& item); };

Finding a Node and its Parent template CBinaryTreeNode * CBinarySearchTree ::FindNode(const T& item, CBinaryTreeNode * &parent) const { CBinaryTreeNode *current = m_Root; parent = NULL; while ( current != NULL ) { if ( item == current->GetData() ) { break; } else { parent = current; if ( item GetData() ) { current = current->GetLeftChild(); } else { current = current->GetRightChild(); } return current; }

Finding an Item in a BST Given a key K and a BST T with the root R –If T is empty, return false. –If R contains K, return R’s data and true. –If K is less than R’s key, search in the left sub- tree. –If K is greater than R’s key, search in the right sub-tree.

Finding an Item in a BST template bool CBinarySearchTree ::Find(T& item) { CBinaryTreeNode *parent; m_Current = FindNode(item, parent); if ( m_Current != NULL ) { item = m_Current->GetData(); return true; } else { return false; }

Inserting an Item Given a key K, and a BST T –See if the BST already contains an item under K. If yes, do nothing. –If no such element is found, insert K at the point where the search terminated.

Inserting an Item template void CBinarySearchTree ::Insert(const T& item) { CBinaryTreeNode *current = m_Root, *parent = NULL; if ( Find(item) == true ) return; while ( current != NULL ) { parent = current; if ( item GetData() ) current = current->GetLeftChild(); else current = current->GetRightChild(); } CBinaryTreeNode *addedNode = new CBinaryTreeNode (item); if ( parent == NULL ) m_Root = addedNode; else if ( item GetData() ) parent->SetLeftChild(addedNode); else parent->SetRightChild(addedNode); m_Size++; }