二叉树排序
二叉树排序的原理:使用第一个元素作为根节点,后面的数依次与根节点元素作比较,比根节点元素小的放在左边,比根节点元素大的放右边。
如: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);
}
}
执行结果