Tree traversals BST properties Search Insertion

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Class 9: Review. cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate.
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.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
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 ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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 Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Trees Chapter 15.
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Binary Search Trees Chapter 7 Objectives
Cinda Heeren / Geoffrey Tien
Binary Search Tree (BST)
Review: recursion Tree traversals
Trees.
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Trees.
Tree data structure.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Search Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Tree data structure.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Hassan Khosravi / Geoffrey Tien
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Trees Chapter 10.
Binary Search Trees Chapter 7 Objectives
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
CSC 205 – Java Programming II
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Tree traversals BST properties Search Insertion Binary Search Trees Tree traversals BST properties Search Insertion October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Binary tree traversal A traversal algorithm for a binary tree visits each node in the tree Typically, it will do something while visiting each node! Traversal algorithms are naturally recursive There are three traversal methods inOrder preOrder postOrder October 30, 2017 Hassan Khosravi / Geoffrey Tien

inOrder traversal algorithm void inOrder(BNode* nd) { if (nd != NULL) inOrder(nd->left); visit(nd); inOrder(nd->right); } typedef struct BNode { int data; struct BNode* left; struct BNode* right; } BNode; The visit function would do whatever the purpose of the traversal is (e.g. print the data value of the node). October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien preOrder traversal visit(nd); preOrder(nd->left); 1 preOrder(nd->right); 17 2 6 visit visit 13 27 preOrder(left) preOrder(left) preOrder(right) preOrder(right) 3 5 7 8 visit visit visit 9 16 20 39 preOrder(left) preOrder(left) preOrder(left) preOrder(right) preOrder(right) preOrder(right) visit 4 preOrder(left) preOrder(right) 11 visit preOrder(left) preOrder(right) October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien postOrder traversal postOrder(nd->left); postOrder(nd->right); 8 visit(nd); 17 4 7 postOrder(left) postOrder(left) 13 27 postOrder(right) postOrder(right) visit visit 2 3 5 6 postOrder(left) postOrder(left) postOrder(left) 9 16 20 39 postOrder(right) postOrder(right) postOrder(right) visit visit visit postOrder(left) 1 postOrder(right) visit postOrder(left) 11 postOrder(right) visit October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Exercise What will be printed by an in-order traversal of the tree? preOrder? postOrder? inOrder(nd->left); 17 visit(nd); inOrder(nd->right); 9 27 6 16 20 31 Food for thought: which traversal will be useful for copying a tree? Deallocating all nodes? 12 39 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Before we begin... Another type of tree traversal We have seen pre-order, in-order, post-order traversals What about a traversal that visits every node in a level before working on the next level? level-order traversal 41 33 87 21 74 36 45 78 25 Use some ADT to support this? October 30, 2017 Hassan Khosravi / Geoffrey Tien

Binary search trees A data structure for the Dictionary ADT? A binary search tree is a binary tree with a special property For all nodes in the tree: All nodes in a left subtree have labels less than the label of the subtree's root All nodes in a right subtree have labels greater than or equal to the label of the subtree's root Binary search trees are fully ordered October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST example 17 13 27 9 16 20 39 11 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST inOrder traversal inOrder(nd->leftchild); inOrder traversal on a BST retrieves data in sorted order visit(nd); 5 inOrder(nd->rightchild); 17 3 7 inOrder(left) inOrder(left) 13 27 visit visit inOrder(right) inOrder(right) 1 4 6 8 inOrder(left) inOrder(left) inOrder(left) 9 visit 16 20 visit 39 visit inOrder(right) inOrder(right) inOrder(right) inOrder(left) 2 visit inOrder(right) inOrder(left) 11 visit inOrder(right) October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST implementation Binary search trees can be implemented using a reference structure Tree nodes contain data and two pointers to nodes BNode* leftchild data BNode* rightchild Data to be stored in the tree (usually an object) References or pointers to other tree Nodes October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is greater than the value in the node search its right subtree Otherwise return true, (or a pointer to the data, or …) How many comparisons? One for each node on the path Worst case: height of the tree + 1 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST search example search(27); 17 27 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST search example search(16); 17 13 16 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST search example search(12); 17 13 9 11 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Search implementation Search can be implemented iteratively or recursively int search(BNode* nd, int key) { if (nd == NULL) return FALSE; else if (nd->data == key) return TRUE; else { if (key < nd->data) return search(nd->left, key); else return search(nd->right, key); } October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST insertion The BST property must hold after insertion Therefore the new node must be inserted in the correct position This position is found by performing a search If the search ends at the (null) left child of a node make its left child refer to the new node If the search ends at the right child of a node make its right child refer to the new node The cost is about the same as the cost for the search algorithm, O(height) October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST insertion example Insert 43 47 Create new node Find position 32 63 Link node 19 41 54 79 43 10 23 37 44 53 59 96 7 12 30 43 57 91 97 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien BST insertion example Create new BST 3 Insert 3 Insert 15 15 Insert 21 Insert 23 21 Insert 37 Search 45 23 How many operations for Search? Complexity? 37 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Insert implementation Insert can also be implemented iteratively or recursively BNode* insert(BNode* nd, int key) { if (nd == NULL) { BNode* newnode = (BNode*) malloc(sizeof(BNode)); newnode->data = key; newnode->left = NULL; newnode->right = NULL; return newnode; } else { if (key < nd->data) nd->left = insert(nd->left, key); else nd->right = insert(nd->right, key); return nd; October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Find Min, Find Max Find minimum: From the root, keep following left child links until no more left child exists (i.e. NULL) Find maximum: From the root, follow right child links until no more right child exists 43 18 68 12 7 9 33 52 56 67 27 39 50 21 October 30, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Exercise Write iterative implementations for: // returns TRUE or FALSE int search(BNode* nd, int key) // returns the root of the subtree receiving the // inserted item BNode* insert(BNode* nd, int key) // returns the value of the minimum key int findMin(BNode* nd) // returns the value of the maximum key int findMax(BNode* nd) October 30, 2017 Hassan Khosravi / Geoffrey Tien

Readings for this lesson Thareja Chapter 9.4 10.1 – 10.2.2, 10.2.4 – 10.2.5, 10.2.8 – 10.2.9 Next class: Thareja Chapter 10.2.3 October 30, 2017 Hassan Khosravi / Geoffrey Tien