4.1 二叉树数据结构

4.1 二叉树数据结构

简介

  1. 三种递归遍历方法,三种递归查找方法,三种迭代遍历方法,一种删除节点方法,一种层次遍历方法。
  2. 顺序存储二叉树

代码

  1. 代码1
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Queue;

/**
 * 实现二叉树结构
 * 1. 前序、中序、后续遍历
 * 2. 前序、中序、后续遍历查找
 * @author dxt
 *
 */
public class BinaryTreeDemo {
	public static void main(String[] args) {
		//1. 构造一棵二叉树
		BinaryTree br = new BinaryTree();
		//1.1 先创建树的节点
		Node n1 = new Node(1, 100);
		Node n2 = new Node(2, 200);
		Node n3 = new Node(3, 300);
		Node n4 = new Node(4, 400);
		Node n5 = new Node(5, 500);
		//1.2 链接所有节点				//			 1(100)
		n1.setLeft(n2);				//			 /     \		
		n1.setRight(n3);			//		 2(200)   3(300)
		n3.setLeft(n4);				//			      /     \
		n3.setRight(n5);			//			   4(400)   5(500)
		//1.3 设置树的跟节点
		br.setRoot(n1);
		
		//2. 测试三种遍历方式
		//2.1 测试前序遍历	
		System.out.println("前序遍历");
		br.preOrder();		//1 2 3 4 5
		System.out.println();
		//2.2 测试中序遍历
		System.out.println("中序遍历");
		br.infixOrder(); 	//2 1 4 3 5
		System.out.println();
		//2.3 测试后序遍历
		System.out.println("后序遍历");
		br.postOrder(); 	//2 4 5 3 1
		
		//3. 测试3中遍历查找方法
		//3.1 测试前序遍历查找
		Node resNode = null;
		System.out.println("前序遍历查找:");
		resNode = br.preOrderSearch(500);
		System.out.println(resNode);
		//3.2 测试中序遍历查找
		System.out.println("中序遍历查找:");
		resNode = br.infixOrderSearch(300);
		System.out.println(resNode);
		//3.3 测试后序遍历查找
		System.out.println("后序遍历查找:");
		resNode = br.postOrderSearch(1000);
		System.out.println(resNode);
		
		//4. 删除4号节点
		System.out.println("删除4号节点:");
		br.deleteNode(4);
		br.preOrder();
		
		//5 迭代遍历
		//5.1 前序迭代遍历
		System.out.println("迭代前序遍历:");
		br.preIteration();
		System.out.println();
		//5.2 中序迭代遍历
		System.out.println("迭代中序遍历:");
		br.infixIteration();
		System.out.println();
		//5.3 后序迭代遍历
		System.out.println("迭代后续遍历:");
		br.postIteration();
		System.out.println();
		
		//6 层次遍历
		System.out.println("层次遍历哦...");
		br.levelOrder();
	}
}
/**
 * 二叉树类
 * @author dxt
 *
 */
class BinaryTree{
	private Node root;
	
