数据结构与方法:
public class MyBinaryTree<E> {
//根节点
private Node root;
public void setRoot(Node root) {
this.root = root;
}
//树的前序遍历
public void preOrder() throws RuntimeException {
if(this.root!=null){
this.root.preOrder();
}else {
throw new RuntimeException("root node is null");
}
}
//树的中序遍历
public void infixOrder() throws RuntimeException {
if(this.root!=null){
this.root.infixOrder();
}else {
throw new RuntimeException("root node is null");
}
}
//树的后序遍历
public void postOrder() throws RuntimeException {
if(this.root!=null){
this.root.postOrder();
}else {
throw new RuntimeException("root node is null");
}
}
//前序遍历查找
public Node preOrderSearch(E e){
if(root != null){
return this.root.preOrderSearch(e);
}else {
return null;
}
}
//中序遍历查找
public Node infixOrderSearch(E e){
if(root != null){
return this.root.infixOrderSearch(e);
}else {
return null;
}
}
//后序遍历查找
public Node postOrderSearch(E e){
if(root != null){
return this.root.postOrderSearch(e);
}else {
return null;
}
}
/**
* 叶子节点结构 E为对象泛型
* @param <E>
*/
public static class Node<E> {
E item;
Node<E> left;
Node<E> right;
public Node(E item) {
this.item = item;
}
public Node(E item, Node<E> left, Node<E> right) {
this.item = item;
this.left = left;
this.right = right;
}
public Node() {
}
public void setLeft(Node<E> left) {
this.left = left;
}
public void setRight(Node<E> right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"item=" + item +
'}';
}
/**
* 前序遍历节点
*/
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 Node preOrderSearch(E e){
System.out.println("进入前序查找");
//比较当前节点是不是
if(this.item.equals(e)){
return this;
}
Node node = null;
//判断左子节点是否为空 进行查找
if(this.left != null ){
node = this.left.preOrderSearch(e);
}
//如果找到了 就赋值返回
if(node != null){
return node;
}
//判断右子节点是否为空 查找
if(this.right != null){
node = this.right.preOrderSearch(e);
}
return node;
}
//中序遍历查找
public Node infixOrderSearch(E e){
Node node = null;
//判断左子节点是否为空 进行查找
if(this.left != null ){
node = this.left.infixOrderSearch(e);
}
//如果找到了 就赋值返回
if(node != null){
return node;
}
System.out.println("进入中序查找");
//比较当前节点是不是
if(this.item.equals(e)){
return this;
}
//判断右子节点是否为空 查找
if(this.right != null){
node = this.right.infixOrderSearch(e);
}
return node;
}
//后序遍历查找
public Node postOrderSearch(E e){
Node node = null;
//判断左子节点是否为空 进行查找
if(this.left != null ){
node = this.left.postOrderSearch(e);
}
//如果找到了 就赋值返回
if(node != null){
return node;
}
//判断右子节点是否为空 查找
if(this.right != null){
node = this.right.postOrderSearch(e);
}
//如果找到了 就赋值返回
if(node != null){
return node;
}
System.out.println("进入后序查找");
//比较当前节点是不是
if(this.item.equals(e)){
node = this;
}
return node;
}
}
}
测试方法、数据:
//测试 根据值查找树节点
@Test
public void test02(){
MyBinaryTree.Node node1 = new MyBinaryTree.Node<Integer>(1);
MyBinaryTree.Node node2 = new MyBinaryTree.Node<Integer>(2);
MyBinaryTree.Node node3 = new MyBinaryTree.Node<Integer>(3);
MyBinaryTree.Node node4 = new MyBinaryTree.Node<Integer>(4);
MyBinaryTree.Node node5 = new MyBinaryTree.Node<Integer>(5);
node1.setLeft(node2);
node1.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
MyBinaryTree tree = new MyBinaryTree();
tree.setRoot(node1);
// MyBinaryTree.Node node = tree.preOrderSearch(5); //前序查找 查找4次
// MyBinaryTree.Node node = tree.infixOrderSearch(5); //中序查找 查找3次
MyBinaryTree.Node node = tree.postOrderSearch(5); //前序查找 查找2次
if(node!=null){
System.out.println(node);
}else{
System.out.println("没有找到");
}
}