java实现而叉树

/**
*java实现二叉树
*/
public class BiTree {
Scanner scan=new Scanner(System.in);
/**
* 定义树的节点类,可以单独创建该类
* */
class Node{

    private char data;

    private Node lchild,rchild;

    public Node(){

    }

    public void setData(char data){
        this.data=data;
    }
}

/**
 * 空节点用#表示
 * 
 * */
public Node createBiTree(){
    Node T = null;
    char ch;
    ch=scan.next().charAt(0);
    if(ch=='#'){
        T=null;
    }
    else{
        T=new Node();
        T.setData(ch);
        T.lchild=createBiTree();
        T.rchild=createBiTree();
    }
    return T;
}

public void preSearch(Node root){
    if(root!=null){
        System.out.print(root.data+" ");
        preSearch(root.lchild);
        preSearch(root.rchild);
    }
}

public void preNoRecursion(Node root){
    Stack<Node>nodeStack=new Stack<Node>();
    Node tree=root;
    while(tree!=null||nodeStack.size()>0){
        while(tree!=null){
            System.out.print(tree.data+" ");
            nodeStack.push(tree);
            tree=tree.lchild;
        }
        if(nodeStack.size()>0){
            tree=nodeStack.pop();
            tree=tree.rchild;
        }
    }

}



public void inSearch(Node root){
    if(root!=null){
        inSearch(root.lchild);
        System.out.print(root.data+" ");
        inSearch(root.rchild);
    }
}

public void inNoRecursion(Node root){
    Stack<Node>nodeStack=new Stack<Node>();
    Node tree=root;
    while(tree!=null||nodeStack.size()>0){
        while(tree!=null){
            nodeStack.push(tree);
            tree=tree.lchild;
        }
        if(nodeStack.size()>0){
            tree=nodeStack.pop();
            System.out.print(tree.data+" ");
            tree=tree.rchild;
        }
    }
}


public void postSearch(Node root){
    if(root!=null){
        postSearch(root.lchild);
        postSearch(root.rchild);
        System.out.print(root.data+" ");
    }
}

public void postNoRecursion(Node root){
    Stack<Node>treeStack=new Stack<BiTree.Node>();
    Node tree=root;
    Node node=null;
    while(tree!=null){
        for(;tree.lchild!=null;tree=tree.lchild){
            treeStack.push(tree);
        }
        while((tree!=null)&&(tree.rchild==null||tree.rchild==node)){
            System.out.print(tree.data+" ");
            node=tree;
            if(treeStack.isEmpty()){
                return;
            }
            tree=treeStack.pop();

        }
        treeStack.push(tree);
        tree=tree.rchild;

    }
}
/**
 *  求节点个数
 * */
public int nodeSize(Node root){
    if(root==null){
        return 0;
    }else{
        return 1+nodeSize(root.lchild)+nodeSize(root.rchild);
    }
}

public int height(Node root){
    if(root==null){
        return 0;
    }else{
        int left=height(root.lchild);
        int right=height(root.rchild);
        return left<right?(right+1):(left+1);
    }
}

public static void main(String[]args){
    BiTree tree=new BiTree();
    Node root=tree.createBiTree();

    System.out.print("前序为:");
    tree.preSearch(root);
    System.out.println();

    System.out.print("非递归前序为:");
    tree.preNoRecursion(root);
    System.out.println();

    System.out.print("中序为:");
    tree.inSearch(root);
    System.out.println();

    System.out.print("非递归中序为:");
    tree.inNoRecursion(root);
    System.out.println();

    System.out.print("后序为:");
    tree.postSearch(root);
    System.out.println();
    System.out.print("非递归后序为:");
    tree.postNoRecursion(root);
    System.out.println();
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
四叉是一种空间索引结构,它将二维空间划分为四个象限,每个象限可以再继续划分为四个象限,以此类推,直到达到某个终止条件。 在Java中,可以使用以下代码实现四叉空间索引: 1. 定义QuadTree类 public class QuadTree { private final int MAX_CAPACITY = 4; // 每个节点最大容量 private final int MAX_LEVEL = 5; // 最大深度 private int level; // 当前深度 private List<Point> points; // 当前节点包含的点 private Rectangle boundary; // 当前节点的边界 private QuadTree[] children; // 子节点 public QuadTree(Rectangle boundary, int level) { this.boundary = boundary; this.level = level; points = new ArrayList<>(); children = null; } // 插入点 public boolean insert(Point point) { if (!boundary.contains(point)) { return false; } if (points.size() < MAX_CAPACITY && children == null) { points.add(point); return true; } if (children == null) { split(); } for (QuadTree child : children) { if (child.insert(point)) { return true; } } return false; } // 分裂节点 private void split() { children = new QuadTree[4]; int subWidth = boundary.width / 2; int subHeight = boundary.height / 2; int x = boundary.x; int y = boundary.y; children[0] = new QuadTree(new Rectangle(x + subWidth, y, subWidth, subHeight), level + 1); children[1] = new QuadTree(new Rectangle(x, y, subWidth, subHeight), level + 1); children[2] = new QuadTree(new Rectangle(x, y + subHeight, subWidth, subHeight), level + 1); children[3] = new QuadTree(new Rectangle(x + subWidth, y + subHeight, subWidth, subHeight), level + 1); for (Point point : points) { for (QuadTree child : children) { if (child.insert(point)) { break; } } } points.clear(); } // 查询某个矩形内的点 public List<Point> query(Rectangle range) { List<Point> result = new ArrayList<>(); if (!boundary.intersects(range)) { return result; } for (Point point : points) { if (range.contains(point)) { result.add(point); } } if (children != null) { for (QuadTree child : children) { result.addAll(child.query(range)); } } return result; } } 2. 定义Point和Rectangle类 public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } public class Rectangle { public int x; public int y; public int width; public int height; public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public boolean contains(Point point) { return point.x >= x && point.x <= x + width && point.y >= y && point.y <= y + height; } public boolean intersects(Rectangle range) { return !(range.x > x + width || range.x + range.width < x || range.y > y + height || range.y + range.height < y); } } 3. 使用QuadTree进行空间索引 public class Main { public static void main(String[] args) { QuadTree quadTree = new QuadTree(new Rectangle(0, 0, 100, 100), 0); quadTree.insert(new Point(10, 10)); quadTree.insert(new Point(20, 20)); quadTree.insert(new Point(30, 30)); quadTree.insert(new Point(40, 40)); quadTree.insert(new Point(50, 50)); quadTree.insert(new Point(60, 60)); quadTree.insert(new Point(70, 70)); quadTree.insert(new Point(80, 80)); quadTree.insert(new Point(90, 90)); List<Point> points = quadTree.query(new Rectangle(25, 25, 50, 50)); System.out.println(points); } } 输出结果为:[Point{x=30, y=30}, Point{x=40, y=40}, Point{x=50, y=50}, Point{x=60, y=60}, Point{x=70, y=70}],表示查询到了在矩形(25, 25, 50, 50)内的所有点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值