一、相关说明
本例中构造的二叉树只有添加,没有修改和按照索引来查找等其他功能,因为我只想练习一下二叉搜索树的遍历方式。
本文采用Java实现。
二、代码说明
1)add(E element)方法
public boolean add(E element){
Entry<E> node=root;
Entry<E> newNode,parent=null;
int result=0;
while(node!=null){
parent=node;
result=((Comparable<E>)node.element).compareTo(element);
if(result==0)
{
return false;
}
if(result>0){
node=node.left;
}
else{
node=node.right;
}
size+=1;
}
newNode=new Entry<E>(element);
/**
* 第一个进来的元素
*/
if(parent==null){
root=newNode;
}
else if(result>0){
parent.left=newNode;
}
else{
parent.right=newNode;
}
return true;
}
通过parent来判断添加的元素是不是作为根节点的值,即原始二叉排序树是个空树。
2)节点类
节点类是内部类,更好的实现了封装性。
private class Entry<E>{
E element;
Entry<E> left;
Entry<E> right;
Entry(E element,Entry<E> leftEntry ,Entry<E> rightEntry){
this.element=element;
this.left=leftEntry;
this.right=rightEntry;
}
Entry(E element){
this(element,null,null);
}
Entry(){
this(null,null,null);
}
}
3)广度优先遍历
广度优先遍历采用队列来保存未访问的节点,并控制访问的顺序。
参数param是返回遍历的结果.
public void guangduFirst(List<E> param){
//使用队列来保存需要访问的节点
Deque<Entry<E>> d=new LinkedList<>();
Entry<E> node;
d.push(root);
while(!d.isEmpty()){
node=d.poll();
if(node!=null){
param.add(node.element);
if(node.left!=null){
d.addLast(node.left);
}
if(node.right!=null){
d.addLast(node.right);
}
}
}
}
4)深度优先遍历
深度优先遍历采用栈来保存未访问的节点,并控制访问的顺序。
参数param是返回遍历的结果.
public void depthFisrt(List<E> param){
//使用栈来保存需要访问的节点
Stack<Entry<E>> s=new Stack<>();
s.push(root);
Entry<E> node;
while(!s.isEmpty()){
node=s.pop();
param.add(node.element);
if(node.right!=null){
s.push(node.right);
}
if(node.left!=null){
s.push(node.left);
}
}
}
三、总代码
二叉排序树:
package List;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class binarySortTree<E> {
public Entry<E> root;
private int size;
public binarySortTree() {
root=null;
size=0;
}
public int size(){
return size;
}
/**
* 添加新节点
* @param element
* @return
*/
public boolean add(E element){
Entry<E> node=root;
Entry<E> newNode,parent=null;
int result=0;
while(node!=null){
parent=node;
result=((Comparable<E>)node.element).compareTo(element);
if(result==0)
{
return false;
}
if(result>0){
node=node.left;
}
else{
node=node.right;
}
size+=1;
}
newNode=new Entry<E>(element);
/**
* 第一个进来的元素
*/
if(parent==null){
root=newNode;
}
else if(result>0){
parent.left=newNode;
}
else{
parent.right=newNode;
}
return true;
}
/**
* 中序遍历
*
* @param node
* @param param
*/
public void zhongxubianli(Entry<E> node,List<E> param){
if(node==null){
return;
}
zhongxubianli(node.left,param);
param.add(node.element);
zhongxubianli(node.right,param);
}
/**
* 前序遍历
*
* @param node
* @param param
*/
public void qianxubianli(Entry<E> node,List<E> param){
if(node==null){
return;
}
param.add(node.element);
qianxubianli(node.left,param);
qianxubianli(node.right,param);
}
/**
* 广度优先遍历
*
* @param param
*/
public void guangduFirst(List<E> param){
//使用队列来保存需要访问的节点
Deque<Entry<E>> d=new LinkedList<>();
Entry<E> node;
d.push(root);
while(!d.isEmpty()){
node=d.poll();
if(node!=null){
param.add(node.element);
if(node.left!=null){
d.addLast(node.left);
}
if(node.right!=null){
d.addLast(node.right);
}
}
}
}
/**
* 深度优先遍历
*
* @param param
*/
public void depthFisrt(List<E> param){
//使用栈来保存需要访问的节点
Stack<Entry<E>> s=new Stack<>();
s.push(root);
Entry<E> node;
while(!s.isEmpty()){
node=s.pop();
param.add(node.element);
if(node.right!=null){
s.push(node.right);
}
if(node.left!=null){
s.push(node.left);
}
}
}
/**
* 内部类,节点类
* @author Administrator
*
* @param <E>
*/
private class Entry<E>{
E element;
Entry<E> left;
Entry<E> right;
Entry(E element,Entry<E> leftEntry ,Entry<E> rightEntry){
this.element=element;
this.left=leftEntry;
this.right=rightEntry;
}
Entry(E element){
this(element,null,null);
}
Entry(){
this(null,null,null);
}
}
}
测试二叉排序树:
package List;
import java.util.ArrayList;
import java.util.List;
public class LPR {
public static void main(String[] args) {
int[]data={5,6,3,57,231,346,2,4,7};
System.out.print("原始数据:");
binarySortTree<Integer> tree=new binarySortTree<>();
for(int i=0;i<data.length;i++){
tree.add(data[i]);
System.out.print(data[i]+" ");
}
System.out.println();
/**
* 深度优先遍历测试
*/
List<Integer> result=new ArrayList<>();
tree.depthFisrt( result);
System.out.print("深度优先遍历结果:");
for(int i=0;i<result.size();i++){
System.out.print(result.get(i)+" ");
}
System.out.println();
/**
* 中序遍历测试
*/
List<Integer> result2=new ArrayList<>();
tree.zhongxubianli(tree.root,result2);
System.out.print("中序遍历结果:");
for(int i=0;i<result2.size();i++){
System.out.print(result2.get(i)+" ");
}
}
}