发布于2021-06-12 14:04 阅读(333) 评论(0) 点赞(5) 收藏(2)
ArrayList中维护了一个object类型的数组elementData.所以它也是用数组来实现的。当添加元素时,先判断是否需要扩容,如果需要扩容,则调用grow方法,否则直接添加元素到合适位置。
2. 链式存储方式的分析
**优点:**在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,
删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)
3. 树存储方式的分析
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
案例: [7, 3, 10, 1, 5, 9, 12]
树的常用术语(结合示意图理解):
package com.atguigu.tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
//先需要创建一棵二叉树
BinaryTree binaryTree = new BinaryTree(null);
//创建需要的节点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
//说明,我们先手动创建二叉树,后面在学习使用递归来创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
binaryTree.setRoot(root);
//测试
System.out.println("前序遍历");
binaryTree.preOrder();
System.out.println("中序遍历");
binaryTree.infixOrder();
System.out.println("后序遍历");
binaryTree.postOrder();
}
}
//定义BinaryTree二叉树
class BinaryTree{
private HeroNode root;
public BinaryTree(HeroNode root) {
this.root = root;
}
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
//前序遍历
public void preOrder() {
if (this.root!=null) {
this.root.preOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
//中序遍历
public void infixOrder() {
if (this.root!=null) {
this.root.infixOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
//后序遍历
public void postOrder() {
if (this.root!=null) {
this.root.postOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left; //默认为null
private HeroNode right; //默认为null
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
//前序遍历方法
public void preOrder() {
System.out.println(this); //先输出父节点
//递归向左子树前序遍历
if (this.left!=null) {
this.left.preOrder();
}
//递归向右子树前序遍历
if (this.right!=null) {
this.right.preOrder();
}
}
//中序遍历
public void infixOrder() {
//递归向左子树前序遍历
if (this.left!=null) {
this.left.preOrder();
}
System.out.println(this); //先输出父节点
//递归向右子树前序遍历
if (this.right!=null) {
this.right.preOrder();
}
}
//后序遍历
public void postOrder() {
//递归向左子树前序遍历
if (this.left!=null) {
this.left.preOrder();
}
//递归向右子树前序遍历
if (this.right!=null) {
this.right.postOrder();
}
System.out.println(this); //先输出父节点
}
}
package com.atguigu.tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
// 先需要创建一棵二叉树
BinaryTree binaryTree = new BinaryTree(null);
// 创建需要的节点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
// 说明,我们先手动创建二叉树,后面在学习使用递归来创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
binaryTree.setRoot(root);
//测试查找
System.out.println("前序遍历方式");
HeroNode resNode = binaryTree.infixOrderSearch(2);
if (resNode!=null) {
System.out.printf("找到了,信息:no=%d name=%s", resNode.getNo(),resNode.getName());
}else {
System.out.printf("没找到编号为%d的英雄", 5);
}
}
}
// 定义BinaryTree二叉树
class BinaryTree {
private HeroNode root;
public BinaryTree(HeroNode root) {
this.root = root;
}
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
//前序查找
public HeroNode preOrderSearch(int no) {
//
if (root!=null) {
return root.preOrderSearch(no);
}else {
return null;
}
}
//中序查找
public HeroNode infixOrderSearch(int no) {
if (root!=null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
//后序查找
public HeroNode postOrdersearch(int no) {
if (root!=null) {
return this.root.postOrderSearch(no);
}else {
return null;
}
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left; // 默认为null
private HeroNode right; // 默认为null
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
// 前序查找
public HeroNode preOrderSearch(int no) {
System.out.println("前序查找");
// 比较当前节点是不是
if (this.no == no) {
return this;
}
// 1. 判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
// 2. 如果左递归前序查找找到节点,则返回
HeroNode resNode = null; // 记录查询结果
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if (resNode != null) { // 说明左子树找到了该节点
return resNode;
}
// 1. 左递归前序查找,找到节点,则返回,否则继续判断
// 2. 当前的节点的右子节点是否为空,如果不为空,则继续向右递归谦虚查找
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
// 中序查找
public HeroNode infixOrderSearch(int no) {
// 1. 判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
// 2. 如果左递归中序查找找到节点,则返回
HeroNode resNode = null; // 记录查询结果
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) { // 说明左子树找到了该节点
return resNode;
}
System.out.println("中序查找");
// 比较当前节点是不是
if (this.no == no) {
return this;
}
// 1. 右递归中序查找,找到节点,则返回,否则继续判断
// 2. 当前的节点的右子节点是否为空,如果不为空,则继续向右递归中序查找
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
// 后序查找
public HeroNode postOrderSearch(int no) {
// 1. 判断当前节点的左子节点是否为空,如果不为空,则递归后序查找
// 2. 如果左递归后序查找找到节点,则返回
HeroNode resNode = null; // 记录查询结果
if (this.left != null) {
resNode = this.left.postOrderSearch(no);
}
if (resNode != null) { // 说明左子树找到了该节点
return resNode;
}
// 1. 右递归后序查找,找到节点,则返回,否则继续判断
// 2. 当前的节点的右子节点是否为空,如果不为空,则继续向右递归后序查找
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
System.out.println("后序查找");
// 比较当前节点是不是
if (this.no == no) {
return this;
}
return resNode;
}
}
package com.atguigu.tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
// 先需要创建一棵二叉树
BinaryTree binaryTree = new BinaryTree(null);
// 创建需要的节点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "鲁智深");
// 说明,我们先手动创建二叉树,后面在学习使用递归来创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node4.setRight(node5);
binaryTree.setRoot(root);
//测试删除
System.out.println("删除前,前序遍历");
binaryTree.preOrder();
binaryTree.delNode(3);
System.out.println("删除后,前序遍历");
binaryTree.preOrder();
}
}
// 定义BinaryTree二叉树
class BinaryTree {
private HeroNode root;
public BinaryTree(HeroNode root) {
this.root = root;
}
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
//删除节点
public void delNode(int no) {
if (root!=null) {
//如果只有一个root节点,这里立即判断root是不是要删除的节点
if (root.getNo()==no) {
root=null;
}else {
root.delNode(no);
}
}else {
System.out.println("空树不能删除!");
}
}
// 前序遍历
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left; // 默认为null
private HeroNode right; // 默认为null
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
// 递归实现删除节点
// 1. 如果删除的节点是叶子结点,则删除该节点
// 如果删除的节点不是叶子结点,则删除该子树
public void delNode(int no) {
/*思路
* 1. 因为我们的二叉树是单项的,所以我们是判断当前节点的子节点是否是需要删除的节点,而不能去判断当前这个节点是不是需要删除的节点
* 2. 如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就将this.left=null,并且返回(结束递归删除)
* 3. 如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,就将this.right=null,并且返回(结束递归删除)
* 4. 如果第2和第3不没有删除节点,那么我们就需要向左子树进行递归
* 5. 如果第4步也没有删除节点,则应当向右子树进行递归删除
* */
//2. 如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就将this.left=null,并且返回(结束递归删除)
if (this.left!=null && this.left.no==no) {
this.left=null;
return;
}
//3. 如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,就将this.right=null,并且返回(结束递归删除)
if (this.right!=null && this.right.no==no) {
this.right=null;
return;
}
//4. 如果第2和第3不没有删除节点,那么我们就需要向左子树进行递归
if (this.left!=null) {
this.left.delNode(no);
}
//5. 如果第4步也没有删除节点,则应当向右子树进行递归删除
if (this.right!=null) {
this.right.delNode(no);
}
}
// 前序遍历方法
public void preOrder() {
System.out.println(this); // 先输出父节点
// 递归向左子树前序遍历
if (this.left != null) {
this.left.preOrder();
}
// 递归向右子树前序遍历
if (this.right != null) {
this.right.preOrder();
}
}
}
需求: 给你一个数组 {1,2,3,4,5,6,7},要求以二叉树前序遍历的方式进行遍历。 前序遍历的结果应当为 1,2,4,5,3,6,7
package com.atguigu.tree;
public class ArrBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
// arrBinaryTree.preOrder(0); //1,2,4,5,3,6,7
arrBinaryTree.preOrder();
System.out.println();
arrBinaryTree.infixOrder();
System.out.println();
arrBinaryTree.postOrder();
}
}
// 编写一个ArrayBinaryTree,实现顺序存储二叉树
class ArrBinaryTree {
private int[] arr; // 存储数据节点的数组
public ArrBinaryTree(int[] arr) {
this.arr = arr;
}
// 重载方法,就不需要在调用的时候输入0
public void preOrder() {
this.preOrder(0);
}
public void infixOrder() {
this.infixOrder(0);
}
public void postOrder() {
this.postOrder(0);
}
// 编写一个方法,完成顺序存储二叉树的前序遍历
// index:数组下标
public void preOrder(int index) {
// 如果数组,或者arr.length=0
if (arr == null && arr.length == 0) {
System.out.println("数组为空,不能按照二叉树的前序遍历");
}
// 输出当前这个元素
System.out.println(arr[index]);
// 向左递归遍历:第n个元素的左子节点为:2*n+1
if ((index * 2 + 1) < arr.length) {
preOrder(index * 2 + 1);
}
// 向右递归遍历:第n个元素的左子节点为:2*n+2
if ((index * 2 + 2) < arr.length) {
preOrder(index * 2 + 2);
}
}
// 编写一个方法,完成顺序存储二叉树的中序遍历
// index:数组下标
public void infixOrder(int index) {
// 如果数组,或者arr.length=0
if (arr == null && arr.length == 0) {
System.out.println("数组为空,不能按照二叉树的前序遍历");
}
// 向左递归遍历:第n个元素的左子节点为:2*n+1
if ((index * 2 + 1) < arr.length) {
infixOrder(index * 2 + 1);
}
// 输出当前这个元素
System.out.println(arr[index]);
// 向右递归遍历:第n个元素的左子节点为:2*n+2
if ((index * 2 + 2) < arr.length) {
infixOrder(index * 2 + 2);
}
}
// 编写一个方法,完成顺序存储二叉树的后序遍历
// index:数组下标
public void postOrder(int index) {
// 如果数组,或者arr.length=0
if (arr == null && arr.length == 0) {
System.out.println("数组为空,不能按照二叉树的前序遍历");
}
// 向左递归遍历:第n个元素的左子节点为:2*n+1
if ((index * 2 + 1) < arr.length) {
postOrder(index * 2 + 1);
}
// 向右递归遍历:第n个元素的左子节点为:2*n+2
if ((index * 2 + 2) < arr.length) {
postOrder(index * 2 + 2);
}
// 输出当前这个元素
System.out.println(arr[index]);
}
}
堆排序。
应用案例说明:将下面的二叉树,进行中序线索二叉树。中序遍历的数列为 {8, 3, 10, 1, 14, 6}
中序线索二叉树之后的结果为:
说明: 当线索化二叉树后,Node 节点的 属性 left 和 right ,有如下情况:
(1) left 指向的是左子树,也可能是指向的前驱节点. 比如 ① 节点 left 指向的左子树, 而 ⑩ 节点的 left 指向的
就是前驱节点.
(2) right 指向的是右子树,也可能是指向后继节点,比如 ① 节点right 指向的是右子树,而⑩ 节点的right 指向的是后继节点.
package com.atguigu.tree.hreadedBinaryTree;
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
HeroNode root = new HeroNode(1, "tom");
HeroNode node2 = new HeroNode(3, "jack");
HeroNode node3 = new HeroNode(6, "smith");
HeroNode node4 = new HeroNode(8, "mary");
HeroNode node5 = new HeroNode(10, "king");
HeroNode node6 = new HeroNode(14, "dim");
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
//测试线索化
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree(root);
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNode();
threadedBinaryTree.threadedList(); //测试线索化后二叉树的遍历
/*//测试,以10号节点为例
HeroNode leftNode=node5.getLeft();
HeroNode rightNode=node5.getRight();
System.out.println(leftNode);
System.out.println(rightNode);*/
}
}
//定义BinaryTree二叉树
class ThreadedBinaryTree {
private HeroNode root;
//为了实现线索化,需要创建要给指向当前节点的前驱结点的指针
//在递归实现线索化时,pre始终保留前驱结点
private HeroNode pre=null;
public ThreadedBinaryTree(HeroNode root) {
this.root = root;
}
//重载,
public void threadedNode() {
this.threadedNode(root);
}
/**
* 编写对二叉树进行中序线索化的方法
* @param node:当前需要进行线索化的节点
*/
public void threadedNode(HeroNode node) {
//如果node==null,不能线索化
if (node==null) {
return;
}
//1. 先线索化左子树
threadedNode(node.getLeft());
//2. 线索化当前节点[有难度]
//(1) 处理当前节点的前驱结点
if (node.getLeft()==null) {
//让当前节点的左指针指向前驱结点
node.setLeft(pre);
node.setLeftTpye(1);
}
//(2) 处理当前节点的后继节点
//{8, 3, 10, 1, 14, 6},由8到3,此时pre=8
if ( pre!=null && pre.getRight()==null ) {
pre.setRight(node);
pre.setRightType(1);
}
pre=node; //这句话必须写:每处理一个节点后,让当前节点指向下一个节点
//3. 线索化右子树
threadedNode(node.getRight());
}
//遍历线索化二叉树的方法
public void threadedList() {
//定义一个变量,存储当前遍历的结点,从root开始
HeroNode node = root;
while (node!=null) {
//循环找到leftType==1的结点,第一个找到就是8节点
//当leftType==0时,说明该节点指向的是左子树,并不是最左边的节点,我们需要找到最左边的节点
while (node.getLeftTpye()==0) {
node=node.getLeft();
}
//打印当前节点;
System.out.println(node);
//依据当前节点的右指针有后继节点时,就不断输出
while (node.getRightType()==1) {
//获取当前节点的后继节点
node=node.getRight();
System.out.println(node);
}
//替换这个遍历的节点
node=node.getRight(); //没有这句话会死循环
}
}
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left; // 默认为null
private HeroNode right; // 默认为null
//说明
private int leftTpye; //1. 如果leftType==0,表示指向的是左子树,如果是1表示指向的是前驱节点
private int rightType; //2. 如果rightType==0,表示指向的是右子树,如果是1表示指向的是后继节点
public int getLeftTpye() {
return leftTpye;
}
public void setLeftTpye(int leftTpye) {
this.leftTpye = leftTpye;
}
public int getRightType() {
return rightType;
}
public void setRightType(int rightType) {
this.rightType = rightType;
}
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
}
作者:你不要惹我
链接:http://www.javaheidong.com/blog/article/221965/19bb30b37d7f18ccc053/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!