码迷,mamicode.com
首页 > 其他好文 > 详细

链式存储的二叉树

时间:2016-05-10 16:45:20      阅读:503      评论:0      收藏:0      [点我收藏+]

标签:

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 + " ");
}
}
}

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

链式存储的二叉树

标签:

原文地址:http://www.cnblogs.com/txq157/p/5477985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!