* 树形结构简介
* 树形结构是一种非线性结构,存储的是“一对多的”关系的数据元素的集合
*
* 树形结构的相关术语:
* 节点(Node):使用树结构存储的每一个数据元素都被称为“节点”
* 节点的度(Degree of Node):某个节点所拥有的子树的个数
* 树的深度(Degree of Tree):树中节点的最大层数
* 叶子节点(Leaf Node):度为0的节点,也叫终端节点
* 分支节点(Branch Node):度不为0的节点,也叫非终端节点或内部节点
* 孩子(child):也可称之为子树或子节点,表示当前节点下层的直接节点
* 双亲(parent):也可称为父节点,表示当前节点的直接上层节点
* 根节点(Root Node):没有双亲节点的节点,在树形结构中只有一个根节点
* 祖先(Ancestor):当前节点上层的所有节点(当前节点的直接祖先节点)
* 子孙(Descendent):当前节点下层的所有节点
* 兄弟(Brother):同一双亲孩子
*
* 二叉树简介
* 二叉树(Binary Tree)是树形结构的重要类型
* 二叉树特点是每个节点最多只能有两颗子树,且有左右之分
*
* 二叉树分类
* 满二叉树:除最后一层外,每一层上的所有节点都有两个子节点
* 完全二叉树:除最后一层可能不满外,其他各层都达到该层节点的最大数,最后一层如果不满,该层节点全部靠左排
*
* 二叉树遍历方式
* 前序遍历:根-左-右
* 中序遍历:左-根-右
* 后序遍历:左-右-根
* 层序遍历:从上至下逐层遍历
//添加元素和排序处理,不能对所有数据类型进行排序,进行上限限定 public class Text4{ private Node root; //根节点地址 //将元素放入排序器的方法 private void add(E element){ //实例化节点对象 Node node=new Node<>(element); //判断当前二叉树中是否有根节点 if (this.root==null){ this.root=node; }else { this.root.addNode(node); } } //元素在排序器中进行排序的方法 private void sort(){ if (this.root==null){ return; } this.root.inorderTraversal(); } //定义节点类 //节点类中添加节点 class Node { / private void addNode(Node node){ //完成新节点元素与当前节点元素大小的判断 //如果新节点中的元素小于当前节点中元素,则新节点放入当前节点的左子树中 //***为什么要使用intValue()方法?this.item为什么可以代表当前元素???*** 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(); } } } public static void main(String[] args) { Text4 aa=new Text4<>(); //12,10,8,5,6,20 aa.add(100); aa.add(80); aa.add(90); aa.add(85); aa.add(110); aa.add(50); aa.add(60); aa.add(70); aa.add(65); aa.add(120); aa.add(119); aa.add(121); aa.sort(); } }



