0% found this document useful (0 votes)
44 views19 pages

Binary Search Trees: Erin Keith

This document discusses binary search trees. It begins by defining the properties of a binary search tree that the values in each node must be greater than all values in its left subtree and less than all values in its right subtree. It then covers removing a node from a binary search tree, which involves finding the node, determining the removal case, and handling the removal based on whether the node is a leaf, has one child, or two children. It also discusses searching for a node by comparing the target value to the current node's value. The document provides code implementations for removing a node, finding the inorder successor, and searching for a node in a binary search tree.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views19 pages

Binary Search Trees: Erin Keith

This document discusses binary search trees. It begins by defining the properties of a binary search tree that the values in each node must be greater than all values in its left subtree and less than all values in its right subtree. It then covers removing a node from a binary search tree, which involves finding the node, determining the removal case, and handling the removal based on whether the node is a leaf, has one child, or two children. It also discusses searching for a node by comparing the target value to the current node's value. The document provides code implementations for removing a node, finding the inorder successor, and searching for a node in a binary search tree.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Binary Search

Trees
ERIN KEITH

18_BINARY_SEARCH_TREE 1
Topics
1. remove
2. getEntry
3. Uses

18_BINARY_SEARCH_TREE 2
Binary Search Tree
MUST MAINTAIN THIS STRUCTURE
• Binary tree that has the following properties for each node n:
• n’s value is > all values in n’s left subtree TL
• n’s value is < all values in n’s right subtree TR
• Both TL and TR are binary search trees
• A binary tree whose nodes contain objects and
• Data in a node is greater than the data in the node’s left child
• Data in a node is less than the data in the node’s right child

18_BINARY_SEARCH_TREE 3
Binary Search Tree

18_BINARY_SEARCH_TREE 4
Linked Implementation
#ifndef LINKED_BSEARCHTREE
#define LINKED_BSEARCHTREE

#include "LinkedBTreeNode.h"
#include "LinkedBTree.h"

template<class ItemType>
class LinkedBSearchTree: public LinkedBTree<ItemType>{
private:
LinkedBTreeNode<ItemType>* rootPtr;

18_BINARY_SEARCH_TREE 5
Linked Implementation
protected:
LinkedBTreeNode<ItemType>* placeNode(LinkedBTreeNode<ItemType>* subTreePtr,
LinkedBTreeNode<ItemType>* newNodePtr);

LinkedBTreeNode<ItemType>* removeValue(LinkedBTreeNode<ItemType>* subTreePtr, const


ItemType& target, bool& isSuccessful) override;

LinkedBTreeNode<ItemType>* removeNode(LinkedBTreeNode<ItemType>* nodePtr);

LinkedBTreeNode<ItemType>* removeLeftmostNode(LinkedBTreeNode<ItemType>* inorder


Successor);

LinkedBTreeNode<ItemType>* findNode(LinkedBTreeNode<ItemType>* treePtr, const ItemType&


target) const;

18_BINARY_SEARCH_TREE 6
Linked Implementation
public:
LinkedBSearchTree();

bool isEmpty() const;


int getHeight() const;
int getNumberOfNodes() const;
ItemType getRootData() const;
bool add(const ItemType& newData);
bool remove(const ItemType& data);
void clear();
ItemType getEntry(const ItemType& anEntry) const;
bool contains(const ItemType& anEntry) const;

void preorderTraverse(void visit(ItemType&)) const;


void inorderTraverse(void visit(ItemType&)) const;
void postorderTraverse(void visit(ItemType&)) const;

~LinkedBSearchTree();
};
#include "LinkedBSearchTree.cpp"
18_BINARY_SEARCH_TREE 7
#endif
Tree Implementation
The public tree interface functions are like a wrapper function that
gets called from outside the class.

The protected class function does the actual work using recursion.
template<class ItemType>
bool LinkedBSearchTree<ItemType>::remove(const ItemType& data){
LinkedBTreeNode<ItemType>* newNodePtr(newData);
bool success = false;

rootPtr = removeValue(rootPtr, data, success);

return success;
}

