cse1303 part a data structures and algorithms lecture a13 – binary search trees (information...
Post on 20-Dec-2015
225 views
TRANSCRIPT
![Page 1: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/1.jpg)
CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms
Lecture A13 – Binary Search Trees Lecture A13 – Binary Search Trees (Information Retrieval)(Information Retrieval)
![Page 2: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/2.jpg)
2
OverviewOverview
• Binary Search Trees.
• Hash Tables.
![Page 3: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/3.jpg)
3
Recall - Binary Search Tree 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.
![Page 4: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/4.jpg)
4
Example 1:
31
43
64
20 40 56
28 33 47 59
89
key is an integer
![Page 5: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/5.jpg)
5
Fred
Dan Mary
Alan Eve
Bill Eric
Kate
Greg Len
Sue
Example 2: key is a string
![Page 6: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/6.jpg)
6
Binary Tree NodeBinary Tree Node
entry
link to right child nodelink to left
child node
![Page 7: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/7.jpg)
7
struct TreeNodeRec{ int key;
struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr;};
typedef struct TreeNodeRec TreeNode;
Binary Search Tree Node Binary Search Tree Node
Example 1:
![Page 8: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/8.jpg)
8
Example 2:
Binary Search Tree Node Binary Search Tree Node
#define MAXLEN 15
struct TreeNodeRec{ char key[MAXLEN];
struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr;};
typedef struct TreeNodeRec TreeNode;
![Page 9: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/9.jpg)
9
Recall:
maximumstring length
is fixed
#define MAXLEN 15
struct TreeNodeRec{ char key[MAXLEN];
struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr;};
typedef struct TreeNodeRec TreeNode;
![Page 10: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/10.jpg)
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.
![Page 11: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/11.jpg)
11
![Page 12: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/12.jpg)
12
struct BookRec{ char* author; char* title; char* publisher; /* etc.: other book information. */};
typedef struct BookRec Book;
Book Record
key
![Page 13: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/13.jpg)
13
struct TreeNodeRec{ Book info; struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr;};
typedef struct TreeNodeRec TreeNode;
Example 4: Binary Search Tree Node
![Page 14: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/14.jpg)
14
struct TreeNodeRec{ float key;
struct TreeNodeRec* leftPtr; struct TreeNodeRec* rightPtr;};
typedef struct TreeNodeRec TreeNode;
Tree NodeTree Node
![Page 15: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/15.jpg)
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
![Page 16: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/16.jpg)
16
MakeNodeMakeNode
• 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
![Page 17: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/17.jpg)
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;}
![Page 18: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/18.jpg)
18
0x2000
newNodePtr NULL
0x2000newNodePtr
0x20000x2000newNodePtr 3.3
NULL NULL
value 3.3
![Page 19: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/19.jpg)
19
InorderInorder
• Inorder traversal of a Binary Search Tree always gives the sorted order of the keys.
void printInorder(TreeNode* nodePtr){
}
initially, pointerto root node
traverse left sub-tree visit the node traverse right sub-tree
![Page 20: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/20.jpg)
20
InorderInorder
• 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) {
}
![Page 21: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/21.jpg)
21
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 22: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/22.jpg)
22
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 23: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/23.jpg)
23
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 24: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/24.jpg)
24
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 25: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/25.jpg)
25
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 26: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/26.jpg)
26
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 27: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/27.jpg)
27
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 28: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/28.jpg)
28
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 29: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/29.jpg)
29
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 30: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/30.jpg)
30
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 31: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/31.jpg)
31
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 32: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/32.jpg)
32
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 33: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/33.jpg)
33
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 34: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/34.jpg)
34
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 35: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/35.jpg)
35
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 36: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/36.jpg)
36
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 37: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/37.jpg)
37
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 38: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/38.jpg)
38
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 39: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/39.jpg)
39
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 40: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/40.jpg)
40
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 41: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/41.jpg)
41
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 42: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/42.jpg)
42
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 43: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/43.jpg)
43
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 44: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/44.jpg)
44
InorderInorder
void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}
nodePtr
![Page 45: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/45.jpg)
45
SearchSearch
31
43
64
20 40 56
28 33 47 59
89
Example: 59
57 found
45
![Page 46: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/46.jpg)
46
SearchSearch
31
43
64
20 40 56
28 33 47 59
89
Example: 61
57 failed
46
![Page 47: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/47.jpg)
47
Search: ChecklistSearch: 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)
![Page 48: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/48.jpg)
48
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL) { if (target < nodePtr->key) { nodePtr = search(nodePtr->leftPtr, target); } else if (target > nodePtr->key) { nodePtr = search(nodePtr->rightPtr, target); } } return nodePtr;}
![Page 49: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/49.jpg)
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 SearchFunction Call to Search
![Page 50: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/50.jpg)
50
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 51: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/51.jpg)
51
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 52: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/52.jpg)
52
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 53: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/53.jpg)
53
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.7nodePtr
![Page 54: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/54.jpg)
54
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 55: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/55.jpg)
55
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 56: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/56.jpg)
56
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 57: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/57.jpg)
57
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 58: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/58.jpg)
58
SearchSearch
TreeNode* search(TreeNode* nodePtr, float target){ if (nodePtr != NULL){ if (target < nodePtr->key) nodePtr = search(nodePtr->leftPtr, target); else if (target > nodePtr->key) nodePtr = search(nodePtr->rightPtr, target); } return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Find 0.5nodePtr
![Page 59: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/59.jpg)
59
InsertInsert
31
43
64
20 40 56
28 33 47 59
89
Example:57
![Page 60: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/60.jpg)
60
InsertInsert
31
43
64
20 40 56
28 33 47 59
89
Example:57
57
![Page 61: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/61.jpg)
61
InsertInsert
• Create new node for the item.
• Find a parent node.
• Attach new node as a leaf.
![Page 62: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/62.jpg)
62
Insert: RecursiveInsert: 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.
![Page 63: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/63.jpg)
63
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL) { nodePtr = makeTreeNode(item); } else if (item < nodePtr->key) { nodePtr->leftPtr = insert(nodePtr->leftPtr, item); } else if (item > nodePtr->key) { nodePtr->rightPtr = insert(nodePtr->rightPtr, item); }
return nodePtr;}
![Page 64: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/64.jpg)
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 InsertFunction Call to Insert
![Page 65: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/65.jpg)
65
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 66: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/66.jpg)
66
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 67: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/67.jpg)
67
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 68: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/68.jpg)
68
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
![Page 69: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/69.jpg)
69
InsertInsert
TreeNode* insert(TreeNode* nodePtr, float item){ if (nodePtr == NULL)
nodePtr = makeTreeNode(item); else if (item < nodePtr->key) nodePtr->leftPtr = insert(nodePtr->leftPtr, item); else if (item > nodePtr->key) nodePtr->rightPtr = insert(nodePtr->rightPtr, item); return nodePtr;}
1.0
1.81.1
2.71.4
1.9
0.4 0.7
0.80.3
0.6
Insert 0.9nodePtr
0.9
![Page 70: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/70.jpg)
70
SortingSorting
To sort a sequence of items:
• Insert items into a Binary Search Tree.
• Then Inorder Traverse the tree.
![Page 71: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/71.jpg)
71
SortSort
Sort the following list into a binary search tree
0.51.0 0.7 2.12.5 3.6
![Page 72: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/72.jpg)
72
SortSort
0.51.0 0.7 2.12.5 3.6
1.0
Sort the following list into a binary search tree
![Page 73: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/73.jpg)
73
SortSort
0.51.0 0.7 2.12.5 3.6
1.0
2.5
Sort the following list into a binary search tree
![Page 74: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/74.jpg)
74
SortSort
0.51.0 0.7 2.12.5 3.6
1.0
2.50.5
Sort the following list into a binary search tree
![Page 75: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/75.jpg)
75
SortSort
0.51.0 0.7 2.12.5 3.6
1.0
2.50.5
0.7
Sort the following list into a binary search tree
![Page 76: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/76.jpg)
76
SortSort
1.0
2.50.5
0.7 3.6
Sort the following list into a binary search tree
0.51.0 0.7 2.12.5 3.6
![Page 77: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/77.jpg)
77
SortSort
0.51.0 0.7 2.12.5 3.6
1.0
2.50.5
0.7 3.62.1
Sort the following list into a binary search tree
![Page 78: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/78.jpg)
78
Sorting: AnalysisSorting: Analysis
• Insert (i+1)th item: ~ log2(i) comparisons
~ log2 n
• Average Case: O(n log(n))
![Page 79: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/79.jpg)
79
Sorting: AnalysisSorting: Analysis
• Insert (i+1)th item: ~ i comparisons
Example:
Insert: 1, 3, 7, 9, 11, 15
• Worst Case: O(n2)
![Page 80: CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)](https://reader036.vdocuments.net/reader036/viewer/2022062421/56649d445503460f94a21152/html5/thumbnails/80.jpg)
80
RevisionRevision• Binary Search Tree• Make Tree Node, Insert item, Search for an item, and Print Inorder. • Tree sort.
Revision: ReadingRevision: Reading• Kruse 9• Standish 9• Langsam 5• Deitel & Deitel 12.7
PreparationPreparationNext lecture: Hash Tables• Read Chapter 8.6 in Kruse et al.