为什么需要树这种数据结构:
1)数组存储方式的分析
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低
2)链式存储方式的分析
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)
3)树存储方式的分析
能提高数据存储,读取的效率,比如利用二叉排序树(Binary SortTree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
二叉树示意图:
二叉树的概念:
1)树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
2)二叉树的子节点分为左节点和右节点。
3) 如果该二叉树的所有叶子节点都在最后一层,并且结点总数=2^n-1, n为层数,则我们称为满二叉树。
4)如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
二叉树遍历:
二叉树查询:
二叉树代码如下:
//定义BinaryTree 二叉树
class BinaryTree{
private TreeNode root;
public void setRoot(TreeNode root){
this.root = root;
}
//前序遍历
public void preOder(){
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("二叉树为空");
}
}
//前序查找
public TreeNode preOrderSearch(int no){
if (root != null){
return root.preOrderSearch(no);
} else {
return null;
}
}
//中序查找
public TreeNode infixOrderSearch(int no){
if (root != null){
return root.infixOrderSearch(no);
} else {
return null;
}
}
//后序查找
public TreeNode sa(int no){
if (root != null){
return root.postOrderSearch(no);
} else {
return null;
}
}
}
class TreeNode{
private int no;
private String name;
private TreeNode left;
private TreeNode right;
public TreeNode(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 TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
@Override
public String toString() {
return "TreeNode{" +
"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.infixOrder();
}
System.out.println(this);
if (this.right != null){
this.right.infixOrder();
}
}
//后序遍历
public void postOrder(){
if (this.left != null){
this.left.postOrder();
}
if (this.right != null){
this.right.postOrder();
}
System.out.println(this);
}
//前序查找
public TreeNode preOrderSearch(int no){
if (this.no == no){
return this;
}
TreeNode resNode = null;
if (this.left != null){
resNode = this.left.preOrderSearch(no);
}
if (resNode != null){
return resNode;
}
if (this.right != null){
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
//中序查找
public TreeNode infixOrderSearch(int no){
TreeNode resNode = null;
if (this.left != null){
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null){
return resNode;
}
if (this.no == no){
return this;
}
if (this.right != null){
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
//后序查找
public TreeNode postOrderSearch(int no){
TreeNode resNode = null;
if (this.left != null){
resNode = this.left.postOrderSearch(no);
}
if (resNode != null){
return resNode;
}
if (this.right != null){
resNode = this.right.postOrderSearch(no);
}
if (resNode != null){
return resNode;
}
if (this.no == no){
return this;
}
return null;
}
}
ps:【源码自取,点击即可】
ps:以上笔记均来自尚硅谷韩顺平老师《Java数据结构与java算法》