二叉查找树的构建及遍历
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;
}
}
}
相关文章推荐
- 二叉树2(二叉查找树的插入、查找、删除、遍历)
- 二叉树的构建和三种遍历算法 (递归实现)
- 给出树的前序遍历和后序遍历,构建树
- *浙大PAT甲级 1099层次遍历二叉查找树
- 二叉树的构建,线索化,以及线索二叉树的遍历c++语言表示(《数据结构》算法6.5,6.6,6.7)
- [LeetCode]106 根据中序遍历和后序遍历构建二叉树
- java 二叉树的构建 先序、中序、后序遍历
- 【LeetCode题目记录-2】从前序遍历和中序遍历构建二叉树
- 使用递归遍历文件夹构建文件树(一)
- 按照前序遍历和中序遍历构建二叉树
- 根据先序遍历和中序遍历构建二叉树
- 无向图的构建及广度优先遍历---邻接表实现
- 二叉查找树的后续遍历序列验证
- <剑指offer 面试题7-1>构建二叉树以及实现遍历 Java
- Java实现二叉树的递归构建及先序、中序、后序、层序遍历(递归和循环)
- 中序遍历,层次遍历构建二叉树
- java实现二叉树的构建以及3种遍历方法
- C++模板实现二叉查找树(四 广度优先遍历)
- java实现二叉树的构建以及3种遍历方法
- 1001 二叉查找树的层序遍历