CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)

Slides:



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

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A17/18 – Revision.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Tree A data structure whose nodes contain two pointer fields. One or both pointers can have the value NULL. Each node in a binary tree can have.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
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.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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.
Introduction to C Programming CE Lecture 23 Binary 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 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
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
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.
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.
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)
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary Search Trees Chapter 7 Objectives
Trees.
Binary Search Tree Chapter 10.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees.
Chapter 20: Binary Trees.
Tree traversals BST properties Search Insertion
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)

2 Overview Binary Search Trees. Hash Tables.

3 Recall - Binary Search Tree A Binary Tree such that: Every node entry has a unique key. All the keys in the left subtree of a node are less than the key of the node. All the keys in the right subtree of a node are greater than the key of the node.

4 Example 1: key is an integer

5 Fred Dan Mary Alan Eve BillEric Kate GregLen Sue Example 2: key is a string

6 Binary Tree Node entry link to right child node link to left child node

7 struct TreeNodeRec { int key; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode; Binary Search Tree Node Example 1:

8 Example 2: Binary Search Tree Node #define MAXLEN 15 struct TreeNodeRec { char key[MAXLEN]; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode;

9 Recall: maximum string length is fixed #define MAXLEN 15 struct TreeNodeRec { char key[MAXLEN]; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode;

10 struct TreeNodeRec { char* key; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode; Recall: Allows strings of arbitrary length. Memory needs to be allocated dynamically before use. Use strcmp to compare strings.

11

12 struct BookRec { char* author; char* title; char* publisher; /* etc.: other book information. */ }; typedef struct BookRec Book; Book Record key

13 struct TreeNodeRec { Book info; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode; Example 4: Binary Search Tree Node

14 struct TreeNodeRec { float key; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode; Tree Node

15 #ifndef TREEH #define TREEH struct TreeNodeRec { float key; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr; }; typedef struct TreeNodeRec TreeNode; TreeNode* makeTreeNode(float value); TreeNode* insert(TreeNode* nodePtr, float item); TreeNode* search(TreeNode* nodePtr, float item); void printInorder(const TreeNode* nodePtr); void printPreorder(const TreeNode* nodePtr); void printPostorder(const TreeNode* nodePtr); #endif

16 MakeNode parameter: item to be inserted steps: –allocate memory for the new node –check if memory allocation is successful –if so, put item into the new node –set left and right branches to NULL returns: pointer to (i.e. address of) new node

17 TreeNode* makeTreeNode(float value) { TreeNode* newNodePtr = NULL; newNodePtr = (TreeNode*)malloc(sizeof(TreeNode)); if (newNodePtr == NULL) { fprintf(stderr, “Out of memory\n”); exit(1); } else { newNodePtr->key = value; newNodePtr->leftPtr = NULL; newNodePtr->rightPtr = NULL; } return newNodePtr; }

18 0x2000 newNodePtr NULL 0x2000 newNodePtr 0x2000 newNodePtr 3.3 NULL value 3.3

19 Inorder Inorder traversal of a Binary Search Tree always gives the sorted order of the keys. void printInorder(TreeNode* nodePtr) { } initially, pointer to root node traverse left sub-tree visit the node traverse right sub-tree

20 Inorder Inorder traversal of a Binary Search Tree always gives the sorted order of the keys. void printInorder(TreeNode* nodePtr) { printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } if (nodePtr != NULL) { }

21Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

22Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

23Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

24Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

25Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

26Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

27Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

28Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

29Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

30Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

31Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

32Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

33Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

34Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

35Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

36Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

37Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

38Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

39Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

40Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

41Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

42Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

43Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

44Inorder void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); } nodePtr

45 Search Example: found 45

46 Search Example: failed 46

47 Search: Checklist if target key is less than current node’s key, search the left sub-tree. else, if target key is greater than current node’s key, search the right sub-tree. returns: –if found, or if target key is equal to current node’s key, a pointer to node containing target key. –otherwise, NULL pointer. (Recall binary search)

48 TreeNode* search(TreeNode* nodePtr, float target) { if (nodePtr != NULL) { if (target key) { nodePtr = search(nodePtr->leftPtr, target); } else if (target > nodePtr->key) { nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; }

49 /* …other bits of code omitted… */ printf(“Enter target ”); scanf(“%f”, &item); if (search(rootPtr, item) == NULL) { printf(“Item was not found\n”); } else { printf(“Item found\n”); } /* …and so on… */ Function Call to Search

50Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.7 nodePtr

51Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.7 nodePtr

52Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.7 nodePtr

53Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.7 nodePtr

54Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.5 nodePtr

55Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.5 nodePtr

56Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.5 nodePtr

57Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.5 nodePtr

58Search TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr; } Find 0.5 nodePtr

59 Insert Example: 57

60 Insert Example: 57

61 Insert Create new node for the item. Find a parent node. Attach new node as a leaf.

62 Insert: Recursive parameters: –pointer to current node (initially: root node). –item to be inserted. If current node is NULL –Create a new node and return it. Else if item’s key is less (greater) than current node’s key: –otherwise, let the left (right) child node be the current node, setting the parent left (right) link equal that node, and repeat recursively.

63 TreeNode* insert(TreeNode* nodePtr, float item) { if (nodePtr == NULL) { nodePtr = makeTreeNode(item); } else if (item key) { nodePtr->leftPtr = insert(nodePtr->leftPtr, item); } else if (item > nodePtr->key) { nodePtr->rightPtr = insert(nodePtr->rightPtr, item); } return nodePtr; }

64 /* …other bits of code omitted… */ printf(“Enter number of items ”); scanf(“%d”, &n); for (i = 0; i < n; i++) { scanf(“%f”, &item); rootPtr = insert(rootPtr, item); } /* …and so on… */ Function Call to Insert

65Insert TreeNode* insert(TreeNode* nodePtr, float item) { if (nodePtr == NULL) nodePtr = makeTreeNode(item); else if (item key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr; } Insert 0.9 nodePtr

66Insert TreeNode* insert(TreeNode* nodePtr, float item) { if (nodePtr == NULL) nodePtr = makeTreeNode(item); else if (item key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr; } Insert 0.9 nodePtr

67Insert TreeNode* insert(TreeNode* nodePtr, float item) { if (nodePtr == NULL) nodePtr = makeTreeNode(item); else if (item key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr; } Insert 0.9 nodePtr

68Insert TreeNode* insert(TreeNode* nodePtr, float item) { if (nodePtr == NULL) nodePtr = makeTreeNode(item); else if (item key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr; } Insert 0.9 nodePtr

69Insert TreeNode* insert(TreeNode* nodePtr, float item) { if (nodePtr == NULL) nodePtr = makeTreeNode(item); else if (item key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr; } Insert 0.9 nodePtr 0.9

70 Sorting To sort a sequence of items: Insert items into a Binary Search Tree. Then Inorder Traverse the tree.

71Sort Sort the following list into a binary search tree

72Sort Sort the following list into a binary search tree

73Sort Sort the following list into a binary search tree

74Sort Sort the following list into a binary search tree

75Sort Sort the following list into a binary search tree

76Sort Sort the following list into a binary search tree

77Sort Sort the following list into a binary search tree

78 Sorting: Analysis Insert (i+1) th item: ~ log 2 (i) comparisons ~ log 2 n Average Case: O(n log(n))

79 Sorting: Analysis Insert (i+1) th item: ~ i comparisons Example: Insert: 1, 3, 7, 9, 11, 15 Worst Case: O(n 2 )

80Revision Binary Search Tree Make Tree Node, Insert item, Search for an item, and Print Inorder. Tree sort. Revision: Reading Kruse 9 Standish 9 Langsam 5 Deitel & Deitel 12.7 Preparation Next lecture: Hash Tables Read Chapter 8.6 in Kruse et al.