数据结构:树的子节点表示法--Java实现

import java.util.ArrayList;
import java.util.List;

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;
		}
	}

	// 默认树的节点数
	private final int DEFAULT_SIZE = 100;
	// 树的节点数
	private int treeSize = 0;
	// 存储树节点的节点数组
	private Node<E>[] nodes;
	// 记录的节点数
	private int nodeNum = 0;

	// 指定根节点创建树
	public TreeChild(E element) {
		treeSize = DEFAULT_SIZE;
		nodes = new Node[treeSize];
		nodes[0] = new Node<E>(element);
		nodeNum++;
	}

	// 指定跟节点以及节点个数
	public TreeChild(E element, int size) {
		this.treeSize = size;
		nodes = new Node[treeSize];
		nodes[0] = new Node<E>(element);
		nodeNum++;
	}

	// 判定树是否为空
	public boolean empty() {
		return nodes[0] == null;
	}

	// 或去树的根节点
	public Node<E> root() {
		return nodes[0];
	}

	// 获取指定节点的存储位
	public int contain(Node element) {
		for (int i = 0; i < treeSize; i++) {
			if (element == nodes[i]) {
				return i;
			}
		}
		return -1;
	}

	// 向指定节点添加子节点
	public void insert(E element, Node pareNode) {
		for (int i = 0; i < treeSize; i++) {
			if (nodes[i] == null) {
				nodes[i] = new Node<E>(element);
				if (pareNode.first == null) {
					pareNode.first = new SonNode(i, null);
				} else {
					SonNode next = pareNode.first;
					while (next.next != null) {
						next = next.next;
					}
					next.next = new SonNode(i, null);
				}
				nodeNum++;
				return;
			}
		}
		throw new RuntimeException("树已满");
	}

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

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

	// 获取树的深度
	public int deep() {
		return deep(nodes[0]);
	}

	// 获取指定节点子节点深度
	public int deep(Node node) {
		if (node.first == null) {
			return 0;
		} else {
			int max = 0;
			SonNode next = node.first;
			while (next.next != null) {
				int temp = deep(nodes[next.pos]);
				if (temp > max) {
					max = temp;
				}
				next = next.next;
			}
			return max + 1;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 基于链表实现结构 */ package dsa; public class TreeLinkedList implements Tree { private Object element;//根节点 private TreeLinkedList parent, firstChild, nextSibling;//父亲、长子及最大的弟弟 //(单节点)构造方法 public TreeLinkedList() { this(null, null, null, null); } //构造方法 public TreeLinkedList(Object e, TreeLinkedList p, TreeLinkedList c, TreeLinkedList s) { element = e; parent = p; firstChild = c; nextSibling = s; } /*---------- Tree接口中各方法的实现 ----------*/ //返回当前节点中存放的对象 public Object getElem() { return element; } //将对象obj存入当前节点,并返回此前的内容 public Object setElem(Object obj) { Object bak = element; element = obj; return bak; } //返回当前节点的父节点;对于根节点,返回null public TreeLinkedList getParent() { return parent; } //返回当前节点的长子;若没有孩子,则返回null public TreeLinkedList getFirstChild() { return firstChild; } //返回当前节点的最大弟弟;若没有弟弟,则返回null public TreeLinkedList getNextSibling() { return nextSibling; } //返回当前节点后代元素的数目,即以当前节点为根的子的规模 public int getSize() { int size = 1;//当前节点也是自己的后代 TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 size += subtree.getSize();//累加 subtree = subtree.getNextSibling();//所有孩子的后代数目 } return size;//即可得到当前节点的后代总数 } //返回当前节点的高度 public int getHeight() { int height = -1; TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 height = Math.max(height, subtree.getHeight());//在所有孩子中取最大高度 subtree = subtree.getNextSibling(); } return height+1;//即可得到当前节点的高度 } //返回当前节点的深度 public int getDepth() { int depth = 0; TreeLinkedList p = parent;//从父亲开始 while (null != p) {//依次 depth++; p = p.getParent();//访问各个真祖先 } return depth;//真祖先的数目,即为当前节点的深度 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值