二叉树(Binary tree)
是树形结构的一个重要类型二叉树特点是每个结点最多只能有两棵子树,且有左右之分
二分搜索树(英语:Binary Search Tree),
也称为 二叉查找树 、二叉搜索树 、有序二叉树或排序二叉树。满足以下几个条件:
- 若它的左子树不为空,左子树上所有节点的值都小于它的根节点。
- 若它的右子树不为空,右子树上所有的节点的值都大于它的根节点
- 它的左、右子树也都是二分搜索树。
实现二分搜索树结构
public class BST<E extends Comparable<E>> {
private class Node {
public E e;
public Node left, right;
public Node(E e) {
this.e = e;
left = null;
right = null;
}
}
private Node root;
private int size;
public BST() {
root=null;
size=0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
}
向二分搜索树中添加元素
package com.qcx.algo.day15.v1;
public class BST<E extends Comparable<E>> {
private class Node {
public E e;
public Node left, right;
public Node(E e) {
this.e = e;
left = null;
right = null;
}
}
private Node root;
private int size;
public BST() {
root=null;
size=0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void add(E e){
if (root == null){
root = new Node(e);
size++;
}else {
add(root, e);
}
}
public void add(Node node, E e){
if (e.equals(node.e))
return;
else if (e.compareTo(node.e) < 0 && node.left == null){
node.left = new Node(e);
size ++;
return;
}else if (e.compareTo(node.e) > 0 && node.right == null){
node.right = new Node(e);
size ++;
return;
}
if (e.compareTo(node.e) < 0)
add(node.left, e);
else if (e.compareTo(node.e) > 0)
add(node.right, e);
}
}
另一种add方式
public Node add(Node node, E e){
if (node == null){
size ++;
return new Node(e);
}
if (e.compareTo(node.e) < 0)
node.left = add(node.left, e);
else if (e.compareTo(node.e) > 0)
node.right = add(node.right, e);
return node;
}
非递归的添加
public void add2(Node node, E e){
if (node == null){
root = new Node(e);
size ++;
return;
}
Node p = root;
while (p != null){
if (e.compareTo(p.e) < 0){
if (p.left == null){
p.left = new Node(e);
size ++;
return;
}
p = p.left;
}else if (e.compareTo(p.e) > 0){
if (p.right == null){
p.right = new Node(e);
size ++;
return;
}
p = p.right;
}
else return;
}
D、L、R 分别代表遍历根结点、遍历左子树、遍历右子树
-
DLR–前序遍历(根在前,从左往右,一棵树的根永远在左子树前面,左子树又永远在右子树前面 ) -
LDR–中序遍历(根在中,从左往右,一棵树的左子树永远在根前面,根永远在右子树前面) -
LRD–后序遍历(根在后,从左往右,一棵树的左子树永远在右子树前面,右子树永远在根前面)
package com.qcx.algo.day15.v1;
public class BST<E extends Comparable<E>> {
private class Node {
public E e;
public Node left, right;
public Node(E e) {
this.e = e;
left = null;
right = null;
}
}
private Node root;
private int size;
public BST() {
root = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void add(E e) {
if (root == null) {
root = new Node(e);
size++;
} else {
add(root, e);
}
}
public Node add(Node node, E e) {
if (node == null) {
size++;
return new Node(e);
}
if (e.compareTo(node.e) < 0) node.left = add(node.left, e);
else if (e.compareTo(node.e) > 0) node.right = add(node.right, e);
return node;
}
public void add2(Node node, E e) {
if (node == null) {
root = new Node(e);
size++;
return;
}
Node p = root;
while (p != null) {
if (e.compareTo(p.e) < 0) {
if (p.left == null) {
p.left = new Node(e);
size++;
return;
}
p = p.left;
} else if (e.compareTo(p.e) > 0) {
if (p.right == null) {
p.right = new Node(e);
size++;
return;
}
p = p.right;
} else return;
}
}
public boolean contains(E e) {
return contains(root, e);
}
public boolean contains(Node node, E e) {
if (node == null)
return false;
if (e.compareTo(node.e) == 0)
return true;
else if(e.compareTo(node.e) < 0)
return contains(node.left, e);
else
return contains(node.right, e);
}
public void preOrder(){
preOrder(root);
}
public void preOrder(Node node){
if (node == null)
return;
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
}
测试
package com.qcx.algo.day15.v1;
import org.omg.CORBA.INTERNAL;
public class Main {
public static void main(String[] args) {
BST<Integer> bst = new BST<>();
Integer[] nums = {5, 3, 6, 8, 4, 2};
for (int num : nums)
bst.add(num);
bst.preOrder();
System.out.println(bst.contains(8));
System.out.println(bst.contains(10));
}
}
中序遍历
public void inOrder(){
inOrder(root);
}
public void inOrder(Node node){
if (node == null)
return;
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
}
后序遍历
public void postOrder(){
postOrder(root);
}
public void postOrder(Node node){
if (node == null)
return;
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
}
|