学数据结构到现在写的最久的一部分,简单总结一下这一周 1.考虑到未来考试要求,实现语言从java换成了C++,没想到意外的顺利 2.没别的了,干就完了 3.代码肯定有错误的地方,虽然我自认为是完美主义者,但都是为了效率没办法啦
#include <iostream>
#include "queue"
#include "stack"
#include <string>
#include <algorithm>
using namespace std;
class Node
{
public:
int element;
Node *left;
Node *right;
Node *parent;
Node()
{
this->element = 0;
this->left = nullptr;
this->right = nullptr;
this->parent = nullptr;
}
Node(int element)
{
this->element = element;
this->left = nullptr;
this->right = nullptr;
this->parent = nullptr;
}
};
class BinarySearchTreesZH
{
private:
int size;
public:
Node *root = nullptr;
void add(int element);
Node *searchBST(int element, Node *root);
bool isEmpty();
void clear();
void clear(Node *node);
void preorderTraversal(Node *root);
void inorderTraversal(Node *root);
void postorderTraversal(Node *root);
void levelorderTraversal(Node *root);
void preorderTraversalNoRecursion(Node *root);
void inorderTraversalNoRecursion(Node *root);
void postorderTraversalNoRecursion(Node *root);
int height(Node *node);
int heightNoRecursion(Node *node);
bool isCompleteTree(Node *node);
Node *invertTreePreOrder(Node *node);
Node *invertTreeInOrder(Node *node);
Node *invertTreePostOrder(Node *node);
Node *invertTreeLeverOrder(Node *node);
Node *searchNode(int key, Node *node);
Node *predecessor(Node *node);
Node *successor(Node *node);
void remove(Node *node);
};
bool BinarySearchTreesZH::isEmpty()
{
if (size == 0)
{
return true;
}
else
{
return false;
}
}
void BinarySearchTreesZH::add(int element)
{
Node *node = new Node(element);
if (root == nullptr)
{
root = node;
size++;
return;
}
Node *nextCompare = root;
Node *parent = root;
while (nextCompare != nullptr)
{
parent = nextCompare;
if (node->element < nextCompare->element)
{
nextCompare = nextCompare->left;
}
else if (node->element > nextCompare->element)
{
nextCompare = nextCompare->right;
}
else if (node->element = nextCompare->element)
{
return;
}
}
if (node->element < parent->element)
{
parent->left = node;
}
else if (node->element > parent->element)
{
parent->right = node;
}
size++;
return;
}
Node *BinarySearchTreesZH::searchBST(int element, Node *root)
{
if (root == nullptr)
{
return nullptr;
}
Node *node = root;
while (node != nullptr)
{
if (element == node->element)
{
return node;
}
if (element < node->element)
{
node = node->left;
}
if (element > node->element)
{
node = node->right;
}
}
cout<<"Not Found"<<endl;
return nullptr;
}
void BinarySearchTreesZH::preorderTraversal(Node *node)
{
if (node == nullptr)
{
return;
}
cout << node->element << " ";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
void BinarySearchTreesZH::inorderTraversal(Node *node)
{
if (node == nullptr)
{
return;
}
inorderTraversal(node->left);
cout << node->element << " ";
inorderTraversal(node->right);
}
void BinarySearchTreesZH::postorderTraversal(Node *node)
{
if (node == nullptr)
{
return;
}
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->element << " ";
}
void BinarySearchTreesZH::levelorderTraversal(Node *node)
{
queue<Node *> list;
if (node == nullptr)
{
return;
}
else
{
list.push(node);
}
while (list.size() != 0)
{
cout << list.front()->element << " ";
if (list.front()->left != nullptr)
{
list.push(list.front()->left);
}
if (list.front()->right != nullptr)
{
list.push(list.front()->right);
}
list.pop();
}
}
void BinarySearchTreesZH::preorderTraversalNoRecursion(Node *node)
{
stack<Node *> stk;
if (node == nullptr)
{
return;
}
stk.push(node);
while (stk.size() != 0)
{
Node *top = stk.top();
cout << stk.top()->element << " ";
stk.pop();
if (top->right != nullptr)
{
stk.push(top->right);
}
if (top->left != nullptr)
{
stk.push(top->left);
}
}
}
void BinarySearchTreesZH::inorderTraversalNoRecursion(Node *node)
{
if (node == nullptr)
{
return;
}
stack<Node *> stk;
stk.push(node);
while (stk.size() != 0)
{
while (node->left != nullptr)
{
stk.push(node->left);
node = node->left;
}
Node *top = stk.top();
cout << top->element << " ";
stk.pop();
if (top->right != nullptr)
{
node = top->right;
stk.push(top->right);
}
}
}
void BinarySearchTreesZH::postorderTraversalNoRecursion(Node *node)
{
if (node == nullptr)
{
return;
}
string s = " ";
stack<Node *> stk;
stk.push(node);
Node *top = stk.top();
while (stk.size() != 0)
{
top = stk.top();
stk.pop();
s = s + to_string(top->element) + " ";
if (top->left != nullptr)
{
stk.push(top->left);
}
if (top->right != nullptr)
{
stk.push(top->right);
}
}
reverse(s.begin(), s.end());
cout << s;
}
int BinarySearchTreesZH::height(Node *node)
{
int nodeHeight = 0;
int leftSonHeight = 0;
int rightSonHeight = 0;
if (node == nullptr)
{
nodeHeight = 0;
return nodeHeight;
}
leftSonHeight = height(node->left);
rightSonHeight = height(node->right);
if (leftSonHeight >= rightSonHeight)
{
nodeHeight = leftSonHeight + 1;
}
else
{
nodeHeight = rightSonHeight + 1;
}
return nodeHeight;
}
int BinarySearchTreesZH::heightNoRecursion(Node *node)
{
queue<Node *> list;
if (node == nullptr)
{
return 0;
}
else
{
list.push(node);
}
int height = 0;
int nextCengNum = 1;
int j = 0;
while (list.size() != 0)
{
if (list.front()->left != nullptr)
{
list.push(list.front()->left);
}
if (list.front()->right != nullptr)
{
list.push(list.front()->right);
}
list.pop();
j++;
if (j == nextCengNum)
{
height++;
nextCengNum = list.size();
j = 0;
}
}
return height;
}
bool BinarySearchTreesZH::isCompleteTree(Node *node)
{
if (node == nullptr)
{
return false;
}
queue<Node *> list;
list.push(node);
while (list.size() != 0)
{
Node *front = list.front();
if (front->left != nullptr && front->right != nullptr)
{
list.pop();
if (front->left != nullptr)
{
list.push(front->left);
}
if (front->right != nullptr)
{
list.push(front->right);
}
}
else if (front->left == nullptr && front->right != nullptr)
{
return false;
}
else
{
if (front->left != nullptr)
{
list.push(front->left);
}
list.pop();
while (list.size() != 0)
{
front = list.front();
if (front->left != nullptr || front->right != nullptr)
{
return false;
}
list.pop();
}
}
}
return true;
}
Node *BinarySearchTreesZH::invertTreePreOrder(Node *node)
{
if (node == nullptr)
{
return node;
}
Node *tmp = node->left;
node->left = node->right;
node->right = tmp;
invertTreePreOrder(node->left);
invertTreePreOrder(node->right);
return node;
}
Node *BinarySearchTreesZH::invertTreeInOrder(Node *node)
{
if (node == nullptr)
{
return node;
}
invertTreeInOrder(node->left);
Node *tmp = node->left;
node->left = node->right;
node->right = tmp;
invertTreeInOrder(node->left);
return node;
}
Node *BinarySearchTreesZH::invertTreePostOrder(Node *node)
{
if (node == nullptr)
{
return node;
}
invertTreePostOrder(node->left);
invertTreePostOrder(node->right);
Node *tmp = node->left;
node->left = node->right;
node->right = tmp;
return node;
}
Node *BinarySearchTreesZH::invertTreeLeverOrder(Node *node)
{
if (node == nullptr)
{
return node;
}
queue<Node *> list;
list.push(node);
while (list.size() != 0)
{
Node *front = list.front();
list.pop();
Node *tmp = front->left;
front->left = front->right;
front->right = tmp;
if (front->left != nullptr)
{
list.push(front->left);
}
if (front->right != nullptr)
{
list.push(front->right);
}
}
return node;
}
Node *BinarySearchTreesZH::predecessor(Node *node)
{
if (node == nullptr)
{
return node;
}
if (node->left != nullptr)
{
node = node->left;
while (node->right != nullptr)
{
node = node->right;
}
return node;
}
else
{
while (node->parent != nullptr && node->parent->right != node)
{
node = node->parent;
}
return node->parent;
}
}
Node *BinarySearchTreesZH::successor(Node *node)
{
if (node == nullptr)
{
return node;
}
if (node->right != nullptr)
{
node = node->right;
while (node->left != nullptr)
{
node = node->left;
}
return node;
}
else
{
while (node->parent != nullptr && node->parent->left != node)
{
node = node->left;
}
return node->parent;
}
}
void BinarySearchTreesZH::remove(Node *node)
{
if (node->left != nullptr && node->right != nullptr)
{
node->element = predecessor(node)->element;
remove(predecessor(node));
}
else if (node->left == nullptr && node->right == nullptr)
{
if (node->parent == nullptr)
{
node = nullptr;
}
if (node == node->parent->left)
{
node->parent->left = nullptr;
}
if (node == node->parent->right)
{
node->parent->right = nullptr;
}
}
else
{
if (node->parent == nullptr)
{
if (node->left != nullptr)
{
root = node->left;
}
else if (node->right != nullptr)
{
root = node->right;
}
}
else if (node == node->parent->left)
{
if (node->left != nullptr)
{
node->parent->left = node->left;
node->left->parent = node->parent;
}
else if (node->right != nullptr)
{
node->parent->left = node->left;
node->left->parent = node->parent;
}
}
else if (node == node->parent->right)
{
if (node->left != nullptr)
{
node->parent->right = node->left;
node->left->parent = node->parent;
}
else if (node->right != nullptr)
{
node->parent->right = node->left;
node->left->parent = node->parent;
}
}
}
}
Node *BinarySearchTreesZH::searchNode(int key, Node *node)
{
if (node == nullptr)
{
return node;
}
if (node->element == key)
{
cout << "find finished" << endl;
return node;
}
return searchNode(key, node->left);
return searchNode(key, node->right);
return nullptr;
}
void BinarySearchTreesZH::clear()
{
if (root == nullptr)
{
return;
}
if (root->left != nullptr)
{
return clear(root->left);
}
if (root->right != nullptr)
{
return clear(root->right);
}
size = 0;
delete this;
}
void BinarySearchTreesZH::clear(Node *root)
{
if (root == nullptr)
{
return;
}
if (root->left != nullptr)
{
return clear(root->left);
}
if (root->right != nullptr)
{
return clear(root->right);
}
delete root;
}
|