二叉树的遍历查找

创建节点、包含插入、查找、删除节点

//定义一个树节点
class Node {
	int id;
	Node left;
	Node right;

	public Node(int id) {
		this.id = id;
	}

	//前序遍历
	public void pre(Node node) {
		if(node==null) {
			return ;
		}
		System.out.print(node.id + " ");
		pre(node.left);
		pre(node.right);
	}
	
	//使用前序遍历查找元素
	public Node preFind(int id, Node node) {
		System.out.println("查找中");
		if(node.id==id) { //找到目标节点
			return node;
		}
		Node temp = null;
		if(node.left!=null) {  //向左子树查找
			temp = preFind(id, node.left);
		}
		if(temp!=null) {
			return temp;
		}
		if(node.right!=null) {  //向右子树查找
			temp = preFind(id, node.right);
		}
		return temp;
	}
	
	//使用中序遍历查找元素
	public Node infixFind(int id, Node node) {
		Node temp = null;
		if(node.left!=null) {  //向左子树查找
			temp = infixFind(id, node.left);
		}
		System.out.println("查找中");
		if(node.id==id) { //找到目标节点
			return node;
		}
		if(temp!=null) {
			return temp;
		}
		if(node.right!=null) {  //向右子树查找
			temp = infixFind(id, node.right);
		}
		return temp;
	}
	
	//使用后序遍历查找元素
	public Node postFind(int id, Node node) {
		Node temp = null;
		if(node.left!=null) {  //向左子树查找
			temp = postFind(id, node.left);
		}
		if(temp!=null) {
			return temp;
		}
		if(node.right!=null) {  //向右子树查找
			temp = postFind(id, node.right);
		}
		System.out.println("查找中");
		if(node.id==id) { //找到目标节点
			return node;
		}
		return temp;
	}
	
	//删除节点
	//如果该非叶子节点 A 只有一个子节点 B,则子节点 B 替代节点 A
	//如果该非叶子节点 A 有左子节点 B 和右子节点 C,则让左子节点 B 替 A
	public boolean deleteNode(int id, Node node) {
		if(node == null) {
			return false;
		}
		if(node.left.id == id) {
			//处理节点
			Node temp = node.left;
			if(temp.left ==null && temp.right == null) { //当被删除节点的左右节点都为空
				node.left = null;
			} else if (temp.left != null && temp.right != null) { //当被删除节点的左右节点都不为空,将左子树作为新的子节点
				node.left = temp.left;
				temp.left.right = temp.right;
			} else if (temp.left != null) { //
				node.left = temp.left;
			} else if (temp.right != null) {
				node.left = temp.right;
			}
			return true;
		}
		if(node.right.id == id){
			//处理节点
			Node temp = node.right;
			if(temp.left ==null && temp.right == null) { //当被删除节点的左右节点都为空
				node.right = null;
			} else if (temp.left != null && temp.right != null) { //当被删除节点的左右节点都不为空,将左子树作为新的子节点
				node.right = temp.left;
				temp.left.right = temp.right;
			} else if (temp.left != null) { //
				node.right = temp.left;
			} else if (temp.right != null) {
				node.right = temp.right;
			}
			return true;
		}
		boolean flag = false;
		flag = deleteNode(id, node.left);
		if(!flag) {
			flag = deleteNode(id, node.right);
		}
		return flag;
	}
}

创建树

class BinaryTree {
	//创建根节点
	Node root = null;
	
	public BinaryTree(Node root) {
		this.root = root;
	}
	
	//使用前序查找
	public void preFind(int id) {
		if(root == null) {
			System.out.println("树为空");
			return ;
		}
		Node temp = root.preFind(id, root);
		System.out.println(temp);
	}
	
	//使用前序查找
	public void infixFind(int id) {
		if(root == null) {
			System.out.println("树为空");
			return ;
		}
		Node temp = root.infixFind(id, root);
		System.out.println(temp);
	}
	
	//使用前序查找
	public void postFind(int id) {
		if(root == null) {
			System.out.println("树为空");
			return ;
		}
		Node temp = root.postFind(id, root);
		System.out.println(temp);
	}
	
	//删除节点
	public boolean deleteNode(int id) {
		if(root == null) {
			return false;
		}
		return root.deleteNode(id, root);
	}
	
	//遍历节点
	public void pre() {
		root.pre(root);
	}
}

测试

public class TraversalTree {
	public static void main(String[] args) {
		Node root = new Node(1);
		Node node1 = new Node(2);
		Node node2 = new Node(3);
		Node node3 = new Node(4);
		Node node4 = new Node(5);
		Node node5 = new Node(6);
		System.out.println(root + " " + node1 + " " + node2 + " " + node3 + " " + node4);
		root.left = node1;
		root.right = node2;
		node2.left = node3;
		node2.right = node4;
		node1.left = node5;
		BinaryTree tree = new BinaryTree(root);
		tree.pre();
		System.out.println("-----------------------------------------------------------------------------");
		tree.deleteNode(3);
		tree.pre();
	}
}

二叉树的迭代遍历

//迭代遍历二叉树--先序
public void preTraversal() {
	if(root == null) {
		System.out.println("树为空");
	}
	//创建栈
	Stack<Node> stack = new Stack<>();
	Node temp = root;
	//将根节点压入栈
	stack.add(temp);
	while(!stack.isEmpty()) {
		temp = stack.pop();
		System.out.print(temp.id + " "); //栈顶出栈
		if(temp.right!=null) { //先压入右子树,这样每次如果左子树也不为空时,保证每次出栈都是最先遍历到的
			stack.add(temp.right);
		}
		if(temp.left!=null) {
			stack.add(temp.left);
		}
	}
}

//迭代遍历二叉树--中序
public void infixTraversal() {
	if(root == null) {
		System.out.println("树为空");
	}
	//创建栈
	Stack<Node> stack = new Stack<>();
	Node temp = root;
	
	while(temp!=null || !stack.isEmpty()) {
		while(temp!=null) {   //向左子树查找,将目前能遍历到的左子树都压入栈中
			stack.add(temp);
			temp = temp.left;
		}
		if(!stack.isEmpty()) {
			temp = stack.pop(); //将最左边的叶子节点输出
			System.out.print(temp.id + " ");
			temp = temp.right; //遍历右子树的左结点
		}
	}
}

//迭代遍历二叉树--后序
public void postTraversal() {
	if(root == null) {
		System.out.println("树为空");
	}
	//创建栈
	Stack<Node> stack = new Stack<>();
	//创建列表
	List<Integer> list = new ArrayList<Integer>();
	Node temp = root;
	//将根节点压入栈
	stack.add(temp);
	while(!stack.isEmpty()) {
		temp = stack.pop();
		list.add(temp.id);
		if(temp.left!=null) { 
			stack.add(temp.left);
		}
		if(temp.right!=null) {
			stack.add(temp.right);
		}
	}
	Collections.reverse(list);
	System.out.println(list);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值