数据结构—二叉树

树的简介:

是一种数据结构,它是由n(n≥1)个有限节点组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。它具有以下的特点:

每个节点有零个或多个子节点;没有父节点的节点称为根节点;每一个非根节点有且只有一个父节点;除了根节点外,每个子节点可以分为多个不相交的子树。
在这里插入图片描述
树的常用术语(结合示意图理解):

  • 节点
  • 根节点
  • 父节点
  • 子节点
  • 叶子节点 (没有子节点的节点)
  • 节点的权(节点值)
  • 路径(从root节点找到该节点的路线)
  • 子树
  • 树的高度(最大层数)
  • 森林 :多颗子树构成森林

二叉树:

定义:二叉树(binary tree)是指树中节点的度不大于2的有序树,它是一种最简单且最重要的树。二叉树的递归定义为:二叉树是一棵空树,或者是一棵由一个根节点和两棵互不相交的,分别称作根的左子树和右子树组成的非空树;左子树和右子树又同样都是二叉树 。

二叉树及其基本操作(前中后序遍历,查找,简单删除):

package com.xawl.tree;

//二叉树及其基本操作
public class BinaryTreeDemo {
	public static void main(String[] args) {

		BinaryTree binaryTree = new BinaryTree();
		//创建需要的结点
		Node root = new Node(1, "宋江");
		Node node2 = new Node(2, "吴用");
		Node node3 = new Node(3, "卢俊义");
		Node node4 = new Node(4, "林冲");
		Node node5 = new Node(5, "关胜");
		
		//说明,我们先手动创建该二叉树,后面我们学习递归的方式创建二叉树
		root.setLeft(node2);
		root.setRight(node3);
		node3.setRight(node4);
		node3.setLeft(node5);
		binaryTree.setRoot(root);
		
		//binaryTree.postOrder();
		
		
		//前序遍历查找的次数:4 
		System.out.println("前序遍历查找~~~");
		System.out.println(binaryTree.preOrderSearch(5));


		//中序遍历查找的次数:3次
		System.out.println("中序遍历查找~~~");
		System.out.println(binaryTree.infixOrderSearch(5));
		

		//后序遍历查找的次数:2次
		System.out.println("后序遍历查找~~~");	
		System.out.println(binaryTree.postOrderSearch(5));
		
		System.out.println("删除no=3后的树:");
		binaryTree.delNode(3);
		binaryTree.preOrder();
	
	}
}

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 no) {
		if(root != null) {
			return root.preOrderSearch(no);
		} else {
			return null;
		}
	}
	//中序遍历查找
	public Node infixOrderSearch(int no) {
		if(root != null) {
			return root.infixOrderSearch(no);
		}else {
			return null;
		}
	}
	//后序遍历查找
	public Node postOrderSearch(int no) {
		if(root != null) {
			return this.root.postOrderSearch(no);
		}else {
			return null;
		}
	}
	
	//删除结点
	public void delNode(int no) {
		//root非空才删除
		if(root != null){
			//只有根节点直接将root置为bull
			if(root.getNo() == no){
				root = null;
			}else{
				root.delNode(no);
			}
		}else {
			System.out.println("当前树为空");
		}
	}
}

//节点类
class Node{

	private int no;
	private String name;
	private Node left; //默认null
	private Node right; //默认null
	
	public Node(int no, String name) {
		super();
		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 Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}

	@Override
	public String toString() {
		return "Node [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 Node preOrderSearch(int no) {
		System.out.println("前序遍历查找被调用");
		//要是当前节点的no等于no直接返回当前节点
		if(this.no == no)return this;
				
		Node resNode = null;//接收结果节点
		//左子节点不为空,就一直向左遍历
		if(this.left != null) resNode =  this.left.preOrderSearch(no);
		
		//要是遍历完左子树resNode不为空,说明找到直接返回
		if(resNode != null) return resNode;
		
		//右子节点不为空,就一直向右遍历
		if (this.right != null) resNode = this.right.preOrderSearch(no);

		//不管在右子节点是否找到,都返回resNode
		return resNode;
	}
	
	//中序遍历查找
	public Node infixOrderSearch(int no) {
		
		Node resNode = null;//接收结果节点
		
		//左子节点不为空,就一直向左遍历
		if(this.left != null) resNode =  this.left.infixOrderSearch(no);		
		
		//要是遍历完左子树resNode不为空,说明找到直接返回
		if(resNode != null) return resNode;
		
		System.out.println("中遍历查找被调用");
		//要是当前节点的no等于no直接返回当前节点
		if(this.no == no) return this;
		
		//右子节点不为空,就一直向右遍历
		if (this.right != null) resNode = this.right.infixOrderSearch(no);
		
		//不管在右子节点是否找到,都返回resNode
		return resNode;
	}
	
	//后序遍历查找
	public Node postOrderSearch(int no) {
		Node resNode = null;//接收结果节点
		//左子节点不为空,就一直向左遍历
		if(this.left != null) resNode =  this.left.postOrderSearch(no);
		
		//要是遍历完左子树resNode不为空,说明找到直接返回
		if(resNode != null) return resNode;
		
		//右子节点不为空,就一直向右遍历
		if (this.right != null) resNode = this.right.postOrderSearch(no);

		//要是遍历完右子树resNode不为空,说明找到直接返回
		if(resNode != null) return resNode;
		
		System.out.println("后序遍历查找被调用");
		//要是当前节点的no等于no直接返回当前节点
		if(this.no == no)return this;
		
		return resNode;
	}
	
	//删除结点(简单删除)
	//1.如果删除的节点是叶子节点,则删除该节点
	//2.如果删除的节点是非叶子节点,则删除该子树
	/*思路
	 * 	1. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
		2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
		3. 如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
		4. 如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
		5.  如果第4步也没有删除结点,则应当向右子树进行递归删除.

	 */
	public void delNode(int no) {
		//左子结点不为空,并且左子结点 就是要删除结点
		if (this.left != null && this.left.no == no) {
			this.left = null;
			return;
		}
		//右子结点不为空,并且右子结点 就是要删除结点
		if (this.right != null && this.right.no == no) {
			this.right = null;
			return;
		}
		//向左递归
		if (this.left != null) {
			delNode(this.left.no);
		}
		//向右递归
		if(this.right != null){
			delNode(this.right.no);
		}
	}
	
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值