Skip to content

Commit

Permalink
update BinaryTree 💄
Browse files Browse the repository at this point in the history
  • Loading branch information
haiji.yang committed Dec 30, 2020
1 parent 3e8025e commit c89f3b6
Show file tree
Hide file tree
Showing 34 changed files with 567 additions and 70 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
259 changes: 253 additions & 6 deletions note/datastructureAlgorithm/book/datastructure/Tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- [二叉树抽象数据类型及其实现](#二叉树抽象数据类型及其实现)
- [二叉树类的Java接口](#二叉树类的Java接口)
- [BinTreePosition接口](#BinTreePosition接口)
- [二叉树类的实现](#二叉树类的实现)
- [基于链表实现二叉树](#基于链表实现二叉树)
- [二叉树的基本算法](#二叉树的基本算法)
- [二叉树遍历与查找方式演示](#二叉树遍历与查找方式演示)

### 术语及性质

Expand Down Expand Up @@ -131,7 +135,7 @@ public class TreeLinkedList<E> implements Tree<E> {
* <p>
* 返回当前节点的最大弟弟;若没有弟弟,则返回null
* </p>
* @return com.javayh.advanced.java.datastructure.tree.linked.TreeLinkedList<E>
* @return com.javayh.advanced.datastructure.tree.linked.TreeLinkedList<E>
*/
@Override
public TreeLinkedList<E> getNextSibling() {
Expand Down Expand Up @@ -373,7 +377,7 @@ public interface BinTree<E> {
* 获取根节点
* </p>
* @param
* @return com.javayh.advanced.java.datastructure.tree.bin.BinTreePosition
* @return com.javayh.advanced.datastructure.tree.bin.BinTreePosition
*/
BinTreePosition getRoot();

Expand Down Expand Up @@ -406,7 +410,7 @@ public interface BinTree<E> {
* 前序遍历
* </p>
* @param
* @return com.javayh.advanced.java.datastructure.Iterator<E>
* @return com.javayh.advanced.datastructure.Iterator<E>
*/
Iterator<E> elementsPreorder();

Expand All @@ -415,7 +419,7 @@ public interface BinTree<E> {
* 中序遍历
* </p>
* @param
* @return com.javayh.advanced.java.datastructure.Iterator
* @return com.javayh.advanced.datastructure.Iterator
*/
Iterator<E> elementsInorder();

Expand All @@ -424,7 +428,7 @@ public interface BinTree<E> {
* 后序遍历
* </p>
* @param
* @return com.javayh.advanced.java.datastructure.Iterator
* @return com.javayh.advanced.datastructure.Iterator
*/
Iterator<E> elementsPostorder();

Expand All @@ -433,7 +437,7 @@ public interface BinTree<E> {
* 层次遍历
* </p>
* @param
* @return com.javayh.advanced.java.datastructure.Iterator
* @return com.javayh.advanced.datastructure.Iterator
*/
Iterator<E> elementsLevelorder();
}
Expand Down Expand Up @@ -984,7 +988,250 @@ O(1)时间内返回相应的变量,即可实现相应的功能。
若节点 v 的深度为 depth(v),则总共需要修改 depth(v)+1 个节点的规模。为了更新一个节点的
规模记录,只需执行两次 getSize()操作并做两次加法,故 updateSize()算法的总体运行时间为O(depth(v)+1)

### 二叉树遍历与查找方式演示

```java
public class BinaryTreeDemo {
public static void main(String[] args) {
/*
* 构建二叉树的模型
* A
* / \
* B C
* / \
* D E
*/
/*测试二叉树的三种遍历方式*/
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1,"A");
HeroNode node1 = new HeroNode(2,"B");
HeroNode node2 = new HeroNode(3,"C");
HeroNode node3 = new HeroNode(4,"D");
HeroNode node4 = new HeroNode(5,"E");
root.setLeft(node1);
root.setRight(node2);
node2.setLeft(node3);
node2.setRight(node4);
binaryTree.setRoot(root);
//前序遍历
System.out.println("前序遍历~~~");
binaryTree.preOrder();
//中序遍历
System.out.println("中序遍历~~~");
binaryTree.infixOrder();
//后序遍历
System.out.println("后序遍历~~~");
binaryTree.postOrder();

/*二叉树查找指定的结点*/

//前序查找
System.out.println("前序查找~~~");
HeroNode preSearch = binaryTree.preSearch(5);
System.out.println("前序查找~~~" + preSearch);
//中序查找
System.out.println("中序查找~~~");
HeroNode infixSearch = binaryTree.infixSearch(5);
System.out.println("中序查找~~~" + infixSearch);
//后序遍查找
System.out.println("后序查找~~~");
HeroNode postSearch = binaryTree.postSearch(5);
System.out.println("后序查找~~~" + postSearch);
}
}

class BinaryTree{
private HeroNode root;

public void setRoot(HeroNode root) {
this.root = root;
}

@Override
public String toString() {
return "BinaryTree[" +
"root=" + root +
']';
}

/**前序遍历*/
public void preOrder(){
if(root != null){
this.root.preOrder();
}else {
System.out.println("二叉树为空~~~~");
}
}
/**中序遍历*/
public void infixOrder(){
if(root != null){
this.root.infixOrder();
}else {
System.out.println("二叉树为空~~~~");
}
}
/**后序遍历*/
public void postOrder(){
if(root != null){
this.root.postOrder();
}else {
System.out.println("二叉树为空~~~~");
}
}

/*二叉树查找指定节点*/

/**前序遍历*/
public HeroNode preSearch(int no){
if(root != null){
return this.root.preSearch(no);
}else {
return null;
}
}
/**中序遍历*/
public HeroNode infixSearch(int no){
if(root != null){
return this.root.infixSearch(no);
}else {
return null;
}
}
/**后序遍历*/
public HeroNode postSearch(int no){
if(root != null){
return this.root.postSearch(no);
}else {
return null;
}
}
}

@Getter
@Setter
class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode right;

public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
@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.infixOrder();
}
//输出父节点
System.out.println(this);
//递归实现右侧遍历
if(this.right != null){
this.right.infixOrder();
}
}

/**后序遍历*/
public void postOrder(){
//递归实现左侧遍历
if(this.left != null){
this.left.infixOrder();
}
//递归实现右侧遍历
if(this.right != null){
this.right.infixOrder();
}
//输出父节点
System.out.println(this);
}

/**前序查找*/
public HeroNode preSearch(int no){
//如果当前节点为要找的节点直接返回
System.out.println("前序查找");
if(this.no == no){
return this;
}
HeroNode res = null;
//递归前序查找
if(this.left != null){
res = this.left.preSearch(no);
}
if(res != null){
return res;
}
if(this.right != null){
res = this.right.preSearch(no);
}
return res;
}

/**中序序查找*/
public HeroNode infixSearch(int no){
//如果当前节点为要找的节点直接返回
HeroNode res = null;
//递归前序查找
if(this.left != null){
res = this.left.infixSearch(no);
}
if(res != null){
return res;
}
System.out.println("中序序查找");
if(this.no == no){
return this;
}
if(this.right != null){
res = this.right.infixSearch(no);
}
return res;
}

/**后序序查找*/
public HeroNode postSearch(int no){
//如果当前节点为要找的节点直接返回
HeroNode res = null;
//递归前序查找
if(this.left != null){
res = this.left.postSearch(no);
}
if(res != null){
return res;
}
if(this.right != null){
res = this.right.postSearch(no);
}
if(res != null){
return res;
}
System.out.println("后序序查找");
if(this.no == no){
return this;
}
return null;
}
}
```



Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.javayh.advanced.java.datastructure;
package com.javayh.advanced.datastructure;

import com.javayh.advanced.exception.ExceptionNoSuchElement;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.javayh.advanced.java.datastructure;
package com.javayh.advanced.datastructure;

import com.javayh.advanced.exception.ExceptionNoSuchElement;
import com.javayh.advanced.java.datastructure.list.List;
import com.javayh.advanced.datastructure.list.List;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.javayh.advanced.java.datastructure;
package com.javayh.advanced.datastructure;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.javayh.advanced.java.datastructure;
package com.javayh.advanced.datastructure;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.javayh.advanced.java.datastructure.deque;
package com.javayh.advanced.datastructure.deque;

import com.javayh.advanced.exception.ExceptionQueueEmpty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.javayh.advanced.java.datastructure.deque;
package com.javayh.advanced.datastructure.deque;

import com.javayh.advanced.exception.ExceptionQueueEmpty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.javayh.advanced.java.datastructure.deque;
package com.javayh.advanced.datastructure.deque;

import com.javayh.advanced.java.datastructure.Position;
import com.javayh.advanced.datastructure.Position;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.javayh.advanced.java.datastructure.linked;
package com.javayh.advanced.datastructure.linked;

import com.javayh.advanced.java.datastructure.Position;
import com.javayh.advanced.datastructure.Position;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.javayh.advanced.java.datastructure.list;
package com.javayh.advanced.datastructure.list;

import com.javayh.advanced.exception.ExceptionBoundaryViolation;
import com.javayh.advanced.exception.ExceptionPositionInvalid;
import com.javayh.advanced.java.datastructure.Iterator;
import com.javayh.advanced.java.datastructure.Position;
import com.javayh.advanced.datastructure.Iterator;
import com.javayh.advanced.datastructure.Position;

/**
* <p>
Expand Down
Loading

0 comments on commit c89f3b6

Please sign in to comment.