@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: array.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: linked_list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: my_list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: leetcode_two_sum.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: space_complexity_types.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: time_complexity_types.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: worst_best_time_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: binary_search.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: hashing_search.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: linear_search.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: bubble_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: insertion_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: merge_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: quick_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: array_queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: array_stack.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: deque.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: linkedlist_queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: linkedlist_stack.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: stack.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: binary_search_tree.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: binary_tree.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: binary_tree_bfs.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* File: binary_tree_dfs.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* @brief Definition for a singly-linked list node
|
||||
*
|
||||
*/
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a linked list with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return ListNode*
|
||||
*/
|
||||
ListNode* vectorToLinkedList(vector<int> list) {
|
||||
ListNode *dum = new ListNode(0);
|
||||
ListNode *head = dum;
|
||||
for (int val : list) {
|
||||
head->next = new ListNode(val);
|
||||
head = head->next;
|
||||
}
|
||||
return dum->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a list node with specific value from a linked list
|
||||
*
|
||||
* @param head
|
||||
* @param val
|
||||
* @return ListNode*
|
||||
*/
|
||||
ListNode* getListNode(ListNode *head, int val) {
|
||||
while (head != nullptr && head->val != val) {
|
||||
head = head->next;
|
||||
}
|
||||
return head;
|
||||
}
|
@ -0,0 +1,197 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
|
||||
class PrintUtil {
|
||||
public:
|
||||
/**
|
||||
* @brief Find an element in a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param vec
|
||||
* @param ele
|
||||
* @return int
|
||||
*/
|
||||
template <typename T>
|
||||
static int vecFind(const vector<T>& vec, T ele) {
|
||||
int j = INT_MAX;
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == ele) {
|
||||
j = i;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Concatenate a vector with a delim
|
||||
*
|
||||
* @tparam T
|
||||
* @param delim
|
||||
* @param vec
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string strJoin(const string& delim, const T& vec) {
|
||||
ostringstream s;
|
||||
for (const auto& i : vec) {
|
||||
if (&i != &vec[0]) {
|
||||
s << delim;
|
||||
}
|
||||
s << i;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Repeat a string for n times
|
||||
*
|
||||
* @param str
|
||||
* @param n
|
||||
* @return string
|
||||
*/
|
||||
static string strRepeat(string str, int n) {
|
||||
ostringstream os;
|
||||
for(int i = 0; i < n; i++)
|
||||
os << str;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the Vector String object
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string getVectorString(vector<T> &list) {
|
||||
return "[" + strJoin(", ", list) + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVector(vector<T> &list) {
|
||||
cout << getVectorString(list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector matrix
|
||||
*
|
||||
* @tparam T
|
||||
* @param matrix
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "[" << '\n';
|
||||
for (vector<T> &list : matrix)
|
||||
cout << " " + getVectorString(list) + "," << '\n';
|
||||
cout << "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a linked list
|
||||
*
|
||||
* @param head
|
||||
*/
|
||||
static void printLinkedList(ListNode *head) {
|
||||
vector<int> list;
|
||||
while (head != nullptr) {
|
||||
list.push_back(head->val);
|
||||
head = head->next;
|
||||
}
|
||||
|
||||
cout << strJoin(" -> ", list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
struct Trunk {
|
||||
Trunk *prev;
|
||||
string str;
|
||||
Trunk(Trunk *prev, string str) {
|
||||
this->prev = prev;
|
||||
this->str = str;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper function to print branches of the binary tree
|
||||
*
|
||||
* @param p
|
||||
*/
|
||||
static void showTrunks(Trunk *p) {
|
||||
if (p == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p->prev);
|
||||
cout << p->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The interface of the tree printer
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void printTree(TreeNode *root) {
|
||||
printTree(root, nullptr, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
static void printTree(TreeNode *root, Trunk *prev, bool isLeft) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
string prev_str = " ";
|
||||
Trunk *trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root->right, trunk, true);
|
||||
|
||||
if (!prev) {
|
||||
trunk->str = "———";
|
||||
}
|
||||
else if (isLeft) {
|
||||
trunk->str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else {
|
||||
trunk->str = "\\———";
|
||||
prev->str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
cout << " " << root->val << endl;
|
||||
|
||||
if (prev) {
|
||||
prev->str = prev_str;
|
||||
}
|
||||
trunk->str = " |";
|
||||
|
||||
printTree(root->left, trunk, false);
|
||||
}
|
||||
};
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Definition for a binary tree node
|
||||
*
|
||||
*/
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a binary tree with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return TreeNode*
|
||||
*/
|
||||
TreeNode* vectorToTree(vector<int> list) {
|
||||
TreeNode *root = new TreeNode(list[0]);
|
||||
queue<TreeNode*> que;
|
||||
que.emplace(root);
|
||||
int i = 1;
|
||||
while(!que.empty()) {
|
||||
TreeNode *node = que.front();
|
||||
que.pop();
|
||||
if(list[i] != INT_MAX) {
|
||||
node->left = new TreeNode(list[i]);
|
||||
que.emplace(node->left);
|
||||
}
|
||||
i++;
|
||||
if(list[i] != INT_MAX) {
|
||||
node->right = new TreeNode(list[i]);
|
||||
que.emplace(node->right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a tree node with specific value in a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param val
|
||||
* @return TreeNode*
|
||||
*/
|
||||
TreeNode* getTreeNode(TreeNode *root, int val) {
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
if (root->val == val)
|
||||
return root;
|
||||
TreeNode *left = getTreeNode(root->left, val);
|
||||
TreeNode *right = getTreeNode(root->right, val);
|
||||
return left != nullptr ? left : right;
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <queue>
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <set>
|
||||
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
#include "PrintUtil.hpp"
|
||||
|
||||
using namespace std;
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: linked_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: my_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: leetcode_two_sum.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: space_complexity_types.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: time_complexity_types.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: worst_best_time_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: binary_search.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: hashing_search.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: linear_search.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: bubble_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: insertion_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: merge_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: quick_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: array_queue.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: array_stack.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: deque.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: linkedlist_queue.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: linkedlist_stack.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: queue.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: stack.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: binary_search_tree.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: binary_tree.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: binary_tree_bfs.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,10 @@
|
||||
'''
|
||||
File: binary_tree_dfs.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
@ -0,0 +1,9 @@
|
||||
import copy
|
||||
import math
|
||||
import random
|
||||
import functools
|
||||
import collections
|
||||
from typing import List
|
||||
from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node
|
||||
from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node
|
||||
from .print_util import print_matrix, print_linked_list, print_tree
|
@ -0,0 +1,82 @@
|
||||
'''
|
||||
File: binary_tree.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import collections
|
||||
|
||||
class TreeNode:
|
||||
"""Definition for a binary tree node
|
||||
"""
|
||||
def __init__(self, val=0, left=None, right=None):
|
||||
self.val = val
|
||||
self.left = left
|
||||
self.right = right
|
||||
|
||||
def list_to_tree(arr):
|
||||
"""Generate a binary tree with a list
|
||||
|
||||
Args:
|
||||
arr ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
if not arr:
|
||||
return
|
||||
i = 1
|
||||
root = TreeNode(int(arr[0]))
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
while queue:
|
||||
node = queue.popleft()
|
||||
if arr[i] != None:
|
||||
node.left = TreeNode(int(arr[i]))
|
||||
queue.append(node.left)
|
||||
i += 1
|
||||
if arr[i] != None:
|
||||
node.right = TreeNode(int(arr[i]))
|
||||
queue.append(node.right)
|
||||
i += 1
|
||||
return root
|
||||
|
||||
def tree_to_list(root):
|
||||
"""Serialize a tree into an array
|
||||
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
if not root: return []
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
res = []
|
||||
while queue:
|
||||
node = queue.popleft()
|
||||
if node:
|
||||
res.append(node.val)
|
||||
queue.append(node.left)
|
||||
queue.append(node.right)
|
||||
else: res.append(None)
|
||||
return res
|
||||
|
||||
def get_tree_node(root, val):
|
||||
"""Get a tree node with specific value in a binary tree
|
||||
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
val ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
if not root:
|
||||
return
|
||||
if root.val == val:
|
||||
return root
|
||||
left = get_tree_node(root.left, val)
|
||||
right = get_tree_node(root.right, val)
|
||||
return left if left else right
|
@ -0,0 +1,57 @@
|
||||
'''
|
||||
File: linked_list.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
class ListNode:
|
||||
"""Definition for a singly-linked list node
|
||||
"""
|
||||
def __init__(self, val=0, next=None):
|
||||
self.val = val
|
||||
self.next = next
|
||||
|
||||
def list_to_linked_list(arr):
|
||||
"""Generate a linked list with a list
|
||||
|
||||
Args:
|
||||
arr ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
dum = head = ListNode(0)
|
||||
for a in arr:
|
||||
node = ListNode(a)
|
||||
head.next = node
|
||||
head = head.next
|
||||
return dum.next
|
||||
|
||||
def linked_list_to_list(head):
|
||||
"""Serialize a linked list into an array
|
||||
|
||||
Args:
|
||||
head ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
arr = []
|
||||
while head:
|
||||
arr.append(head.val)
|
||||
head = head.next
|
||||
return arr
|
||||
|
||||
def get_list_node(head, val):
|
||||
"""Get a list node with specific value from a linked list
|
||||
|
||||
Args:
|
||||
head ([type]): [description]
|
||||
val ([type]): [description]
|
||||
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
while head and head.val != val:
|
||||
head = head.next
|
||||
return head
|
@ -0,0 +1,73 @@
|
||||
'''
|
||||
File: print_util.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
from .binary_tree import TreeNode, tree_to_list
|
||||
from .linked_list import ListNode, linked_list_to_list
|
||||
|
||||
def print_matrix(mat):
|
||||
"""Print a matrix
|
||||
|
||||
Args:
|
||||
mat ([type]): [description]
|
||||
"""
|
||||
pstr = []
|
||||
for arr in mat:
|
||||
pstr.append(' ' + str(arr))
|
||||
|
||||
print('[\n' + ',\n'.join(pstr) + '\n]')
|
||||
|
||||
def print_linked_list(head):
|
||||
"""Print a linked list
|
||||
|
||||
Args:
|
||||
head ([type]): [description]
|
||||
"""
|
||||
arr = linked_list_to_list(head)
|
||||
print(' -> '.join([str(a) for a in arr]))
|
||||
|
||||
|
||||
class Trunk:
|
||||
def __init__(self, prev=None, str=None):
|
||||
self.prev = prev
|
||||
self.str = str
|
||||
|
||||
def showTrunks(p):
|
||||
if p is None:
|
||||
return
|
||||
showTrunks(p.prev)
|
||||
print(p.str, end='')
|
||||
|
||||
def print_tree(root, prev=None, isLeft=False):
|
||||
"""Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
prev ([type], optional): [description]. Defaults to None.
|
||||
isLeft (bool, optional): [description]. Defaults to False.
|
||||
"""
|
||||
if root is None:
|
||||
return
|
||||
|
||||
prev_str = ' '
|
||||
trunk = Trunk(prev, prev_str)
|
||||
print_tree(root.right, trunk, True)
|
||||
|
||||
if prev is None:
|
||||
trunk.str = '———'
|
||||
elif isLeft:
|
||||
trunk.str = '/———'
|
||||
prev_str = ' |'
|
||||
else:
|
||||
trunk.str = '\———'
|
||||
prev.str = prev_str
|
||||
|
||||
showTrunks(trunk)
|
||||
print(' ' + str(root.val))
|
||||
if prev:
|
||||
prev.str = prev_str
|
||||
trunk.str = ' |'
|
||||
print_tree(root.left, trunk, False)
|
Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 53 KiB |
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |