Properties: -Each node has a value -The left subtree contains only values less than the parent...

Preview:

Citation preview

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 than or equal to the parent node’s value

Binary Search Tree (BST)Binary Search Tree (BST)

BST ExampleBST Example

if the root is NULL then the item is not foundelse if the root->Data = SearchData then

return the root else if the root->Data > SearchData

then recursively search left subtree else recursively search right subtree end ifend if

BST Search AlgorithmBST Search Algorithm

If the tree is full BST search requires O(logN) operations, which is the same as O(h) where h is the height of the tree.

BST Search PerformanceBST Search Performance

Height (h)

1

2

3

4

if the root is NULL then replace the empty tree with the new nodeelse if the root->Data = SearchData then

do nothing, the item is already in the tree else if the root->Data > SearchData then

recursively insert into the left subtree else recursively insert into the right subtree end ifend if

Insertion into BSTInsertion into BST

If the node is a leaf then delete it.

Removal from BST: Case 1Removal from BST: Case 1

If the node has only one child then delete it and replace it with it’s child node.

Removal from BST: Case 2Removal from BST: Case 2

If the node has a right child then find the right-most node of its left sub-tree…

Removal from BST: Case 3Removal from BST: Case 3

If the node has a right child then:- find the right-most node of its left

sub-tree- set the node’s value to the value

of the right-most node (RMN)- set RMN’s parent to the

reference to the RMN->Left- delete the right-most node.

Removal from BST: Case 3Removal from BST: Case 3

if the root is NULL then returnif root->Data < SearchData then delete from left subtreeelseif root->Data > SearchData then delete from left subtreeelse if root->IsLeaf() then delete root set its parent reference to NULL elseif the root has only one child then set the parent of the root to the reference to that child delete root elseif the root->Left->Right = NULL then set the parent of the root to root->Left else find the rightmost node in the right subtree of the left child copy its data into the root->Data set its parent to the reference to the right most node’s left child delete the rightmost nodeend

Removal from BSTRemoval from BST

Write a program that:- reads a sentence from cin- separates words and stores them in BST- prints the tree (inorder traversal)- prompts for a word to be removed from BST- prints the tree (inorder traversal)Write an Insert function for inserting new words

into the tree.Write a Delete function for deleting words from

the tree.Tip: You may use your earlier tree code.

Exercise: BST Insert / DeleteExercise: BST Insert / Delete

1) For Insert() function you have to pass TreeNode<T>* pointer by reference:void Insert(TreeNode<T>*& root, const string& data);

2) For Delete() function you have to pass pointer to the parent node:void Delete(TreeNode<T>*& root, TreeNode<T>*& parent, const string& value);

3) To separate words in sentence use cin >> operator:

while ( cin.peek() != '\n' )cin >> s;

BST Insert / Delete HintsBST Insert / Delete Hints

Read chapter 8, prepare for quiz next class.

I will randomly question 10 students. Correct answer earns 1%, incorrect earns -2%.

AssignmentAssignment

Recommended