二叉树排序(递归与非递归)

二叉树排序

二叉树排序的原理:使用第一个元素作为根节点,后面的数依次与根节点元素作比较,比根节点元素小的放在左边,比根节点元素大的放右边。
如:7 ,2 ,4 ,9 ,6 ,1 ,10在这里插入图片描述
代码实现(递归)

import javax.xml.soap.Node;

public class BinaryTree {
	private Node root=null;
	public Node getRoot() {
		return root;
	}
	private class Node{
		private Node left;
		private Node right;
		private int data;
		public Node(int data) {
			this.data=data;
		}
		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;
		}
		public int getData() {
			return data;
		}
		public void setData(int data) {
			this.data = data;
		}

	}
private void buildTree(Node node,int data) {
		if(root == null) {
			root=new Node(data);
		}else {
			if(data < node.getData()) {
				if(node.getLeft()==null) {
					node.setLeft( new Node(data) );
				}else {
					buildTree(node.getLeft(),data);
				}
			}else {
				if(data>node.getData()) {
					if(node.getRight()==null) {
						node.setRight(new Node(data));
					}else {
						buildTree(node.getRight(), data);
					}
				}
			}
		}
	}
	public static BinaryTree createBitree(int[] datas) {
		BinaryTree binarytree = new BinaryTree();
		for(int data:datas ) {
			binarytree.buildTree(binarytree.getRoot(),data);
		}
		return binarytree;
	}
public class Node {
	public int data;
	public Node leftNode;
	public Node rightNode;
	
	/**
	 * 添加节点
	 * 
	 * */
	public void addNode(Node t) {
		if(t.data<this.data) {
			if(leftNode==null)
				leftNode=t;
			else 
				leftNode.addNode(t);
		}else
			if(rightNode==null)
				rightNode=t;
			else
				rightNode.addNode(t);
	}
	
	/**
	 * 前序排序
	 * 
	 * */
	
	public void qianxu() {
		System.out.print(data+" ");
		if(leftNode!=null)
			leftNode.qianxu();
		if(rightNode!=null)
			rightNode.qianxu();
	}
	/**
	 * 
	 * 中序排序
	 * 
	 * */
	public void zhongxu() {
		if(leftNode!=null)
			leftNode.zhongxu();
		System.out.print(data+" ");
		if(rightNode!=null)
			rightNode.zhongxu();
	}
	/**
	 * 后序排序
	 * 
	 * */
	public void houxu() {
		if(leftNode!=null)
			leftNode.houxu();
		if(rightNode!=null)
			rightNode.houxu();
		System.out.print(data+" ");
	}
public class MyTree {
	public Node root;
	public void add(int x) {
		Node p = new Node();
		p.data=x;
		if(root==null)
			root=p;
		else
			root.addNode(p);
			
	}
	public void sort() {
		if(root==null) return;
		root.qianxu();
		System.out.println("前序排序");

		root.zhongxu();
		System.out.println("中序排序");

		root.houxu();
		System.out.println("后序排序");
	}
	
	public static void main(String[] args) {
		MyTree my = new MyTree();
		my.add(7);
		my.add(2);
		my.add(4);
		my.add(9);
		my.add(6);
		my.add(1);
		my.add(10);
		
		my.sort();
		
	}

执行结果
在这里插入图片描述

代码实现(非递归)

import java.util.Stack;

public class StackTree {
	static Node root;

	class Node {
		
		public int data;
		public Node leftNode;
		public Node rightNode;
		public Node(int data) {
			this.data=data;
		}
	}
		
	 boolean insert(int data) {
		Node node = new Node(data);
		if(root==null) {
			root=node;
			return true;
		}else {
			Node r= root;
			while(true) {
				Node node1 = new Node(data);
				if(r.data > data) {
					if(r.leftNode==null){
						r.leftNode=node1;
						return true;
					}
					r = r.leftNode;
				}else if(r.data < data) {
						if(r.rightNode==null) {
							r.rightNode = node1;
							return true;
						}
						r=r.rightNode;
					}else 
						return false;
			}
		}
	}
		
		 static void qianxu(Node node) {
			if(node==null)
				return;
			Stack<Node> stack = new Stack<>();
			Node r = root;
			while (r != null || !stack.isEmpty()) {
				while (r != null) {
					System.out.print(r.data+" ");//输出根
					stack.push(r);//进栈
					r = r.leftNode;//遍历左子树
				}
				r = stack.pop();
				r = r.rightNode;
			}	
			System.out.print("前序遍历");
		}
		 static void zhongxu(Node node) {
				if(node==null)
					return;
				Stack<Node> stack = new Stack<>();
				Node r = root;
				while (r != null || !stack.isEmpty()) {
					while (r != null) {
						
						stack.push(r);//进栈
						r = r.leftNode;//遍历左子树
					}
					r = stack.pop();
					System.out.print(r.data+" ");//输出根
					r = r.rightNode;
				}	
				System.out.print("中序遍历");
			}
		 static void houxu(Node node) {
				if(node==null)
					return;
				Stack<Node> stack = new Stack<>();
				Node r = root;
				Node t = null;
				while (r != null || !stack.isEmpty()) {
					while (r != null) {
						stack.push(r);//进栈
						r = r.leftNode;//遍历左子树
					}
					r = stack.peek();//取出栈顶的值
					if(r.rightNode==null||r.rightNode==t) {
						t=stack.pop();
						System.out.print(t.data+" ");
						r=null;
					}else 
						r=r.rightNode;
				}	
				System.out.print("后序遍历");
			}

		public static void main(String[] args) {
			StackTree tree = new StackTree();
			int[] a = {7,2,4,9,6,1,10};
			for (int i = 0; i < a.length; i++) {
				tree.insert(a[i]);
			}
			qianxu(root);
			System.out.println(" ");
			zhongxu(root);
			System.out.println(" ");
			houxu(root);
		}
}

执行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值