	public void setRoot(Node 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("二叉树当前为空。。。");
		}
	}
	
	//前序遍历查找
	public Node preOrderSearch(int value) {
		if(this.root != null) {
			return this.root.preOrderSearch(value);
		}else {
			return null;
		}
		
	}
	//中序遍历查找
	public Node infixOrderSearch(int value) {
		if(this.root != null) {
			return this.root.infixOrderSearch(value);
		}else {
			return null;
		}
	}
	//后序遍历查找
	public Node postOrderSearch(int value) {
		if(this.root != null) {
			return this.root.postOrderSearch(value);
		}else {
			return null;
		}
	}
	
	//删除节点,如果删除节点为叶子节点,则直接删除;如果不是叶子节点,则删除以它为根的子树
	public void deleteNode(int key) {
		if(root != null) {
			//删除root节点
			if(root.getKey() == key) {
				root = null;
			}else {
				root.deleteNode(key);
			}
		}else {
			System.out.println("二叉树为空,删除失败");
		}
	}
	
	//前序迭代遍历
	public void preIteration() {
		if(this.root == null) {
			System.out.println("二叉树为空。。。");
			return;
		}
		List<Integer> list = new ArrayList<Integer>();
		//创建一个栈结构保存节点
		Deque<Node> stack = new ArrayDeque<Node>();
		Node node = this.root;
		while(!stack.isEmpty() || node != null) {
			while(node != null) {
				list.add(node.getKey());
				stack.push(node);
				node = node.getLeft();	//移动到当前节点的左节点
			}
			//当前节点没有左节点可以遍历,就访问右节点
			node = stack.pop();
			node = node.getRight();
		}
		//输出遍历结果
		System.out.println(list.toString());
	}
	//中序迭代遍历
	public void infixIteration() {
		//判断树是否为空
		if(this.root == null) {
			System.out.println("二叉树为空。。。");
			return;
		}		
		List<Integer> list = new ArrayList<Integer>();
		Deque<Node> stack = new ArrayDeque<Node>();
		Node node = root;
		while(!stack.isEmpty() || node != null) {
			//顺着左子树,直到最左叶子节点,将途中的节点保存到栈中
			while(node != null) {
				stack.push(node);
				node = node.getLeft(); 
			}
			//此节点的左子节点为空,则将此节点从栈中取出,遍历此节点,然后遍历此节点的右节点
			node = stack.pop();
			list.add(node.getKey());
			node = node.getRight();
		}
		//输出迭代结果
		System.out.println(list.toString());
	}
	//后序迭代遍历, 左、 右、 节点
	public void postIteration() {
		//判断树是否为空
		if(root == null) {
			System.out.println("二叉树为空...");
			return;
		}
		
		List<Integer> list = new ArrayList<Integer>();
		Deque<Node> stack = new ArrayDeque<Node>();
		Node node = root;
		Node prev = null;
		while(!stack.isEmpty() || node != null) {
			//先通过左子树一直到最左节点
			while(node != null) {
				stack.push(node);
				node = node.getLeft();
			}
			//栈顶节点的左节点为空
			node = stack.pop();
			//右节点为空,或者已经遍历
			if(node.getRight() == null || node.getRight() == prev) {
				list.add(node.getKey());
				prev = node;
				node = null;
			}else {
				stack.push(node);
				node = node.getRight();
			}
		}
		System.out.println(list.toString());
	}
	
	//层次遍历
	public void levelOrder() {
		if(root == null) {
			System.out.println("二叉树为空...");
			return;
		}
		List<Integer> list = new ArrayList<Integer>();
		Queue<Node> q = new ArrayDeque<Node>();
		Node node = root;
		q.add(root);
		while(!q.isEmpty()) {
			node = q.remove();
			list.add(node.getKey());
			//左右子节点入队列
			if(node.getLeft() != null) {
				q.add(node.getLeft());
			}
			if(node.getRight() != null) {
				q.add(node.getRight());
			}
		}
		//输出结果
		System.out.println(list.toString());
	}
}
/**
 * 二叉树节点
 * @author dxt
 *
 */
class Node{
	private int key;
	private int value;
	private Node left;
	private Node right;
	
