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

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

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

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

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)

2

OverviewOverview

• Binary Search Trees.

• Hash Tables.

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

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)

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)

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)

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)

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)

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)

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)

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)

11

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

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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.