The characteristics of binary search tree:
1. The value of the parent node is greater than the value of the left subtree; the value of the parent node is less than or equal to the right subtree
The way to traverse the tree:
1. First-order traversal: firstly traverse the root node, then traverse the left subtree, and finally traverse the right subtree
2. Mid-order traversal: Mid-order traverses the left subtree, then traverses the root node, and finally traverses the right subtree
3. Post-order traversal: post-order traverse the left subtree, then traverse the right subtree, and finally traverse the root node
Code:
package com.phome.mapdemo.treedemo;
public class BinarySearchTree {
// 定义一个二叉树节点
private BinaryTree binaryTree;
// 二叉树中总共有几个节点
private int size;
public int getSize() {
return size;
}
public BinaryTree getBinaryTree() {
return binaryTree;
}
// 插入元素(默认)
public void add(int data){
// 如果二叉树的父节点是空的则初始化一个二叉树
BinaryTree newTree = new BinaryTree(data,null,null,null);
if (binaryTree == null){
binaryTree = newTree;
size++;
return;
}
BinaryTree parent = binaryTree;
BinaryTree root = null;
while(true){
if (parent == null)
break;
root = parent;
if(data < parent.data){
parent = parent.left;
}else {
parent = parent.right;
}
}
if (data < root.data){
root.left = newTree;
size++;
}else {
root.right = newTree;
size++;
}
}
// 先序遍历
public void prePrintTree(BinaryTree binaryTree){
if(binaryTree != null){
System.out.print(binaryTree.data+" ");
prePrintTree(binaryTree.left);
prePrintTree(binaryTree.right);
}
}
// 中序遍历
public void inOrderPrintTree(BinaryTree binaryTree){
if(binaryTree != null){
inOrderPrintTree(binaryTree.left);
System.out.print(binaryTree.data + " ");
inOrderPrintTree(binaryTree.right);
}
}
// 后序遍历
public void postOrderPrintTree(BinaryTree binaryTree){
if(binaryTree != null){
postOrderPrintTree(binaryTree.left);
postOrderPrintTree(binaryTree.right);
System.out.print(binaryTree.data + " ");
}
}
public static void main(String[] args) {
BinarySearchTree binarySearchTree = new BinarySearchTree();
binarySearchTree.add(10);
binarySearchTree.add(9);
binarySearchTree.add(11);
binarySearchTree.add(3);
binarySearchTree.add(5);
binarySearchTree.add(8);
binarySearchTree.add(2);
System.out.println("数的节点:"+binarySearchTree.getSize());
System.out.println("先序遍历:");
binarySearchTree.prePrintTree(binarySearchTree.getBinaryTree());
System.out.println();
System.out.println("中序遍历:");
binarySearchTree.inOrderPrintTree(binarySearchTree.getBinaryTree());
System.out.println();
System.out.println("后序遍历:");
binarySearchTree.postOrderPrintTree(binarySearchTree.getBinaryTree());
}
}
class BinaryTree{
public int data;
public BinaryTree parent;
public BinaryTree left;
public BinaryTree right;
public BinaryTree(int data, BinaryTree parent, BinaryTree left, BinaryTree right) {
this.data = data;
this.parent = parent;
this.left = left;
this.right = right;
}
public BinaryTree() {
this(0,null,null,null);
}
@Override
public String toString() {
return "data:["+data+"]";
}
}
Results of the:
数的节点:7
先序遍历:
10 9 3 2 5 8 11
中序遍历:
2 3 5 8 9 10 11
后序遍历:
2 8 5 3 9 11 10