簡體   English   中英

來自文本文件的通用二叉樹

[英]Generic Binary tree from Text File

我正在嘗試制作一個從文本文件創建的通用二叉樹。 我正在尋找最好的方法並且正確地這樣做並且已經嘗試過。 在我的第一種方法中,BinaryTree(Scanner) 是從綁定到文件的掃描器創建樹的方法。 然后 leftInsert() 向左插入,rightInsert() 向右插入,我希望這些也是正確的。 但我不確定如何正確制作文本文件以便制作樹。

文本文件:

    A
    B
    C
    D 
    E
    F        

通用二叉樹類:

import java.util.*;
import java.io.*;

import org.w3c.dom.Node;

private class Nodes<E> {

    public E value;
    public Nodes<E> leftChild;
    public Nodes<E> rightChild;

    public Nodes(E value) {
        leftChild = null;
        rightChild = null;
        this.value = value;
    }
}

public class BinaryTree<E> {

private Node root;


// biggest issue here
public BinaryTree(Scanner){
// reads a file to create a tree
}

public void leftInsert(Nodes<E> Node, E value) {

    if ((value).compareTo(Node.value) < 0) {

        if (Node.leftChild == null) {

            Node.leftChild = new Nodes<E>(value);

        } else {

            leftInsert(Node.leftChild, value);

        }
    }

}

public void rightInsert(Nodes<E> Node, E value) {

    if ((value).compareTo(Node.value) >= 0) {

        if (Node.rightChild == null) {

            Node.rightChild = new Nodes<E>(value);

        } else {

            rightInsert(Node.rightChild, value);

        }
    }

}
}

好極了! :D

import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;
import java.util.regex.*;
public class Tree {
    public static void main(String[] args) {
        (new Tree()).build();
    }
    private ArrayList<Node> nodes;
    private ArrayList<Link> links;
    public void build() {
        nodes = new ArrayList<Node>();
        links = new ArrayList<Link>();
        // Read in nodes and links
/*
    A
   / \
  B   C
 / \
D   E
   / \
  F   G
*/
        String input = "    A\n   / \\\n  B   C\n / \\\nD   E\n   / \\\n  F   G";
        int y = 0;
        for (String line : input.split("\n")) {
            Matcher matcher = Pattern.compile("[A-Z]").matcher(line);
            while (matcher.find()) {
                Node node = new Node();
                node.name = matcher.group();
                node.x = matcher.start();
                node.y = y;
                nodes.add(node);
            }
            matcher = Pattern.compile("[/\\\\]").matcher(line);
            while (matcher.find()) {
                Link link = new Link();
                link.x = matcher.start();
                link.y = y;
                links.add(link);
            }
            ++y;
        }
        // Join the nodes
        for (Node node : nodes) {
            Node left = getNodeAt(node.x - 2, node.y + 2);
            if (left != null && hasLinkAt(node.x - 1, node.y + 1))
                node.left = left;
            Node right = getNodeAt(node.x + 2, node.y + 2);
            if (right != null && hasLinkAt(node.x + 1, node.y + 1))
                node.right = right;
        }
        // Great success!
        nodes.get(0).print(0);
    }
    private Node getNodeAt(int x, int y) {
        for (Node node : nodes)
            if (node.x == x && node.y == y)
                return node;
        return null;
    }
    private boolean hasLinkAt(int x, int y) {
        for (Link link : links)
            if (link.x == x && link.y == y)
                return true;
        return false;
    }
    private class Node {
        private int x, y;
        private String name;
        private Node left, right;
        public void print(int indent) {
            String indentString = "";
            for (int i = 0; i < indent; ++i, indentString += "    ") {}
            System.out.println(indentString + name + ": {");
            if (left != null)
                left.print(indent + 1);
            if (right != null)
                right.print(indent + 1);
            System.out.println(indentString + "}");
        }
    }
    private class Link {
        private int x, y;
    }
}

證明取得巨大成功的輸出:

A: {
    B: {
        D: {
        }
        E: {
            F: {
            }
            G: {
            }
        }
    }
    C: {
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM