java实现二叉树的链式存储

package com.fxr.二叉树链式存储;

import java.util.Scanner;

public class Tree {

    static final int MAXLEN = 20;
    static Scanner input = new Scanner(System.in);
    CBTType InitTree()
    {
        CBTType node;
        if((node = new CBTType()) != null)
        {
            System.out.println("请输入一个根节点的数据:");
            node.data = input.next();
            node.left = null;
            node.right = null;
            if(node != null)
            {
                return node;
            }else{
                return null;
            }
        }
        return null;
    }

    //添加节点
    void AddTreeNode(CBTType treeNode)//添加节点的实现
    {
        CBTType pnode,parent;
        String data;
        int menusel;
        if((pnode = new CBTType()) != null)
        {
            System.out.println("输入二叉树节点的数据");
            pnode.data = input.next();
            pnode.left = null;
            pnode.right = null;
            System.out.println("输入该节点的父节点的数据:");
            data = input.next();
            parent = TreeFindNode(treeNode,data);//查找制定数据的节点
            if(parent == null)//如果没有找到父节点
            {
                System.out.println("没有找到父节点");
                pnode = null;
                return;
            }
            System.out.println("1.添加该节点到左子树--2.添加节点到右子树");
            do{
                //输入选择项
                menusel = input.nextInt();
                if(menusel == 1 || menusel == 2)
                {
                    if(parent == null)
                    {
                        System.out.println("不存在父节点,请先设置父节点");
                    }else{
                        switch(menusel)
                        {
                        case 1://添加到左节点
                            if(parent.left != null)
                            {
                                System.out.println("左子树节点不为空");
                            }else{
                                parent.left = pnode;
                            }
                            break;
                        case 2://添加到右节点
                            if(parent.right != null)
                            {
                                System.out.println("右子树节点不为空");
                            }else{
                                parent.right = pnode;
                            }
                            break;
                            default:
                                System.out.println("无效的参数");

                        }

                    }
                }

            }while(menusel != 1 && menusel != 2);

        }
    }

    private CBTType TreeFindNode(CBTType treeNode, String data) {
        // 查找节点的实现
        CBTType ptr;
        if(treeNode == null)
        {
            return null;

        }else{
            if(treeNode.data.equals(data))
            {
                return treeNode;
            }else{
                //分别向左右子树递归查找
                if((ptr = TreeFindNode(treeNode.left,data))!=null )
                {
                    return ptr;
                }else if((ptr = TreeFindNode(treeNode.right,data)) != null)
                {
                    return ptr;
                }else{
                    return null;
                }
            }
        }

    }

    //获取左子树
    CBTType TreeLeftNode(CBTType treeNode)//获取左子树
    {
        if(treeNode != null)
        {
            return treeNode.left;
        }else{
            return null;
        }
    }
    //获取右子树
    CBTType TreeRightNode(CBTType treeNode)//获取左子树
    {
        if(treeNode != null)
        {
            return treeNode.right;
        }else{
            return null;
        }
    }

    //判断树是不是为空
    int TreeIsEmpty(CBTType treeNode)//判断空树
    {
        if(treeNode != null)
        {
            return 0;
        }else{
            return 1;
        }
    }

    //计算二叉树的深度
    int TreeDepth(CBTType treeNode)
    {
        int deptleft,deptright;
        if(treeNode == null)
        {
            return 0;
        }else{
            deptleft = TreeDepth(treeNode.left);//左子树深度(递归调用)
            deptright =TreeDepth(treeNode.right);//右子树深度(递归调用) 

            if(deptleft > deptright)
            {
                return deptleft+1;
            }else{
                return deptright+1;
            }

        }
    }

    //清空二叉树
    void ClearTree(CBTType treeNode)
    {
        if(treeNode !=null)
        {
            ClearTree(treeNode.left);//清空左子树
            ClearTree(treeNode.right);//清空右子树
            treeNode = null;//释放当前节点所占的内存
        }
    }

    //显示节点的数据
    void TreeNodeData(CBTType p)
    {
        System.out.printf("%s",p.data);
    }

    //按层遍历
    void LevelTree(CBTType treeNode)
    {
        CBTType p;
        CBTType[]q = new CBTType[MAXLEN];//定义一个顺序栈
        int head = 0,tail = 0;
        if(treeNode != null)//如果队首的引用不为空
        {
            tail = (tail+1)%MAXLEN;//计算循环队列队尾序号
            q[tail] = treeNode;//将二叉树根引用进队列

        }

        //队列不为空进行循环
        while(head != tail)
        {
            head = (head+1)%MAXLEN;
            p = q[head];
            TreeNodeData(p);
            if(p.left != null)
            {
                tail = (tail+1)%MAXLEN;
                q[tail] = p.left;
            }

            if(p.right != null)
            {
                tail = (tail+1)%MAXLEN;
                q[tail] = p.right;
            }

        }

    }

    //先序遍历
    void DLRTree(CBTType treeNode)
    {
        if(treeNode != null)
        {
            TreeNodeData(treeNode);
            DLRTree(treeNode.left);
            DLRTree(treeNode.right);

        }
    }

    //中序遍历
        void LDRTree(CBTType treeNode)
        {
            if(treeNode != null)
            {
                DLRTree(treeNode.left);

                TreeNodeData(treeNode);

                DLRTree(treeNode.right);

            }
        }

        //后序遍历
                void LRDTree(CBTType treeNode)
                {
                    if(treeNode != null)
                    {
                        DLRTree(treeNode.left);

                        DLRTree(treeNode.right);

                        TreeNodeData(treeNode);

                    }
                }

}
时间: 2024-08-03 18:07:11

java实现二叉树的链式存储的相关文章

数据结构:二叉树的链式存储

数据结构:二叉树的链式存储(C语言版) 1.写在前面 二叉树同样有两种存储方式,数组和链式存储,对于数组来说,我们利用二叉树的性质然后利用下标可以方便的找到一个节点的子节点和父节点. 二叉树的性质: 1.二叉树的第i层上至多有2i-1个节点 2.深度为K的二叉树至多有2k-1个节点 3.任何一个二叉树中度数为2的节点的个数必度数为0的节点数目少1. 说明:度数为0,为叶子节点. 4.具有n个节点的完全二叉树的深度为|_Log2N_|+1 5.若完全二叉树中的某节点编号为i,则若有左孩子编号为2i

二叉树的链式存储结构--二叉链表

1 二叉树的链式存储结构 //二叉链表的结点结构定义 typedef struct BiTNode { int data; struct BiTNode *lchild; struct BiTNode *rchild; }BiTNode; typedef struct BiTNode *BiTree; 结构示意图如下: 2 二叉树的遍历方法 (1)前序遍历:先访问根结,然后前序遍历左子树,再前序遍历右子树. (2)

二叉树的链式存储结构----二叉链表

头文件:head.h #include<string.h> #include<ctype.h> #include<malloc.h> /* malloc()等 */ #include<limits.h> /* INT_MAX等 */ #include<stdio.h> /* EOF(=^Z或F6),NULL */ #include<stdlib.h> /* atoi() */ #include<io.h> /* eof()

数据结构之自建算法库——二叉树的链式存储及基本运算

本文是数据结构基础系列(6):树和二叉树中第9课时二叉树的基本运算及其实现的例程. 单链表算法库算法库采用程序的多文件组织形式,包括两个文件: 1.头文件:btree.h,包含定义顺序表数据结构的代码.宏定义.要实现算法的函数的声明: #ifndef BTREE_H_INCLUDED #define BTREE_H_INCLUDED #define MaxSize 100 typedef char ElemType; typedef struct node { ElemType data; //

二叉树的链式存储

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXSIZE 100 typedef char ElemType; typedef struct Node { ElemType data; struct Node *lchild; struct Node *rchild; }*BitTree,BitNode; /*二叉树的基本操作*/ void InitBitTree(BitTree

二叉树的链式存储结构

只复习一下二叉树的遍历,其他的以后再看 表示 /* bt_data_t for bi tree */ typedef char bt_data_t; #define NULL_DATA '\0' /* data_t for queue which will hold the pointer of bitree_t */ typedef void *data_t; typedef struct tree_node_t { bt_data_t data; struct tree_node_t *lc

Java实现链式存储的二叉树

二叉树的定义: 二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的.分别称作这个根的左子树和右子树的二叉树组成. 二叉树的遍历方式主要有:先序遍历(NLR),中序遍历(LNR),后序遍历(LRN),和层次遍历. 注意: 由二叉树的先序序列和中序序列可以唯一地确定一颗二叉树: 由二叉树的后序序列和中序序列可以唯一地确定一颗二叉树: 由二叉树的层序序列和中序序列可以唯一地确定一棵二叉树: 但,由二叉树的先序序列和后序序列无法唯一地确定

链式存储二叉树(java)

 链式存储一个完全二叉树代码: //二叉树(链式存储) import java.util.ArrayDeque; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; public class MyTree { static Node root; class Node{ private Node lChild; private Node rChild;

链式存储的二叉树

package com.txq.dataStructure; import java.util.ArrayList;import java.util.List;import java.util.Queue;import java.util.concurrent.ConcurrentLinkedDeque; /** * 链式存储的二叉树 * * @author TongXueQiang * @date 2016/05/09 * @param <T> */public class LinkedBi