1 数据结构简介
1.1 什么是数据结构
简单地说,数据结构是以某种特定的布局方式存储数据的容器。这种“布局方式”决定了 数据结构对于某些操作是高效的,而对于其他操作则是低效的。所以我们需要理解各种数据 结构,才能在处理实际问题时选取最合适的数据结构。 数据结构=逻辑结构+物理结构(顺序、链式、索引、散列) 。 逻辑结构:数据元素间抽象化的相互关系 。 物理结构:(存储结构),在计算机存储器中的存储形式。
2 数据结构逻辑分类
数据结构从逻辑上划分为三种基本类型:
2.1 线性结构
数据结构中的元素存在一对一的相互关系; 常见的线性结构: 线性表,栈,队列,串(一维数组)等。
2.2树形结构
数据结构中的元素存在一对多的相互关系; 常见树形结构: 二叉树,红黑树,B 树,哈夫曼树等。
2.3图形结构
数据结构中的元素存在多对多的相互关系; 常见图形结构: 有向图,无向图,简单图等。
3 线性结构
3.1 栈的定义
栈是一种只能从一端存取数据且遵循 “后进先出(LIFO)” 原则的线性存储结构。
3.2 实现栈容器
3.2.1 创建栈容器类
演示的代码如下:
package com.txw;
@SuppressWarnings("all")
public class MyStack <E> {
private Object[] arr;
private int stackLength = 4;
private int size;
private int index = -1;
public boolean empty(){
return false;
}
public E pop(){
return null;
}
public E push(E item){
return null;
}
public static void main(String[] args) {
}
}
如图所示:
3.2.2 实现添加元素
演示的代码如下:
public E push(E item){
this.capacity();
this.arr[++ index] = item;
this.size ++;
return item;
}
public void capacity(){
if (this.arr == null){
this.arr = new Object [stackLength];
}
if (this.size - (this.stackLength - 1) > 0){
this.stackLength = this.stackLength + (this.stackLength >> 1);
this.arr = Arrays.copyOf(this.arr,this.stackLength);
}
}
如图所示:
3.2.2 实现获取元素
演示的代码如下:
public E pop(){
if (this.index == -1){
throw new EmptyStackException();
}
size --;
return (E) this.arr[index --];
}
如图所示:
3.2.3 判断栈容器是否为空
演示的代码如下:
package com.txw;
import java.util.Arrays;
import java.util.EmptyStackException;
@SuppressWarnings("all")
public class MyStack <E> {
private Object[] arr;
private int stackLength = 4;
private int size;
private int index = -1;
public boolean empty(){
return this.size == 0;
}
public E pop(){
if (this.index == -1){
throw new EmptyStackException();
}
size --;
return (E) this.arr[index --];
}
public E push(E item){
this.capacity();
this.arr[size ++] = item;
this.size ++;
return item;
}
public void capacity(){
if (this.arr == null){
this.arr = new Object [stackLength];
}
if (this.size - (this.stackLength - 1) > 0){
this.stackLength = this.stackLength + (this.stackLength >> 1);
this.arr = Arrays.copyOf(this.arr,this.stackLength);
}
}
}
如图所示:  测试的代码如下:
package com.txw;
import java.util.Arrays;
import java.util.EmptyStackException;
@SuppressWarnings("all")
public class MyStack <E> {
private Object[] arr;
private int stackLength = 4;
private int size;
private int index = -1;
public boolean empty(){
return this.size == 0;
}
public E pop(){
if(this.index == -1){
throw new EmptyStackException();
}
this.size--;
return (E) this.arr[index --];
}
public E push(E item){
this.capacity();
this.arr[size ++] = item;
this.size ++;
return item;
}
private void capacity(){
if(this.arr == null){
this.arr = new Object[this.stackLength];
}
if(this.size - (this.stackLength-1) >= 0){
this.stackLength = this.stackLength + (this.stackLength >> 1);
this.arr = Arrays.copyOf(this.arr,this.stackLength);
}
}
public static void main(String[] args) {
MyStack<String> myStack= new MyStack<>();
myStack.push("a");
myStack.push("b");
myStack.push("c");
myStack.push("d");
myStack.push("e");
myStack.push("f");
System.out.println(myStack.size);
System.out.println(myStack.pop());
System.out.println(myStack.pop());
System.out.println(myStack.pop());
}
}
4 链表结构
4.1链表结构的定义
4.1.1 什么是链表
链表结构是由许多节点构成的,每个节点都包含两部分: 1.数据部分:保存该节点的实际数据。 2.地址部分:保存的是上一个或下一个节点的地址。
4.1.2 链表分类
1.单向链表 2.双向链表 3.双向循环链表
4.1.3 链表的特点
1…结点在存储器中的位置是任意的,即逻辑上相邻的数据元素在物理上不一定相邻。 2.访问时只能通过头或者尾指针进入链表,并通过每个结点的指针域向后或向前扫描 其余结点,所以寻找第一个结点和最后一个结点所花费的时间不等。 链表的优缺点: 1.优点:数据元素的个数可以自由扩充 、插入、删除等操作不必移动数据,只需修 改链接指针,修改效率较高。 2.缺点:必须采用顺序存取,即存取数据元素时,只能按链表的顺序进行访问,访问 节点效率较低。
4.2 单向链表结构
4.2.1 单向链表定义
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问 要通过从头部开始顺序读取。 
4.2.2 实现单向链表
4.2.2.1 创建链表接口
演示的代码如下:
package com.txw;
@SuppressWarnings("all")
public interface MyList<E> {
void add(E element);
E get(int index);
int size();
E remove(int index);
}
如图所示:
4.2.2.2 创建单向链表类
演示的代码如下:
package com.txw;
@SuppressWarnings("all")
public class MySinglyLinkedList<E> implements MyList {
@Override
public void add(Object element) {
}
@Override
public E get(int index) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public E remove(int index) {
return null;
}
}
4.2.2.3 创建节点类
演示的代码如下:
class Node<E>{
private E item;
private Node next;
Node(E item,Node next){
this.item = item;
this.next = next;
}
}
如图所示:
4.2.2.4 实现添加元素方法
演示的代码如下:
private Node head;
private int size;
@Override
public void add(E element) {
Node<E> node = new Node<>(element,null);
Node tail = getTail();
if(tail == null)
this.head = node;
else
tail.next = node;
this.size++;
}
private Node getTail() {
if(this.head == null){
return null;
}
Node node = this.head;
while (true){
if(node.next == null) {
break;
}
node = node.next;
}
return node;
}
如图所示:
4.2.2.5 实现获取元素方法
演示的代码如下:
@Override
public E get(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
return node.item;
}
private void checkIndex(int index){
if(!(index >= 0 && index < this.size)){
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size);
}
}
private Node getNode(int index){
Node<E> node = this.head;
for(int i = 0;i < index;i ++){
node = node.next;
}
return node;
}
如图所示:
4.2.2.6 实现删除元素方法
演示的代码如下:
@Override
public E remove(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
E item = node.item;
if(this.head == node){
this.head = node.next;
}else{
Node<E> temp = this.head;
for(int i=0;i< index - 1;i++){
temp = temp.next;
}
temp.next = node.next;
}
node.next = null;
this.size--;
return item;
}
如图所示:
4.2.2.7 实现获取元素个数
演示的代码如下:
@Override
public int size() {
return this.size;
}
如图所示: 演示测试的代码如下:
package com.txw;
@SuppressWarnings("all")
public class MySinglyLinkedList<E> implements MyList<E> {
class Node<E>{
private E item;
private Node next;
Node(E item,Node next){
this.item = item;
this.next = next;
}
}
private Node head;
private int size;
@Override
public void add(E element) {
Node<E> node = new Node<>(element,null);
Node tail = getTail();
if(tail == null)
this.head = node;
else
tail.next = node;
this.size++;
}
private Node getTail() {
if(this.head == null){
return null;
}
Node node = this.head;
while (true){
if(node.next == null) {
break;
}
node = node.next;
}
return node;
}
@Override
public E get(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
return node.item;
}
private void checkIndex(int index){
if(!(index >= 0 && index < this.size)){
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size);
}
}
private Node getNode(int index){
Node<E> node = this.head;
for(int i = 0;i < index;i ++){
node = node.next;
}
return node;
}
@Override
public int size() {
return this.size;
}
@Override
public E remove(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
E item = node.item;
if(this.head == node){
this.head = node.next;
}else{
Node<E> temp = this.head;
for(int i=0;i< index - 1;i++){
temp = temp.next;
}
temp.next = node.next;
}
node.next = null;
this.size--;
return item;
}
public static void main(String[] args) {
MySinglyLinkedList<String> mySinglyLinkedList = new MySinglyLinkedList<>();
mySinglyLinkedList.add("a");
mySinglyLinkedList.add("b");
mySinglyLinkedList.add("c");
mySinglyLinkedList.add("d");
System.out.println(mySinglyLinkedList.size());
System.out.println(mySinglyLinkedList.remove(0));
for(int i=0;i<mySinglyLinkedList.size();i++){
System.out.println(mySinglyLinkedList.get(i));
}
}
}
如图所示:
4.3 双向链表结构
4.3.1 双向链表定义
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直 接前驱和直接后继。如图所示:
4.3.2 实现双向链表
4.3.2.1 创建双向链表类
演示的代码如下:
package com.txw;
@SuppressWarnings("all")
public class MyDoublyLinkedList<E> implements MyList<E> {
@Override
public void add(E element) {
}
@Override
public E get(int index) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public E remove(int index) {
return null;
}
public static void main(String[] args) {
}
}
如图所示:
4.3.2.2 创建节点类
演示的代码如下:
class Node<E>{
E item;
Node<E> prev;
Node<E> next;
Node(Node<E> prev,E item,Node<E> next){
this.prev = prev;
this.item = item;
this.next = next;
}
}
如图所示:
4.3.2.3 实现添加元素方法
演示的代码如下:
private Node head;
private Node tail;
private int size;
@Override
public void add(E element) {
this.linkLast(element);
}
private void linkLast(E element){
Node t = this.tail;
Node<E> node = new Node<>(t,element,null);
this.tail = node;
if(t == null){
this.head = node;
}else{
t.next = node;
}
this.size++;
}
如图所示:
4.3.2.4 实现获取元素方法
演示的代码如下:
@Override
public E get(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
return node.item;
}
private void checkIndex(int index){
if(!(index >= 0 && index < this.size)){
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + size);
}
}
private Node getNode(int index){
if(index < (this.size >> 1)){
Node node = this.head;
for(int i = 0;i < index;i ++){
node = node.next;
}
return node;
}else{
Node node = this.tail;
for(int i = this.size - 1;i > index;i --){
node = node.prev;
}
return node;
}
}
如图所示:
4.3.2.5 实现删除元素方法
演示的代码如下:
@Override
public E remove(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
E item = node.item;
if(node.prev ==null){
this.head = node.next;
}else{
node.prev.next = node.next;
}
if(node.next == null){
this.tail = node.prev;
}else{
node.next.prev = node.prev;
}
node.prev = null;
node.next = null;
node.item = null;
this.size--;
return item;
}
如图所示:
4.3.2.6 获取元素的个数
演示的代码如下:
@Override
public int size() {
return this.size;
}
如图所示:
4.3.2.7 实现在双向链表的头添加元素
演示的代码如下:
public void addFirst(E element){
this.linkFirst(element);
}
private void linkFirst(E element){
Node head = this.head;
Node node = new Node(null,element,head);
this.head = node;
if(head == null){
this.tail = node;
}else{
head.prev = node;
}
this.size++;
}
如图所示:
4.3.2.8 实现在双向链表的尾添加元素
演示的代码如下:
public void addLast(E element){
this.linkLast(element);
}
如图所示: 演示测试的代码如下:
package com.txw;
@SuppressWarnings("all")
public class MyDoublyLinkedList<E> implements MyList<E> {
class Node<E>{
E item;
Node<E> prev;
Node<E> next;
Node(Node<E> prev,E item,Node<E> next){
this.prev = prev;
this.item = item;
this.next = next;
}
}
private Node head;
private Node tail;
private int size;
@Override
public void add(E element) {
this.linkLast(element);
}
private void linkLast(E element){
Node t = this.tail;
Node<E> node = new Node<>(t,element,null);
this.tail = node;
if(t == null){
this.head = node;
}else{
t.next = node;
}
this.size ++;
}
@Override
public E get(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
return node.item;
}
private void checkIndex(int index){
if(!(index >= 0 && index < this.size)){
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + size);
}
}
private Node getNode(int index){
if(index < (this.size >> 1)){
Node node = this.head;
for(int i = 0;i < index;i ++){
node = node.next;
}
return node;
}else{
Node node = this.tail;
for(int i = this.size - 1;i > index;i --){
node = node.prev;
}
return node;
}
}
@Override
public int size() {
return this.size;
}
@Override
public E remove(int index) {
this.checkIndex(index);
Node<E> node = this.getNode(index);
E item = node.item;
if(node.prev ==null){
this.head = node.next;
}else{
node.prev.next = node.next;
}
if(node.next == null){
this.tail = node.prev;
}else{
node.next.prev = node.prev;
}
node.prev = null;
node.next = null;
node.item = null;
this.size --;
return item;
}
public void addFirst(E element){
this.linkFirst(element);
}
private void linkFirst(E element){
Node head = this.head;
Node node = new Node(null,element,head);
this.head = node;
if(head == null){
this.tail = node;
}else{
head.prev = node;
}
this.size ++;
}
public void addLast(E element){
this.linkLast(element);
}
public static void main(String[] args) {
MyDoublyLinkedList<String> list = new MyDoublyLinkedList<>();
list.add("a");
list.addFirst("A");
list.addLast("B");
list.addFirst("C");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
如图所示:
5 树形结构
5.1 树形结构简介
树结构是一种非线性存储结构,存储的是具有“一对多”关系的数据元素的集合。如图所示:
5.2 树的相关术语
5.2.1 结点(Node)
使用树结构存储的每一个数据元素都被称为“结点”。
5.2.2 结点的度(Degree of Node)
某个结点所拥有的子树的个数。
5.2.3 树的深度(Degree of Tree)
树中结点的最大层次数。
5.2.4 4叶子结点(Leaf Node)
度为 0 的结点,也叫终端结点。
5.2.5 5分支结点(Branch Node)
度不为 0 的结点,也叫非终端结点或内部结点。
5.2.6 孩子(Child)
也可称之为子树或者子结点,表示当前结点下层的直接结点。
5.2.7 双亲(Parent)
也可称之为父结点,表示当前结点的直接上层结点。
5.2.8 根节点(Root Node)
没有双亲结点的结点。在一个树形结构中只有一个根节点。
5.2.9 祖先(Ancestor)
从当前结点上层的所有结点。
5.2.10 子孙(Descendant)
当前结点下层的所有结点。
2.11 兄弟(Brother)
同一双亲的孩子。
5.3 二叉树简介
二叉树(Binary Tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构 往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其 算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个结点最多只能有两棵子树, 且有左右之分。
5.3.1 二叉树分类
5.3.1.1 满二叉树
满二叉树指除最后一层外,每一层上的所有节点都有两个子节点。如图所示:
5.3.1.2 完全二叉树
完全二叉树,除最后一层可能不满以外,其他各层都达到该层节点的最大数,最后一层 如果不满,该层所有节点都全部靠左排。如图所示:
5.3.2 二叉树遍历
二叉树遍历的方式: 1.前序遍历:根-左-右 2.中序遍历:左-根-右 3.后序遍历:左-右-根 4.层序遍历:从上至下逐层遍历
5.3.2.1 前序遍历
前序遍历顺序:根-左-右,如图所示:
5.3.2.2 中序遍历
中序遍历顺序:左-根-右,如图所示:
5.3.2.3 后序遍历
后序遍历顺序:左-右-根,如图所示:
5.3.2.4 层序遍历
层序遍历顺序: 从根节点出发,依次访问左右孩子结点,再从左右孩子出发,依次它们的孩子结点,直 到节点访问完毕。如图所示:
5.3.3 二叉树排序
5.3.3.1 二叉树排序分析
利用二叉树结构以及遍历方式可以实现基于二叉树的元素排序处理。如图所示:
5.3.3.2 二叉树排序实现
5.3.3.2.1 创建二叉树排序器类
演示的代码如下:
package com.txw;
@SuppressWarnings("all")
public class BinaryTreeSort<E extends Integer>{
public void add(E element){
}
public void sort(){
}
public static void main(String[] args) {
}
}
如图所示:
5.3.3.2.2 创建结点类
演示的代码如下:
class Node<E extends Integer>{
private E item;
private Node left;
private Node right;
Node(E item){
this.item = item;
}
public void addNode(Node node){
if(node.item.intValue() < this.item.intValue()){
if(this.left == null)
this.left = node;
else
this.left.addNode(node);
}else{
if(this.right == null)
this.right = node;
else
this.right.addNode(node);
}
}
public void inorderTraversal(){
if(this.left != null)this.left.inorderTraversal();
System.out.println(this.item);
if(this.right != null)this.right.inorderTraversal();
}
}
如图所示:
5.3.3.2.3 实现向排序器中添加元素方法
演示的代码如下:
private Node root;
public void add(E element){
Node<E> node = new Node<>(element);
if(this.root == null)
this.root = node;
else
this.root.addNode(node);
}
如图所示:
5.3.3.2.4 实现排序器中排序方法
演示的代码如下:
public void sort(){
if(this.root == null)return ;
this.root.inorderTraversal();
}
如图所示: 演示测试的代码如下:
package com.txw;
@SuppressWarnings("all")
public class BinaryTreeSort<E extends Integer>{
class Node<E extends Integer>{
private E item;
private Node left;
private Node right;
Node(E item){
this.item = item;
}
public void addNode(Node node){
if(node.item.intValue() < this.item.intValue()){
if(this.left == null)
this.left = node;
else
this.left.addNode(node);
}else{
if(this.right == null)
this.right = node;
else
this.right.addNode(node);
}
}
public void inorderTraversal(){
if(this.left != null)this.left.inorderTraversal();
System.out.println(this.item);
if(this.right != null)this.right.inorderTraversal();
}
}
private Node root;
public void add(E element){
Node<E> node = new Node<>(element);
if(this.root == null)
this.root = node;
else
this.root.addNode(node);
}
public void sort(){
if(this.root == null)return ;
this.root.inorderTraversal();
}
public static void main(String[] args) {
BinaryTreeSort<Integer> sort = new BinaryTreeSort<>();
sort.add(1);
sort.add(8);
sort.add(6);
sort.add(3);
sort.add(5);
sort.add(2);
sort.sort();
}
}
如图所示:
6 自定义树形结构容器
6.1 树形结构定义
能够找到当前结点的父结点 能够找到当前结点的子结点 能够找到当前结点的兄弟结点 能够找到当前结点的祖先结点 能够找到当前结点的子孙节点
6.2 自定义树形结构分析

6.3 实现自定义树形结构容器
6.3.1 创建树形结构容器类
演示的代码如下:
package com.txw;
import java.util.List;
@SuppressWarnings("all")
public class MyTree<E>{
public void add(E parent,E item){
}
public E getParent(E item){
return null;
}
public List<E> getChild(E item){
return null;
}
public List<E> getBrother(E item){
return null;
}
public List<E> getForefathers(E item){
return null;
}
public List<E> getGrandChildren(E item){
return null;
}
public static void main(String[] args) {
}
}
如图所示:
6.3.2 实现添加元素方法
演示的代码如下:
private Map<E,E> map = new HashMap<>();
private Map<E,List<E>> map2 = new HashMap<>();
public void add(E parent,E item){
this.map.put(item,parent);
List<E> list = this.map2.get(parent);
if(list == null){
list = new ArrayList<>();
this.map2.put(parent,list);
}
list.add(item);
}
如图所示:
6.3.3 获取当前结点的父结点与子结点
6.3.3.1 获取父结点
演示的代码如下:
public E getParent(E item){
return this.map.get(item);
}
如图所示:
6.3.3.2 获取子结点
演示的代码如下:
public List<E> getChild(E item){
return this.map2.get(item);
}
如图所示:
6.3.4 获取当前结点的兄弟结点
演示的代码如下:
public List<E> getBrother(E item){
E parent = this.getParent(item);
List<E> list = this.getChild(parent);
List<E> brother = new ArrayList<>();
if(list != null){
brother.addAll(list);
brother.remove(item);
}
return brother;
}
如图所示:
6.3.5 获取当前结点的祖先结点
演示的代码如下:
public List<E> getForefathers(E item){
E parent = this.getParent(item);
if(parent == null){
return new ArrayList<>();
}
List<E> list = this.getForefathers(parent);
list.add(parent);
return list;
}
如图所示:
6.3.6 获取当前结点的子孙节点
演示的代码如下:
public List<E> getGrandChildren(E item){
List<E> list = new ArrayList<>();
List<E> child = this.getChild(item);
if (child == null){
return list;
}
for(int i=0;i<child.size();i++){
E ele = child.get(i);
List<E> temp = this.getGrandChildren(ele);
list.add(ele);
list.addAll(temp);
}
return list;
}
如图所示:
6.3.7 测试自定义容器
演示的代码如下:
package com.txw;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
public class MyTree<E>{
private Map<E,E> map = new HashMap<>();
private Map<E,List<E>> map2 = new HashMap<>();
public void add(E parent,E item){
this.map.put(item,parent);
List<E> list = this.map2.get(parent);
if(list == null){
list = new ArrayList<>();
this.map2.put(parent,list);
}
list.add(item);
}
public E getParent(E item){
return this.map.get(item);
}
public List<E> getChild(E item){
return this.map2.get(item);
}
public List<E> getBrother(E item){
E parent = this.getParent(item);
List<E> list = this.getChild(parent);
List<E> brother = new ArrayList<>();
if(list != null){
brother.addAll(list);
brother.remove(item);
}
return brother;
}
public List<E> getForefathers(E item){
E parent = this.getParent(item);
if(parent == null){
return new ArrayList<>();
}
List<E> list = this.getForefathers(parent);
list.add(parent);
return list;
}
public List<E> getGrandChildren(E item){
List<E> list = new ArrayList<>();
List<E> child = this.getChild(item);
if (child == null){
return list;
}
for(int i=0;i<child.size();i++){
E ele = child.get(i);
List<E> temp = this.getGrandChildren(ele);
list.add(ele);
list.addAll(temp);
}
return list;
}
public static void main(String[] args) {
MyTree<String> myTree = new MyTree<>();
myTree.add("root","生物");
myTree.add("生物","植物");
myTree.add("生物","动物");
myTree.add("生物","菌类");
myTree.add("动物","脊椎动物");
myTree.add("动物","脊索动物");
myTree.add("动物","腔肠动物");
myTree.add("脊椎动物","哺乳动物");
myTree.add("脊椎动物","鱼类");
myTree.add("哺乳动物","猫");
myTree.add("哺乳动物","牛");
myTree.add("哺乳动物","人");
System.out.println("---------获取父结点---------");
String parent = myTree.getParent("鱼类");
System.out.println(parent);
System.out.println("---------获取子结点---------");
List<String> child= myTree.getChild("动物");
for(int i=0;i<child.size();i++){
System.out.println(child.get(i));
}
System.out.println("---------获取兄弟结点---------");
List<String> brother = myTree.getBrother("脊椎动物");
for(int i=0;i<brother.size();i++){
System.out.println(brother.get(i));
}
System.out.println("---------获取祖先结点---------");
List<String> foreFathers = myTree.getForefathers("人");
for(int i=0;i<foreFathers.size();i++){
System.out.println(foreFathers.get(i));
}
System.out.println("---------获取子孙结点---------");
List<String> grandChildren = myTree.getGrandChildren("root");
for(int i =0;i<grandChildren.size();i++){
System.out.println(grandChildren.get(i));
}
}
}
如图所示:
|