您的位置:首页 > 理论基础 > 数据结构算法

数据结构-树

2018-03-14 11:39 197 查看
父节点表示法:让每个节点“记住”它的父节点的索引。import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2018/3/14.
*/
public class TreeParent<E> {

public static class Node<T> {
T data;
int parent;

public Node() {

}

public Node(T data) {
this.data = data;
}

public Node(T data, int parent) {
this.data = data;
this.parent = parent;
}

public String toString() {
return "TreeParent$Node[data=" + data + ", parent=" + parent + "]";
}
}

private final int DEFAULT_TREE_SIZE = 100;

private int treeSize = 0;

//保存树里的所有节点
private Node<E>[] nodes;

private int nodeNums;

public TreeParent(E data) {
treeSize = DEFAULT_TREE_SIZE;
nodes = new Node[treeSize];
nodes[0] = new Node<>(data, -1);
nodeNums++;
}

public TreeParent(E data, int treeSize) {
this.treeSize = treeSize;
nodes = new Node[treeSize];
nodes[0] = new Node<>(data, -1);
nodeNums++;
}

public void addNode(E data, Node parent) {
for (int i = 0; i < treeSize; i++) {
if (nodes[i] == null) {
nodes[i] = new Node(data, pos(parent));
nodeNums++;
return;
}
}
throw new RuntimeException("该树已满,无法添加新节点");
}

public boolean empty() {
return nodes[0] == null;
}

public Node<E> root() {
return nodes[0];
}

public Node<E> parent(Node node) {
return nodes[node.parent];
}

public List<Node<E>> children(Node parent) {
List<Node<E>> list = new ArrayList<>();
for (int i = 0; i < treeSize; i++) {
if (nodes[i] != null && nodes[i].parent == pos(parent)) {
list.add(nodes[i]);
}
}
return list;
}

public int deep() {
int max = 0;
for (int i = 0; i < treeSize && nodes[i] != null; i++) {
int def = 1;
int m = nodes[i].parent;
while (m != -1 && nodes[m] != null) {
m = nodes[m].parent;
def++;
}
if (max < def) {
max = def;
}
}
return max;
}

public int pos(Node node) {
for (int i = 0; i < treeSize; i++) {
if (nodes[i] == node) {
return i;
}
}
return -1;
}

public String toString() {
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[TreeParent:");
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] == null) {
break;
}
if (i > 0) {
sb.append(", ");
}
sb.append(nodes[i]);
}
sb.append("]");
return sb.toString();
}
}

public static void main(String[] args) {
TreeParent<String> tp = new TreeParent<String>("root");
System.out.println(tp);
System.out.println(tp.deep());
//获取唯一根节点
Node root = tp.root();
tp.addNode("1-1", root);
tp.addNode("1-2", root);
tp.addNode("1-3", root);
System.out.println(tp);
System.out.println(tp.deep());
//获取刚才添加的3个子节点
List<Node<String>> subRoot = tp.children(root);
System.out.println(subRoot);
tp.addNode("2-1-1", subRoot.get(0));
tp.addNode("2-1-2", subRoot.get(0));
tp.addNode("2-2-1", subRoot.get(1));
tp.addNode("2-2-2", subRoot.get(1));
tp.addNode("2-2-3", subRoot.get(1));
tp.addNode("2-3-1", subRoot.get(2));
tp.addNode("2-3-2", subRoot.get(2));
tp.addNode("2-3-3", subRoot.get(2));
tp.addNode("2-3-4", subRoot.get(2));
System.out.println(tp);
System.out.println(tp.deep());
}
}子节点链表表示法:让父节点“记住”它的所有子节点import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2018/3/14.
*/
public class TreeChild<E> {

private static class SonNode {
//记录当前节点的位置
private int pos;
private SonNode next;

public SonNode(int pos, SonNode next) {
this.pos = pos;
this.next = next;
}
}

public static class Node<T> {
T data;
// 记录第一个子节点
SonNode first;

public Node(T data) {
this.data = data;
this.first = null;
}

public String toString() {
if (first != null) {
return "TreeChild$Node[data=" + data + ", first=" + first.pos + "]";
} else {
return "TreeChild$Node[data=" + data + ", first=-1]";
}
}
}

private final int DEFAULT_TREE_SIZE = 100;
private int treeSize = 0;

//使用Node[]数组记录该树里的所有节点
private Node<E>[] nodes;
//记录节点数
private int nodeNums;

public TreeChild(E data) {
treeSize = DEFAULT_TREE_SIZE;
nodes = new Node[treeSize];
nodes[0] = new Node<>(data);
nodeNums++;
}

public TreeChild(E data, int treeSize) {
this.treeSize = treeSize;
nodes = new Node[treeSize];
nodes[0] = new Node<>(data);
nodeNums++;
}

//为指定节点添加子节点
public void addNode(E data, Node parent) {
for (int i = 0; i < treeSize; i++) {
if (nodes[i] == null) {
nodes[i] = new Node<>(data);
if (parent.first == null) {
parent.first = new SonNode(i, null);
} else {
SonNode next = parent.first;
while (next.next != null) {
next = next.next;
}
next.next = new SonNode(i, null);
}
nodeNums++;
return;
}
}
throw new RuntimeException("该树已满,无法添加新节点");
}

public boolean empty() {
return nodes[0] == null;
}

public Node<E> root() {
return nodes[0];
}

//返回指定节点(非叶子节点)的所有子节点
public List<Node<E>> children(Node parent) {
List<Node<E>> list = new ArrayList<>();
SonNode next = parent.first;
while (next != null) {
list.add(nodes[next.pos]);
next = next.next;
}
return list;
}

//返回指定节点(非叶子节点)的第index个子节点
public Node<E> child(Node parent, int index) {
SonNode next = parent.first;
for (int i = 0; next != null; i++) {
if (index == i) {
return nodes[next.pos];
}
next = next.next;
}
return null;
}

public int deep() {
return deep(root());
}

private int deep(Node node) {
if (node.first == null) {
return 1;
} else {
int max = 0;
SonNode next = node.first;
while (next != null) {
int tmp = deep(nodes[next.pos]);
if (tmp > max) {
max = tmp;
}
next = next.next;
}
System.out.println(max);
return max + 1;
}
}

public int pos(Node node) {
for (int i = 0; i < treeSize; i++) {
if (nodes[i] == node) {
return i;
}
}
return -1;
}

public String toString() {
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[TreeChild:");
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] == null) {
break;
}
if (i > 0) {
sb.append(", ");
}
sb.append(nodes[i]);
}
sb.append("]");
return sb.toString();
}
}

public static void main(String[] args) {
//创建一个数组为100 的空间存储 Nodes,数组第一位元素为 root
TreeChild<String> tp = new TreeChild<String>("root");
Node<String> root = tp.root();
System.out.println(root);
tp.addNode("1-1", root);
tp.addNode("1-2", root);
tp.addNode("1-3", root);
System.out.println(tp);
List<Node<String>> children = tp.children(root);
tp.addNode("2-1-1", children.get(0));
tp.addNode("2-1-2", children.get(0));
System.out.println(tp);
System.out.println(tp.deep());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: