Acy夕拾算法 Week1_day5


知识点参考:代码随想录-二叉树理论基础
· 父节点的数组下标是 i,那么它的左孩子就是 i * 2 +1,右孩子就是 i * 2 +2。
· 深度优先:有前中后序遍历:这里前中后,其实指的就是 中间节点的遍历顺序(位置)
·· 前序遍历:中左右
·· 中序遍历:左中右
·· 后序遍历: 右中
···· 从“最”开始:从最中开始,从最左开始;左右顺序不变;所有的 中、左都要遍历完,才能开始 左、中/右;左右是指图中的左右,即当中在图中的最左时它为左,eg,中序的5

代码随想录的图
递归:1.参数和返回值;2.终止条件;3.单层逻辑。

144. 二叉树的前序遍历

/*
整理思路:
前序:中左右;
1.参数:当前节点,数组结果;返回空;2.遍历到当前为空,结束;3.中左右
void preT(TreeNode* cur, vector& res)
*/

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        preT(root, res);
        return res;
    }
    void preT(TreeNode* cur, vector<int>& res)
    {
        if(cur == NULL)    return ;
        res.push_back(cur->val);
        preT(cur->left, res);
        preT(cur->right, res);
    }
};

94. 二叉树的中序遍历

/*
我的思路:
左中右,把左全部遍历完,再中再全部左…
*/

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        midT(root, res);
        return res;
    }
    void midT(TreeNode* cur, vector<int>& res)
    {
        if(cur == NULL) return;
        midT(cur->left, res);
        res.push_back(cur->val);
        midT(cur->right, res);
    }
};

104. 二叉树的最大深度

/*
整理思路:
深度优先搜索:一直向下,返回左右最大+1(根),直到空返回0。
广度优先搜索:
*/

深度优先搜索

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == NULL)    return 0;
        return max( maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};

广度优先搜索

周日补

### Java实现Delaunay三角剖分算法 Delaunay三角剖分是一种在二维空间中对点集进行三角化的方法,其核心特性是:任意两个点之间的边不会与其他边相交,并且每个三角形的外接圆内不包含其他点。这种特性使得Delaunay三角剖分广泛应用于计算机图形学、地理信息系统(GIS)等领域。 在Java中实现Delaunay三角剖分,通常可以采用**增量插入法**(Bowyer-Watson算法),该算法通过逐步插入点并删除与新点冲突的三角形来维护Delaunay性质。以下是一个基于该算法的Java实现示例。 --- ### Java代码实现 ```java import java.util.*; class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Point)) return false; Point other = (Point) obj; return Double.compare(other.x, x) == 0 && Double.compare(other.y, y) == 0; } @Override public int hashCode() { return Objects.hash(x, y); } } class Triangle { Point a, b, c; Set<Edge> edges = new HashSet<>(); Triangle(Point a, Point b, Point c) { this.a = a; this.b = b; this.c = c; edges.add(new Edge(a, b)); edges.add(new Edge(b, c)); edges.add(new Edge(c, a)); } boolean circumcircleContains(Point p) { double abx = a.x - b.x; double aby = a.y - b.y; double acx = a.x - c.x; double acy = a.y - c.y; double d = 2 * (abx * (a.y - c.y) - acx * aby); double cx_ = ( (a.x * a.x - b.x * b.x) * (a.y - c.y) - (a.x * a.x - c.x * c.x) * aby ) / d; double cy_ = ( (a.y * a.y - b.y * b.y) * (abx) - (a.y * a.y - c.y * c.y) * acx ) / d; double r = Math.sqrt((a.x - cx_) * (a.x - cx_) + (a.y - cy_) * (a.y - cy_)); double px = p.x - cx_; double py = p.y - cy_; return (px * px + py * py) <= r * r + 1e-8; } } class Edge { Point p1, p2; Edge(Point p1, Point p2) { if (p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y)) { this.p1 = p1; this.p2 = p2; } else { this.p1 = p2; this.p2 = p1; } } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Edge)) return false; Edge other = (Edge) obj; return p1.equals(other.p1) && p2.equals(other.p2); } @Override public int hashCode() { return Objects.hash(p1, p2); } } public class DelaunayTriangulation { private static List<Triangle> triangulate(List<Point> points) { List<Triangle> triangles = new ArrayList<>(); Point p1 = new Point(-1000, -1000); Point p2 = new Point(1000, -1000); Point p3 = new Point(0, 1000); Triangle superTriangle = new Triangle(p1, p2, p3); triangles.add(superTriangle); for (Point p : points) { Set<Edge> polygon = new HashSet<>(); List<Triangle> badTriangles = new ArrayList<>(); for (Triangle t : triangles) { if (t.circumcircleContains(p)) { badTriangles.add(t); for (Edge e : t.edges) { boolean shared = false; for (Triangle other : triangles) { if (other != t && other.edges.contains(e)) { shared = true; break; } } if (!shared) { polygon.add(e); } } } } for (Edge e : polygon) { triangles.add(new Triangle(e.p1, e.p2, p)); } triangles.removeAll(badTriangles); } // 移除包含超级三角形顶点的三角形 triangles.removeIf(t -> t.a == p1 || t.a == p2 || t.a == p3 || t.b == p1 || t.b == p2 || t.b == p3 || t.c == p1 || t.c == p2 || t.c == p3); return triangles; } public static void main(String[] args) { List<Point> points = new ArrayList<>(); points.add(new Point(0.0, 0.0)); points.add(new Point(1.0, 0.0)); points.add(new Point(0.5, 1.0)); points.add(new Point(0.5, 0.5)); List<Triangle> result = triangulate(points); for (Triangle t : result) { System.out.println("Triangle: (" + t.a.x + "," + t.a.y + ") - (" + t.b.x + "," + t.b.y + ") - (" + t.c.x + "," + t.c.y + ")"); } } } ``` --- ### 代码说明 - **Point类**:表示二维空间中的点。 - **Edge类**:表示三角形的边,用于边去重。 - **Triangle类**:表示一个三角形,并包含判断点是否在三角形外接圆内的方法。 - **triangulate方法**:使用Bowyer-Watson算法进行增量插入,构建Delaunay三角剖分。 - **main方法**:提供测试点集并输出三角剖分结果。 该实现能够处理任意二维点集,并输出符合Delaunay条件的三角形集合。算法的时间复杂度为O(n²),适用于中等规模的数据集。 --- ### 性能优化与扩展 对于大规模点集,可以引入**分治法**或**平面扫描法**来提升性能。此外,该算法也可以扩展到三维空间,形成**三维Delaunay三角剖分**(Tetrahedralization),但实现复杂度显著增加[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值