数据结构与算法学习笔记

笔记目录

单链表插入算法:
在这里插入图片描述
双向链表插入算法:
在这里插入图片描述
双向链表删除元素的操作:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • [思考题] 单链表的转置:
    例:head -> A -> B -> C -> D
    解题思路:依次将头节点之后的一位元素删除并插入到尾节点之后,即①head -> B -> C -> D -> A;②head -> C -> D -> B -> A;③head -> D -> C -> B -> A

  • 借楼:
    在这里插入图片描述

  • 堆与堆的构建:https://www.jianshu.com/p/d67d68a11c93

  • 霍夫曼编码:https://www.sohu.com/a/454644669_115128

  • 回路

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct TreeNode {
    char val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
class Solution {
private:
		char a[30] = {};
		char b[30] = {};
		int controlNum = 0;
public:
	void input(){
		cin>>a;
		cin>>b;
	}
	void createTree(TreeNode* result, char array[]){
		if (array[controlNum] == '#'){
//			cout<<controlNum<<"->"<<"#"<<" ";
			result == nullptr;
			controlNum ++;
			return;
		}
		result->val = array[controlNum];
//		cout<<controlNum<<"->"<<result->val<<" ";
		if (++ controlNum <= 30) {
			this->createTree(result->left = new TreeNode(), array);
			this->createTree(result->right = new TreeNode(), array);
		}
	}
	TreeNode* initA() {
		TreeNode* A = new TreeNode();
		createTree(A, a);
		controlNum = 0;
		return A;
	}
	TreeNode* initB() {
		TreeNode* B = new TreeNode();
		createTree(B, b);
		controlNum = 0;
		return B;
	}
    bool isSameTree(TreeNode* root, TreeNode* subRoot){
        if(root == nullptr && subRoot == nullptr){
            return true;
        }
        else if(root == nullptr || subRoot == nullptr){
            return false;
        }
        else if(root->val != subRoot->val){
            return false;
        }
        else{
            return isSameTree(root->left, subRoot->left) && isSameTree(root->right, subRoot->right);
        }
    }
    bool dfs(TreeNode* root, TreeNode* subRoot){
        if(root == nullptr){
            return false;
        }
        // 当前两棵树是否相同,如果不相同,判断root左子树或者右子树是否和subRoot相同
        return isSameTree(root, subRoot) || dfs(root->left, subRoot) || dfs(root->right, subRoot);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        return dfs(root, subRoot);
    }
};

int main() {
	Solution solution;
	solution.input();
	TreeNode* A = solution.initA();
	TreeNode* B = solution.initB();
	bool result = solution.isSubtree(A, B);
//	cout<<result;
	if(result == 1){
		cout<<"yes";
	}
	else{
		cout<<"no";
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 二叉树建立:
#include<stdio.h>
#include<stdlib.h>

typedef struct BinaryTreeNode
{
    int data;
    struct BinaryTreeNode *Left;
    struct BinaryTreeNode *Right;
}Node;
 
 
//创建二叉树,顺序依次为中间节点->左子树->右子树
Node* createBinaryTree()
{
    Node *p;
    int ch;
	printf("输入data");
    scanf("%d",&ch);
    if(ch == 0)     //如果到了叶子节点,接下来的左、右子树分别赋值为0
    {
        p = NULL;
    }
    else
    {
        p = (Node*)malloc(sizeof(Node));
        p->data = ch;
        p->Left  = createBinaryTree();  //递归创建左子树
        p->Right = createBinaryTree();  //递归创建右子树
    }
    return p;
}
 
//先序遍历
void preOrderTraverse(Node* root)
{
    if( root )
    {
        printf("%d",root->data);
        preOrderTraverse(root->Left);
        preOrderTraverse(root->Right);
    }
}
 
//中序遍历
void inOrderTraverse(Node* root)
{
    if( root )
    {
        inOrderTraverse(root->Left);
        printf("%d",root->data);
        inOrderTraverse(root->Right);
    }
}
 
//后序遍历
void lastOrderTraverse(Node* root)
{
    if( root )
    {
        lastOrderTraverse(root->Left);
        lastOrderTraverse(root->Right);
        printf("%d",root->data);
    }
}
 
//二叉树节点总数目
int Nodenum(Node* root)
{
    if(root == NULL)
    {
        return 0;
    }
    else
    {
        return 1+Nodenum(root->Left)+Nodenum(root->Right);
    }
}

 
//二叉树叶子节点数
int Leafnum(Node* root)
{
    if(!root)
    {
        return 0;
    }
    else if(  (root->Left == NULL) && (root->Right == NULL) )
    {
        return 1;
    }
    else
    {
        return  (Leafnum(root->Left) + Leafnum(root->Right)) ;
    }
}
 
 
int main()
{
	int i;
    Node *root = NULL;
    root = createBinaryTree();
    printf("二叉树建立成功");
	printf("\n");
    preOrderTraverse(root);
	printf("\n");
    inOrderTraverse(root);
	printf("\n");
    lastOrderTraverse(root);
	printf("\n");
    i = Leafnum(root);
	printf("%d",i);
	printf("\n");
	i = Nodenum(root);
	printf("%d",i);
    return 0;
}

2022.05.07
今天看到基数排序,原算法是从低位向高位计算的,感觉这样子有点繁琐:
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个代码我看不懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值