链式存储的二叉树

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 LinkedBinTree<T> {
private BinaryNode<T> root;// 根节点
private int nodeNum;//树的元素个数
private BinaryNode<T> result;//查找节点的结果

public LinkedBinTree() {

}

public BinaryNode<T> insert(T[] data) {
return root = insert(data, root, 0);
}
/**
* 构建二叉树
* @param data
* @param node
* @param index
* @return
*/
private BinaryNode<T> insert(T[] data, BinaryNode<T> node, int index) {
if (node == null) {
node = new BinaryNode<T>(data[index]);
nodeNum++;

if (nodeNum >= Integer.MAX_VALUE) {
throw new RuntimeException("二叉树已满!");
}

}
if ((2 * index + 1) <= data.length - 1) {
node.leftNode = insert(data, node.leftNode, 2 * index + 1);
}
if ((2 * index + 2) <= data.length - 1) {
node.rightNode = insert(data, node.rightNode, 2 * index + 2);
}
return node;
}
/**
* 按广度遍历二叉树
* @return
*/
public List<BinaryNode> iterator(){
return iterator(root);
}
private List<BinaryNode> iterator(BinaryNode<T> node) {
List<BinaryNode> results = new ArrayList<BinaryNode>();

Queue<BinaryNode> q = new ConcurrentLinkedDeque<BinaryNode>();
q.offer(node);
while (!q.isEmpty()) {
BinaryNode n = q.poll();
if (n != null){
results.add(n);
}

if (n.leftNode != null) {
q.offer(n.leftNode);
}
if (n.rightNode != null) {
q.offer(n.rightNode);
}
}
return results;
}

/**
* 查找指定节点
* @param node
* @return
*/
public BinaryNode<T> findNode(T data){
findNode(data,root);
return result;
}
private void findNode(T data, BinaryNode<T> node) {
if (node == null) {
return;
}
if (data == null) {
return;
}
if (node.data != data) {
findNode(data,node.leftNode);//向左查找
findNode(data,node.rightNode);//向右查找
} else {
result = node;//找到时,把结果传递给result
}
}

/**
* 树的元素个数
* @return
*/
public int size(){
return nodeNum;
}
/**
* 返回树的深度
* @return
*/
public int deep(){
return (int) (Math.log(nodeNum+1) / Math.log(2));
}
}

二叉树节点对象:

package com.txq.dataStructure;
/**
* 二叉树节点对象
* @author TongXueQiang
* @date 2016/05/09
* @param <T>
*/
public class BinaryNode<T> {
public T data;
BinaryNode<T> leftNode;
BinaryNode<T> rightNode;

public BinaryNode() {
}

public BinaryNode(T data) {
this.data = data;
}

@Override
public String toString() {
return "Node [data=" + data + ", leftNode=" + leftNode + ", rightNode=" + rightNode + "]";
}

}

测试类:

package com.txq.dataStructure.test;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.txq.dataStructure.BinaryNode;
import com.txq.dataStructure.LinkedBinTree;

public class DataTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

// @Test
// public void test() {
// SequenceList<Integer> list = new SequenceList<Integer>();
// list.add(0);
// list.add(2);
// list.add(4);
// list.add(7);
// System.out.println(list.get(2));
// list.remove(2);
// System.out.println(list.get(2));
// list.insert(56,10);
// System.out.println(list);
// System.out.println(list.locate(7));
// }
@Test
public void testLinkedBinTree(){
LinkedBinTree<Integer> binTree = new LinkedBinTree<Integer>();
Integer[]data = {12,23,54,65,13,54,32};
binTree.insert(data);
System.out.println(binTree.size());

BinaryNode<Integer> node = binTree.findNode(23);
System.out.println(node.toString());
System.out.println("树的深度:"+binTree.deep());

List<BinaryNode> nodes = binTree.iterator();
for (BinaryNode no : nodes) {
System.out.print(no.data + " ");
}
}
}

具体解说,忽略了,你懂得!本人不善于表达,喜欢用代码交流。

时间: 2024-08-03 18:07:17

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

Java实现链式存储的二叉树

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

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

头文件: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()

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

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

二叉树的创建与遍历(链式存储)

这里采用的是链式存储,每个结点包含三个属性(指向左右孩子的指针和本结点的数据),如果想了解顺序存储二叉树,可以参考http://www.cnblogs.com/-beyond/p/6065189.html 采用先序递归创建二叉树,叶子的左右孩子链域为NULL 输入的顺序为:abd--e--c-f--   (-表示空一个空格) #include<iostream> #include<cstdio> using namespace std; struct BiTNode{ BiTNod

10 二叉树-链式存储-递归遍历

终于进入非线性数据结构的第一站了! 先从简单的开始回忆起来吧! 1.二叉树的链式存储 用一个链表来存储一颗二叉树,每一个结点用链表的一个链结点来存储. 通常地,一个二叉链表至少包含3个域:数据域data.左指针域lchild.右指针域rchild. 现实应用的过程中,可以按照自己的需求添加其他指针域. 1 typedef struct BitNode{ 2 int data; 3 struct BitNode *lchild,*rchild; 4 }BitNode,*BiTree; 2.遍历 二

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

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

二叉树链式存储结构

二叉链表的C语言描述 基本运算的算法--建立二叉链表.先序遍历二叉树.中序遍历二叉树.后序遍历二叉树.后序遍历求二叉树深度 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

二叉树链式存储和遍历

1 二叉树的链式存储 1.1 链式存储 顺序存储对空间利用率较低,所以,二叉树一般采用链式存储结构,用一个链表来存储一颗二叉树.二叉链表至少包含3个域:数据域data,左指针域lchild和右指针域rchild,如果再加上一个指向双亲结点的指针就变成了三叉链表. 二叉树的链式存储结构如下: /** * 二叉链表结点 * @author cyhe */ private class Node{ Integer data; Node lchild, rchild; } 根据完全二叉树的序列递归创建二叉

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("请输入一个根节点的数