Download presentation
Presentation is loading. Please wait.
Published byAlan Stevenson Modified over 9 years ago
1
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)
2
BST Example
3
if the root is NULL then the item is not found else 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 if BST Search Algorithm
4
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 Performance Height (h) 1 2 3 4
5
if the root is NULL then replace the empty tree with the new node else 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 if Insertion into BST
6
If the node is a leaf then delete it. Removal from BST: Case 1
7
If the node has only one child then delete it and replace it with it’s child node. Removal from BST: Case 2
8
If the node has a right child then find the right-most node of its left sub-tree… Removal from BST: Case 3
9
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 3
10
if the root is NULL then return if root->Data < SearchData then delete from left subtree elseif root->Data > SearchData then delete from left subtree else 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 node end Removal from BST
11
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 / Delete
12
1)For Insert() function you have to pass TreeNode * pointer by reference: void Insert(TreeNode *& root, const string& data); 2)For Delete() function you have to pass pointer to the parent node: void Delete(TreeNode *& root, TreeNode *& parent, const string& value); 3)To separate words in sentence use cin >> operator: while ( cin.peek() != '\n' ) cin >> s; BST Insert / Delete Hints
13
Read chapter 8, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -2%.Assignment
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.