因为跟完了才想补这个博客emm,就倒叙吧,而且估计前面的我也不一定还记得hh 。附个bv吧:
【【强烈推荐】深入浅出数据结构 - 顶尖程序员图文讲解 - UP主翻译校对 (已完结)】 https://www.bilibili.com/video/BV1Fv4y1f7T1/?share_source=copy_web&vd_source=91f3feb21aaf57583c0f398bc469d207
p37(二叉搜索树的中序后续节点)24.5.19
还是比较难的吧(,看图来理解会好点。如果没有右子树,就要遍历了,写一个祖先和后续,依次遍历下来,这种情况要画图好理解点,我也是边看边写的hh
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
node* find(node* root, int data)
{
if (root == NULL) return NULL;
else if (root->data == data) return root;
else if (root->data < data) return find(root->right, data);
else return find(root->left, data);
}
struct node* findmin(node* root)
{
if (root == NULL) return NULL;
while (root->left != NULL)
root = root->left;
return root;
}
struct node* gethouxu(node* root, int data)
{
struct node* current = find(root, data);
if (current == NULL) return current;
if (current->right != NULL) return findmin(current->right);
else {
node* houxu = NULL;
node* zu = root;
while (zu != NULL)
{
if (current->data < zu->data) {
houxu = zu;
zu = zu->left;
}
else {
zu = zu->right;
}
return houxu;
}
}
}
void inorder(node* root)
{
if (root == NULL) return;
inorder(root->left);
cout << root->data << endl;
inorder(root->right);
}
node* insert(node* root, int data)
{
if (root == NULL)
{
root = new node();
root->data = data;
root->left = root->right = NULL;
}
else if (data < root->data) root->left = insert(root->left, data);
else root->right = insert(root->right, data);
return root;
}
int main()
{
node* root = NULL;
root = insert(root, 5);
root = insert(root, 10);
root = insert(root, 3);
root = insert(root, 4);
root = insert(root, 1);
root = insert(root, 11);
cout << "inorder:";
inorder(root);
cout << endl;
node* houxu = gethouxu(root, 1);
if (houxu == NULL) cout << "ji" << endl;
else cout<< "win";
return 0;
}
p36 (二叉搜索树的删除)24.5.19
这主要就是分有无右子树吧,如果有,就找右子树的min,就可以保证改后符合规则了。
//p36 删除
struct node
{
int data;
struct node* left;
struct node* right;
};
node* findmin(node* root)
{
while (root->left != NULL) root = root->left;
return root;
}
struct node* Delete(node* root, int data)
{
if (root == NULL) return root;
else if (data < root->data) root->left= Delete(root->left, data);
else if (data > root->data) root->right= Delete(root->right, data);
else {
if (root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
else if (root->left == NULL)
{
struct node* temp = root;
root = root->right;
delete temp;
}
else if (root->right == NULL)
{
struct node* temp = root;
root = root->left;
delete temp;
}
else {
struct node* temp = findmin(root->right);
root->data = temp->data;
root->right = Delete(root->right,temp->data);
}
return root;
}
}
void inorder(struct node* root)
{
if (root == NULL) return;
inorder(root->left);
cout << root->data<<" ";
inorder(root->right);
}
node* insert(node* root, int data)
{
if (root == NULL)
{
root = new node();
root->data = data;
root->left = root->right = NULL;
}
else if (root->data > data) root->left= insert(root->left, data);
else root->right= insert(root->right, data);
return root;
}
int main()
{
node* root = NULL;
root = insert(root, 5); root = insert(root, 10);
root = insert(root, 3); root = insert(root, 4);
root =insert(root, 1); root = insert(root, 11);
root = Delete(root, 5);
inorder(root);
}
p35 (判断是否为二叉搜索树)24.5.18
这个递归,蟀
// 判断是否为二叉树
struct node
{
int data;
node* left;
node* right;
node(int x) : data(x), left(NULL), right(NULL) {}
};
bool pd(node*root,int min,int max)
{
if (root == NULL) return true;
if (root->data<max &&
root->data>min &&
pd(root->left, min, root->data) &&
pd(root->right, root->data, max)) {
return true;
}
else return false;
}
bool ispd(node*root)
{
return pd(root, INT_MIN, INT_MAX);
}
int main() {
node* root = new node(2);
root->left = new node(1);
root->right = new node(3);
if (ispd(root))
cout << "The tree is a valid Binary Search Tree.\n";
else
cout << "The tree is not a valid Binary Search Tree.\n";
return 0;
}
p33,p34(二叉树层次遍历(广度优先,前中后序))24.5.18
// 二叉树层次遍历(广度优先,前序,中序,后序)
struct node
{
char data;
struct node* left;
struct node* right;
};
void levelorder(node* root)
{
queue<node*>q;
if (root == NULL)
{
return;
}
q.push(root);
while (!q.empty())
{
node* temp = q.front();
cout << temp->data << endl;
if (temp->left != NULL) { q.push(temp->left); }
if (temp->right != NULL) { q.push(temp->right); }
q.pop();
}
}
node* insert(node* root, char data)
{
if (root == NULL)
{
root = new node();
root->data = data;
root->left = root->right = NULL;
}
else if (data <= root->data)
{
root->left = insert(root->left, data);
}
else {
root->right = insert(root->right, data);
}
return root;
delete(root);
}
void preorder(struct node*root)
{
if (root == NULL) {
return;
}
cout << root->data << endl;
preorder(root->left);
preorder(root->right);
}
void inorder(struct node* root)
{
if (root == NULL) {
return;
}
inorder(root->left);
cout << root->data << endl;
inorder(root->right);
}
void postorder(struct node* root)
{
if (root == NULL) {
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << endl;
}
/*
m
/ \
b q
/\ \
a c z
*/
int main()
{
node* root = NULL;
root = insert(root, 'M'); root = insert(root, 'B');
root = insert(root, 'Q'); root = insert(root, 'Z');
root = insert(root, 'A'); root = insert(root, 'C');
cout << "pre:" << endl;
preorder(root);
cout << endl;
cout << "in:" << endl;
inorder(root);
cout << endl;
cout << "post:" << endl;
postorder(root);
cout << endl;
return 0;
}
p30,31(二叉树的高度,最值,查找) 24.5.17
//findheight不算熟,用了max(高度从0开始,所以加一)
struct bstnode {
int data;
bstnode* left;
bstnode* right;
};
bstnode* getnewnode(int data) {
bstnode* newnode = new bstnode();
newnode->data = data;
newnode->left = newnode->right = NULL;
return newnode;
}
bstnode* insert(bstnode*root,int data)
{
if (root == NULL) {
root = getnewnode(data);
}
else if (data <= root->data)
{
root->left = insert(root->left, data);
}
else {
root->right = insert(root->right, data);
}
return root;
delete(root);
}
bool search(bstnode* root, int data)
{
if (root == NULL)
{
return false;
}
else if (root->data == data)
{
return true;
}
else if (data <= root->data)
{
return search(root->left, data);
}
else
{
return search(root->right, data);
}
}
// 迭代版本
int max_a(struct bstnode* root)
{
struct bstnode* temp = root;
if (temp == NULL)
{
cout << "kong" << endl;
return -1;
}
while (temp->right != NULL)
{
temp = temp->right;
}
return temp->data;
}
// 递归版本
int max_b(struct bstnode* root)
{
struct bstnode* temp = root;
if (temp == NULL)
{
cout << "ji" << endl;
return -1;
}
else if (temp->right == NULL)
{
return temp->data;
}
return max_b(temp->right);
}
int findheight(struct bstnode* root)
{
if (root == NULL)
{
return -1;
}
return max(findheight(root->left), findheight(root->right)) + 1;
}
int main()
{
bstnode* root = NULL;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 60);
cout << "max:"<<max_b(root) << endl;
cout << "高度是" << findheight(root) << endl;
if (search(root,10) == true)
{
cout << "找到10咯" << endl;
}
else
cout << "没找到。。" << endl;
return 0;
}
ps:到此二叉树就没了,然后是队列和栈了
p24(用链表实现队列) 24.5.16
实现也不算难吧,还是不熟练,多看看
#include <iostream>
using namespace std;
struct node
{
int data;
struct node* next;
};
struct node* front = NULL;
struct node* rear = NULL;
void enqueue(int x)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
temp->next = NULL;
if (front ==NULL && rear == NULL)
{
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
void dequeue()
{
if (front == NULL)
{
printf("queue is empty\n");
return;
}
struct node* temp = front;
if (front == rear)
{
front = rear = NULL;
}
else {
front = front->next;
}
free(temp);
}
int Front()
{
if (front == NULL)
{
printf("queue is empty\n");
return -1;
}
return front->data;
}
void print()
{
struct node* temp = front;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(2); print();
enqueue(4);print();
enqueue(6); print();
dequeue(); print();
enqueue(8); print();
}
p23 (用循环数组实现队列) 24.5.16
还是挺难的,用max_size的时机,对于没用过循环数组来说,还是多看看
#include <iostream>
using namespace std;
#define MAX_SIZE 101
class Queue {
private:
int A[MAX_SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool IsEmpty() {
return (front == -1 && rear == -1);
}
bool IsFull() {
return (rear + 1) % MAX_SIZE == front;
}
void Enqueue(int x) {
cout << "Enqueuing " << x << " \n";
if (IsFull()) {
cout << "Error: Queue is Full\n";
return;
}
if (IsEmpty()) {
front = rear = 0;
}
else {
rear = (rear + 1) % MAX_SIZE;
}
A[rear] = x;
}
void Dequeue() {
cout << "Dequeuing \n";
if (IsEmpty()) {
cout << "Error: Queue is Empty\n";
return;
}
else if (front == rear) {
rear = front = -1;
}
else {
front = (front + 1) % MAX_SIZE;
}
}
void Print() {
int count = (rear + MAX_SIZE - front) % MAX_SIZE ;
cout << "Queue: ";
for (int i = 0; i <= count; i++) {
int index = (front + i) % MAX_SIZE;
cout << A[index] << " ";
}
cout << "\n";
}
};
int main() {
Queue Q;
Q.Enqueue(2); Q.Print();
Q.Enqueue(4); Q.Print();
Q.Enqueue(6); Q.Print();
Q.Dequeue(); Q.Print();
Q.Enqueue(8); Q.Print();
return 0;
}
p20,21用栈表示前缀后缀表达式(当时没写,水过去吧...
p18 (栈:检查括号的匹配性) 24.5.14
//数据结构p18 符号匹配
bool A(char a, char b)
{
if (a == '(' && b==')') return true;
if (a == '[' && b == ']') return true;
if (a == '{' && b == '}') return true;
return false;
}
bool B(string a)
{
stack<char>s;
for (int i = 0; i < a.size(); i++)
{
if (a[i] == '(' || a[i] == '[' || a[i] == '{')
{
s.push(a[i]);
}
if (a[i] == ')' || a[i] == ']' || a[i] == '}')
{
if (A(s.top(), a[i]))
{
s.pop();
return true;
}
}
}
if (s.empty())
{
return false;
}
return false;
}
int main()
{
string expression;
cout << "Enter an expression: ";
cin >> expression;
if (B(expression))
cout << "Balanced\n";
else
cout << "Not Balanced\n";
}
p17(双链表实现,反转链表) 24.5.13
// 双链表实现
struct node {
int data;
struct node* next;
struct node* prev;
};
struct node* head;
struct node* getnewnode(int x) {
struct node* newnode = new node;
newnode->data = x;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
void insertathead(int x)
{
struct node * newnode = getnewnode(x);
if (head == NULL)
{
head = newnode;
return;
}
head->prev = newnode;
newnode->next = head;
head = newnode;
}
void insertatTail(int x)
{
struct node* newnode = getnewnode(x);
struct node* temp = head;
if (head == NULL)
{
head = newnode;
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->prev = temp;
}
void print() {
struct node* temp = head;
cout << "forward: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void reverprint() {
struct node* temp = head;
if (temp == NULL) return;
while (temp->next != NULL) {
temp = temp->next;
}
cout << "reverse: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
void fanzhuan()
{
if (head == NULL)
{
return;
}
stack<struct node*>s;
node* temp = head;
while (temp!=NULL)
{
s.push(temp);
temp = temp->next;
}
temp = s.top();
head = temp;
s.pop();
while(!s.empty())
{
temp->next = s.top();
s.pop();
temp = temp->next;
}
}
int main() {
head = NULL;
insertathead(2);
print();
reverprint();
insertathead(4);
print();
reverprint();
insertathead(6);
print();
reverprint();
return 0;
}