Download
binary search tree bst n.
Skip this Video
Loading SlideShow in 5 Seconds..
Binary Search Tree (BST) PowerPoint Presentation
Download Presentation
Binary Search Tree (BST)

Binary Search Tree (BST)

119 Views Download Presentation
Download Presentation

Binary Search Tree (BST)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Binary Search Tree (BST) 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

  2. BST Example

  3. BST Search Algorithm 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 end if

  4. BST Search Performance Height (h) 1 2 3 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.

  5. Insertion into BST if the root is NULL then replace the empty tree with the new node else if the root->Data = SearchData thendo 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 end if

  6. Removal from BST: Case 1 If the node is a leaf then delete it.

  7. Removal from BST: Case 2 If the node has only one child then delete it and replace it with it’s child node.

  8. Removal from BST: Case 3 If the node has a right child then find the right-most node of its left sub-tree…

  9. Removal 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.

  10. Removal from BST 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

  11. Exercise: BST Insert / Delete 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.

  12. BST Insert / Delete Hints For Insert() function you have to pass TreeNode<T>* pointer by reference:void Insert(TreeNode<T>*& root, const string& data); For Delete() function you have to pass pointer to the parent node:void Delete(TreeNode<T>*& root, TreeNode<T>*& parent, const string& value); To separate words in sentence use cin >> operator: while ( cin.peek() != '\n' ) cin >> s;

  13. Assignment Read chapter 8, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -2%.