18_BINARY_SEARCH_TREE 8
Binary Search Tree
REMOVE
Steps
1. Find node with target value
2. Decide removal case
3. Remove
1. Find leftmost node

18_BINARY_SEARCH_TREE 9
Binary Search Tree
SEARCHING FOR NODE TO DELETE
Four cases
1.
2.
3.
4.

18_BINARY_SEARCH_TREE 10
Binary Search Tree
SEARCHING FOR NODE TO DELETE
Four cases
1. Node is not in tree
2. Current node is target
3. Current node is greater than target
4. Current node is less than target

18_BINARY_SEARCH_TREE 11
Linked Implementation
template<class ItemType>
LinkedBTreeNode<ItemType>* LinkedBSearchTree<ItemType>:: removeValue(LinkedBTreeNode<ItemType>*
subTreePtr, const ItemType& target, bool& isSuccessful){

if (subTreePtr == nullptr){
isSuccessful = false;
}
else if(subTreePtr->getItem() == target){
isSuccessful = true;
return removeNode(subTreePtr);
}
else if(subTreePtr->getItem() > target){
LinkedBTreeNode<ItemType>* tempPtr = removeValue(subTreePtr->getLeftChildPtr(), target,
isSuccessful);
subTreePtr->setLeftChildPtr(tempPtr);
}
else {
LinkedBTreeNode<ItemType>* tempPtr = removeValue(subTreePtr->getRightChildPtr(), target,
isSuccessful);
subTreePtr->setRightChildPtr(tempPtr);
}

return subTreePtr;
}
18_BINARY_SEARCH_TREE 12
Binary Search Tree
REMOVING NODE
Three cases
1. Node is a leaf
2. Node has only one child
3. Node has two children

18_BINARY_SEARCH_TREE 13
Linked Implementation
template<class ItemType>
LinkedBTreeNode<ItemType>* LinkedBSearchTree<ItemType>:: removeNode(LinkedBTreeNode
<ItemType>* nodePtr){

if (node is leaf){
delete node
}
else if(node has one child){
delete node
return that child (child will replace current node)
}
else { //node has two children
find inorder successor (leftmost node)
remove it
return inorder succesor (to replace current node)
}
}

18_BINARY_SEARCH_TREE 14
Binary Search Tree
FINDING INORDER SUCCESSOR (leftmost node)
Three cases
1. Node has left child
2. Node has no left child

18_BINARY_SEARCH_TREE 15
Linked Implementation
template<class ItemType>
LinkedBTreeNode<ItemType>* LinkedBSearchTree<ItemType>::
removeLeftmostNode(LinkedBTreeNode <ItemType>* nodePtr, ItemType& inorderSuccessor){

if(node->getLeftChildPtr() == nullptr){
inorderSuccessor = nodePtr->getItem();
return removeNode(nodePtr);
}
else {
tempPtr = removeLeftmostNode(nodePtr->getLeftChildPtr(), inorderSuccessor);
nodePtr->setLeftChildPtr(tempPtr);
return nodePtr;
}
}

18_BINARY_SEARCH_TREE 16
Binary Search Tree
SEARCHING FOR NODE
Four cases
1. Node is not in tree
2. Current node is target
3. Current node is greater than target
4. Current node is less than target

18_BINARY_SEARCH_TREE 17
Linked Implementation
template<class ItemType>
LinkedBTreeNode<ItemType>* LinkedBSearchTree<ItemType>:: findNode(LinkedBTreeNode
<ItemType>* subTreePtr, ItemType& target){
if (subTreePtr == nullptr || subTreePtr->getItem() == target){
return subTreePtr;
}
else if(subTreePtr->getItem() > target){
return findNode(subTreePtr->getLeftChildPtr(), target);
}
else {
return findNode(subTreePtr->getRightChildPtr(), target);
}
}

18_BINARY_SEARCH_TREE 18
Next Class
Red Black Trees
Textbook:
• Chapters 19.4
Internet:
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-123-red-black-trees.pdf

18_BINARY_SEARCH_TREE 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy