1.二叉树的数据存储结构
(1)二叉树的结点的数据存储结构
class Node {
//数据元素
public Object date;
//左树
public Node leftchild;
//右树
public Node righchild;
public Node(){
this.righchild=this.leftchild=null;
}
}
(2)二叉树的树根的数据存储结构
接下来:
1.我会通过树根创建一颗二叉树(树的模样在下文有图)
2.然后先序,中序,后序遍历二叉树
3.在数根类中写一些对此二叉树进行操作的实例方法
(1)查找某个元素是否存在
(2)求该树的深度
(3)求该树的结点个数
(4)求该树的叶的个数
class binaryTree{
// 这是一个根节点
public Node root;
private boolean search=false;
private int nodeNumber=0;
private int LeafNumber=0;
public binaryTree(){
this.root=null;
}
}
2.树的递归创建以及递归遍历
树如图所示

public class TreeApp{
private int nodeNumber;
private int leafnumber;
public static Node create(Node node){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(!s.equals("#")){
node=new Node();
node.date=s;
node.righchild=create(node.righchild);
node.leftchild=create(node.leftchild);
}else {
//不添加元素 滞空
node=null;
}
return node;
}
public static void main(String[] args) {
TreeApp app=new TreeApp();
binaryTree tree=new binaryTree();
tree.root=create(tree.root);
System.out.println("=====前序遍历====");
qianxubianli(tree.root);
System.out.println("=====后序遍历===");
houbianli(tree.root);
System.out.println("======中序遍历===");
zhongxubianli(tree.root);
}
public static void qianxubianli(Node root){
if(root==null)return;
System.out.println(root.date);
qianxubianli(root.leftchild);
qianxubianli(root.righchild);
}
public static void houbianli(Node root){
if(root==null)return;
houbianli(root.leftchild);
houbianli(root.righchild);
System.out.println(root.date);
}
public static void zhongxubianli(Node root){
if(root==null)return;
zhongxubianli(root.leftchild);
System.out.println(root.date);
zhongxubianli(root.righchild);
}
}
以下是创建方式(键盘输入内容):

3.重点:上述1.2所有操作的实现
这里写目录标题
class binaryTree{
// 这是一个根节点
public Node root;
//用于查找
private boolean search=false;
//用于结点个数的计算
private int nodeNumber=0;
//用于叶的个数计算
private int LeafNumber=0;
//无参构造初始化 (养成好习惯 令root=null)
public binaryTree(){
this.root=null;
}
/*
binaryTree_Operation_realization
*/
//======================查找某个元素(假设树中无重复元素)====================================
public boolean search(Object obj){
search(this.root,obj);
return this.search;
}
// 这里我选择后序遍历查找(你怎么遍历无所谓随意)
private void search(Node node,Object obj){
if(node==null){
return;
}
search(node.leftchild,obj);
search(node.righchild,obj);
// 如果查询到存在obj那么 实例变量search=true;
if(obj.equals(node.date)){
this.search=true;
}
}
//===========================求树的深度==============================
public int calculationdepth(Node node){
if(node==null){
return 0;
}
//这里的L和r无先后顺序都可以 只是先算左孩子的深度还是右孩子的深度
int L_length=calculationdepth(node.leftchild);
int r_length=calculationdepth(node.righchild);
// 这样的判断 就可以实现先算左树的深度 然后再递归完回来的时候计算的就是右树深度
return L_length>=r_length?L_length+1:r_length+1;
}
//============================节点个数====================================
public int Nodenumber(){
docalculationNodenumber(this.root);
return this.nodeNumber;
}
private void docalculationNodenumber(Node node){
if (node==null){return;}
nodeNumber++;
docalculationNodenumber(node.righchild);
docalculationNodenumber(node.leftchild);
}
//======================求叶子个数==========================================
public int LeafNumber(){
doLeafnumber(this.root);
return this.LeafNumber;
}
private void doLeafnumber(Node node){
if (node==null){
return;
}else{
if(node.righchild==null && node.leftchild==null){
LeafNumber++;
}
}
doLeafnumber(node.righchild);
doLeafnumber(node.leftchild);
}
}
4.我们进行测试
结果如图:

class TreeApp{
private int nodeNumber;
private int leafnumber;
public static Node create(Node node){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(!s.equals("#")){
node=new Node();
node.date=s;
node.righchild=create(node.righchild);
node.leftchild=create(node.leftchild);
}else {
//不添加元素 滞空
node=null;
}
return node;
}
public static void main(String[] args) {
//树的创建
binaryTree tree=new binaryTree();
tree.root=create(tree.root);
System.out.println("============查找==============");
System.out.println(tree.search("F"));
System.out.println("==========树的深度============");
System.out.println(tree.calculationdepth(tree.root));
System.out.println("==========结点个数============");
System.out.println(tree.Nodenumber());
System.out.println("==========叶的个数============");
System.out.println(tree.LeafNumber());
}
}
该博客主要围绕二叉树展开,使用Java语言实现相关操作。先介绍了二叉树结点和树根的数据存储结构,接着通过树根创建二叉树,进行先序、中序、后序遍历,还实现了查找元素、求树深度、结点个数和叶的个数等操作,最后进行了测试。
1252

被折叠的 条评论
为什么被折叠?