	public Node() {}
	public Node(int key, int value) {
		this.key = key;
		this.value = value;
	}
	public int getKey() {
		return this.key;
	}
	public void setKey(int key) {
		this.key = key;
	}
	public int getValue() {
		return this.value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public Node getLeft() {
		return this.left;
	}
	public void setLeft(Node left) {
		this.left = left;
	}
	public Node getRight() {
		return this.right;
	}
	public void setRight(Node right) {
		this.right = right;
	}
	/**
	 * 重写toString()方法
	 */
	public String toString() {
		return "Node [key=" + this.key +", value=" + this.value +"]";
	}
	
	//前序遍历, 节点 左子树 右子树
	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(int value) {
		//判断当前节点
		if(this.value == value) {
			return this;
		}
		//判断左子树
		Node resNode = null;
		if(this.left != null) {
			resNode = this.left.preOrderSearch(value);
		}
		if(resNode != null) {	//左子树找到了
			return resNode;
		}
		//左子树没有找到,再判断右子树
		if(this.right != null) {
			resNode = this.right.preOrderSearch(value);
		}
		return resNode;	//查找完毕,不管找没找到,都要返回
	}
	//中序遍历查找
	public Node infixOrderSearch(int value) {
		Node resNode = null;
		//1. 先找左子树
		if(this.left != null) {
			resNode = this.left.infixOrderSearch(value);
		}
		if(resNode != null) {
			return resNode;
		}
		//2. 判断当前节点
		if(this.value == value) {
			return this;
		}
		//3. 判断右子树
		if(this.right != null) {
			resNode = this.right.infixOrderSearch(value);
		}
		return resNode;
	}
	//后序遍历查找
	public Node postOrderSearch(int value) {
		Node resNode = null;
		//1. 先判断左子树
		if(this.left != null) {
			resNode = this.left.postOrderSearch(value);
		}
		if(resNode != null) {
			return resNode;
		}
		//2. 再判断右子树
		if(this.right != null) {
			resNode = this.right.postOrderSearch(value);
		}
		if(resNode != null) {
			return resNode;
		}
		//3. 判断当前节点
		if(this.value == value) {
			return this;
		}
		//4. 否则 返回null
		return null;
	}
	
	//删除节点,如果删除节点为叶子节点,则直接删除;如果不是叶子节点,则删除以它为根的子树
	public void deleteNode(int key) {
		//左节点
		if(this.left != null && this.left.key == key) {
			this.left = null;
			return;
		}
		//右节点
		if(this.right != null && this.right.key == key) {
			this.right = null;
			return;
		}
		
		if(this.left != null) {
			this.left.deleteNode(key);
		}
		if(this.right != null) {
			this.right.deleteNode(key);
		}
	}
}
  1. 代码2
/**
 * 顺序存储二叉树--数组存储二叉树
 * 重要:
 * 对于一棵完全二叉树
 * 	1. 第n个节点的左节点为2*n+1
 *  2. 第n个节点的右节点为2*n+2
 *  3. 第n个节点的父节点为(n-1)/2
 * @author dxt
 *
 */
public class ArrBinaryTreeDemo {
	public static void main(String[] args) {
		int[] arr = {1,2,3,4,5,6,7};
		ArrBinaryTree abt = new ArrBinaryTree(arr);
		//abt.preOrder();
		//abt.infixOrder();
		abt.postOrder();
	}
}
/**
 * 模拟实现顺序存储二叉树
 * @author dxt
 *
 */
class ArrBinaryTree{
	int[] arr;
	
	public ArrBinaryTree(int[] arr) {
		this.arr = arr;
	}
	//重载三种遍历方法
	public void preOrder() {
		this.preOrder(0);
	}
	public void infixOrder() {
		this.infixOrder(0);
	}
	public void postOrder() {
		this.postOrder(0);
	}
	//前序遍历
	public void preOrder(int index) {
		if(arr == null || arr.length == 0) {
			System.out.println("二叉树为空。。。");
			return;
		}
		//输出节点数据
		System.out.println(arr[index]);
		//遍历左子树
		if((2 * index + 1) < arr.length) {
			preOrder(2*index+1);
		}
		//遍历右子树
		if((2 * index + 2) < arr.length) {
			preOrder(2*index+2);
		}
	}
	//中序遍历
	public void infixOrder(int index) {
		if(arr == null || arr.length == 0) {
			System.out.println("二叉树为空。。。");
			return;
		}
		//遍历左子树
		if((2 * index + 1) < arr.length) {
			infixOrder(2*index+1);
		}	
		//根节点
		System.out.println(arr[index]);
		//遍历右子树
		if((2 * index + 2) < arr.length) {
			infixOrder(2*index+2);
		}
	}
	//后序遍历
	public void postOrder(int index) {
		if(arr == null || arr.length == 0) {
			System.out.println("二叉树为空。。。");
			return;
		}
		//遍历左子树
		if((2 * index + 1) < arr.length) {
			postOrder(2*index+1);
		}	
		//遍历右子树
		if((2 * index + 2) < arr.length) {
			postOrder(2*index+2);
		}
		//根节点
		System.out.println(arr[index]);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值