您的位置:首页 > 其它

二叉查找树的构建及遍历

2017-12-22 10:32 295 查看
*二叉树:由一个称为根的元素和两棵不同的子二叉树组成。

二叉查找树(没有重复元素)特点:每一个结点左子树中的值都小于该结点的值,右子树中结点的值都大于该结点的值。

完全二叉树:二叉树除了最后一层以外的每一层都是满的,而且最后一层的叶子都是在左边。*

二叉树结点的定义

class TreeNode{

Object element;

TreeNode left;

TreeNode right;

public TreeNode(Object o){

element=o;

}

}


向二叉树中插入元素

若为空,使用新元素创建一个根结点,否则,查找新元素的父结点,然后比较与父结点的大小,选择插入左子树或右子树。

public boolean insert(Object o){
if(root == null){
root = new TreeNode(o);
}
else {
TreeNode parent = null;
TreeNode current = root;
while(current != null)
if(((Comparable)o).compareTo(current.element) < 0){
parent = current;
current = current.left;
}
else if (((Comparable)o).compareTo(current.element) > 0){
parent = current;
current = current.right;
}
else
return false;

if(((Comparable)o).compareTo(parent.element) < 0)
parent.left = new TreeNode(o);
else
parent.right = new TreeNode(o);

}
size++;
return true;
}


二叉树的遍历(递归)

/*中序遍历*/
public void inorder(){
inorder(root);
}
private void inor
4000
der(TreeNode root){
if(root == null) return;
inorder(root.left);
System.out.print(root.element + " ");
inorder(root.right);
}
/*后序遍历*/
public void postorder(){
postorder(root);
}
public void postorder(TreeNode root){
if(root == null) return;
inorder(root.left);
inorder(root.right);
System.out.print(root.element + " ");
}
/*前序遍历*/
public void preorder(){
postorder(root);
}
public void preorder(TreeNode root){
if(root == null) return;
inorder(root.left);
inorder(root.right);
System.out.print(root.element + " ");
}
/*查找指定元素*/
public Boolean search(Object o){
TreeNode current=root;
if(current==null) return false;
while(current!=null){
if((((Comparable)o).compareTo(current.element)==0)) return true;
if(((Comparable)o).compareTo(current.element)>0){
current=current.right;
}
else{
current=current.left;
}
}
return false;
}


完整代码


public class BinaryTree {
private TreeNode root;
private int size = 0;

public BinaryTree(){}
/*构造函数*/
public BinaryTree(Object[] objects){
for(int i = 0;i< objects.length; i++)
insert(objects[i]);
}
/*构建二叉树*/
public boolean insert(Object o){ if(root == null){ root = new TreeNode(o); } else { TreeNode parent = null; TreeNode current = root; while(current != null) if(((Comparable)o).compareTo(current.element) < 0){ parent = current; current = current.left; } else if (((Comparable)o).compareTo(current.element) > 0){ parent = current; current = current.right; } else return false; if(((Comparable)o).compareTo(parent.element) < 0) parent.left = new TreeNode(o); else parent.right = new TreeNode(o); } size++; return true; }
/*中序遍历*/
public void inorder(){
inorder(root);
}
private void inorder(TreeNode root){
if(root == null) return;
inorder(root.left);
System.out.print(root.element + " ");
inorder(root.right);
}
/*后序遍历*/
public void postorder(){
postorder(root);
}
public void postorder(TreeNode root){
if(root == null) return;
inorder(root.left);
inorder(root.right);
System.out.print(root.element + " ");
}
/*前序遍历*/
public void preorder(){
postorder(root);
}
public void preorder(TreeNode root){
if(root == null) return;
inorder(root.left);
inorder(root.right);
System.out.print(root.element + " ");
}
/*获取树的长度*/
public int getSize(){
return size;
}
/*查找元素*/
public Boolean search(Object o){
TreeNode current=root;
if(current==null) return false;
while(current!=null){
if((((Comparable)o).compareTo(current.element)==0)) return true;
if(((Comparable)o).compareTo(current.element)>0){
current=current.right;
}
else{
current=current.left;
}
}
return false;
}
/*定义树结点*/
private static class TreeNode{
Object element;
TreeNode left;
TreeNode right;
public TreeNode(Object o){
element=o;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: