Files
Data-Structure/BinaryTree/indexBinarySearchTree/indexBinarySearchTree.h
2025-08-12 12:09:53 +08:00

132 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef INDEXBINARYSEARCHTREE_H
#define INDEXBINARYSEARCHTREE_H
#include <iostream>
#include "binaryTreeNode.h"
template<class K, class E>
class indexBinarySearchTree {
private:
binaryTreeNode<K, E>* root;
int treeSize;
int leftSize;
void inOrder(binaryTreeNode<K, E>* node);
public:
indexBinarySearchTree() : root(nullptr), leftSize(0) {}
indexBinarySearchTree(const indexBinarySearchTree<K, E>& e) : leftSize(e.leftSize) {
//深拷贝 采取中序遍历的方法
}
std::pair<K, E>* find(const K& e);
void insert(const std::pair<K, E>& e);
void erase(const K& e);
void ascent();
};
template<class K, class E>
void indexBinarySearchTree<K, E>::inOrder(binaryTreeNode<K, E> *node) {
if (node == nullptr) return;
inOrder(node->left);
std::cout << node->element.second << " ";
inOrder(node->right);
}
template<class K, class E>
std::pair<K, E> * indexBinarySearchTree<K, E>::find(const K &e) {
binaryTreeNode<K, E> *current = root;
while (current != nullptr) {
if (current->element.first < e) {
current = current->right;
}
else if (current->element.first > e) {
current = current->left;
}
else {
return &current->element;
}
}
//没有匹配的
return nullptr;
}
template<class K, class E>
void indexBinarySearchTree<K, E>::insert(const std::pair<K, E> &e) {
binaryTreeNode<K, E>* pp = nullptr;
binaryTreeNode<K, E>* p = root;
while (p != nullptr) {
pp = p;
if (e.first < p->element.first) {
p = p->left;
}
else if (e.first > p->element.first) {
p = p->right;
}
else {
p->element.second = e.second;
return;
}
}
auto newNode = new binaryTreeNode<K, E>(e);
if (root != nullptr) {
if (e.first < pp->element.first) {
pp->left = newNode;
}
else if (e.first > pp->element.first) {
pp->right = newNode;
}
}
else {
root = newNode;
}
treeSize++;
}
template<class K, class E>
void indexBinarySearchTree<K, E>::erase(const K &e) {
//这里用find不好因为没法确定其父节点是哪个
binaryTreeNode<K, E>* p = root;
binaryTreeNode<K, E>* pp = nullptr;
bool isleft = false;
while (p != nullptr && p->element.first != e) {
pp = p;
if (e < p->element.first) {
p = p->left;
isleft = true;
}
else if (e > p->element.first) {
p = p->right;
isleft = false;
}
}
if (p == nullptr) return;
//p-nochild
if (p->left == nullptr && p->right == nullptr) {
if (pp->left == p) pp->left = nullptr;
else if (pp->right == p) pp->right = nullptr;
treeSize--;
delete p;
}
//p-child == 2
if (p->left != nullptr && p->right != nullptr) {
pp = p->right;
while (pp->left != nullptr) {
pp = pp->left;
}
p->element = pp->element;
treeSize--;
delete pp;
}
//p-child == 1
if (p->right == nullptr) {
isleft ? pp->left = p->left : pp->right = p->left;
}
if (p->left == nullptr) {
isleft ? pp->left = p->right : pp->right = p->right;
}
}
template<class K, class E>
void indexBinarySearchTree<K, E>::ascent(){
inOrder(root);
std::cout << ";" << std::endl;
}
#endif //INDEXBINARYSEARCHTREE_H