资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

决策树Java代码实现 决策树 java

二分查找法的判定树有什么特点?能不能用一个公式直接求出树的深度?

算法思想:

10年积累的网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有安远免费网站建设让你可以放心的选择与我们合作。

将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小于该中点元素,则将待查序列纯态纤缩小为左半部分,否则为右半部分。通过一次比较,将查找区间缩小一半。

折半查找是一种高效的查找方法。它可以明显减少比较次数,提高查找效率。但是,折半查找的先决条件是查找表中的数据元素必须有序。

算法步骤描述:

step1 首先确定整个查找区间的中间位置

mid = ( left + right )/ 2

step2 用待查关键字值与中间位置的关键字值进行比较;

若相等,则查找成功

若大于,则在后(右)半个区域继续进行折半查找

若小于,则在前(左)半个区域继续进行折半查找

Step3 对确定的缩小区域再按折半公式,重复上述步骤。最后,得到结果:要么查找成功, 要么查找失败。

折半查找的存储结构采用一维数组存放。

折半查找算法举例

对给定数列(有序),按折半查找算法,查找关键字值为30的数据元素。

折半查找的算法讨论:

优点: ASL≤log2n,即每经过一次比较,查找范围就缩小一半。经log2n 次计较就可以完成查找过程。

缺点:因要求有序,所以要求查找数列必须有序,而对所有数据元素按大小排序是非常费时的操作。另外,顺序存储结构的插入、删除操作不便利。

考虑:能否通过一次比较抛弃更多的部分闭唤(即经过一次比较,使查找范围缩得更小),以达到提高效率的目的。……?

可以考虑把两种方法(顺序查找和折半查找)结合起来,即取顺序查找简单和折半查找高效之所长,来达到提高效率的目的?实际上这就是分块查找的算法思想。

例如:[问题分析] 由于数据按升序排列,故用折半查找最快捷.

program binsearch;

const max=10;

var num:array[1..max] of integer;

i,n:integer;

procedure search(x,a,b:integer);

var mid:integer;

begin

if a=b then

if x=num[a] then writeln('Found:',a) else writeln('Number not found')

else begin

mid:=(a+b) div 2;

if xnum[mid] then search(x,mid,b);

if xnum[mid] then search(x,a,mid);

if x=num[mid] then writeln('Found:',mid);

end;

end;

begin

write('Please input 10 numbers in order:');

for i:=1 to max do read(num);

write('Please input the number to search:');

readln(n);

search(n,1,max);

end.

Java风格的代码举例:

//使用折半法进行查找

int getIndex(int[] nList, int nCount, int nCode) {

int nIndex = -1;

int jMin = 0;

int jMax = nCount - 1;

int jCur = (jMin+jMax)/2;

do

{

if(nList[jCur] nCode) {

jMax--;

} else if(nList[jCur] nCode) {

jMin++;

} else if(nList[jCur] == nCode) {

nIndex = jCur;

break;

}

jCur = (jMin + jMax)/2;

} while(jMin jMax);

return nIndex;

}

二分查找的性能说明

虽然二分查找的效率高,但是要将表按关键字排序。而排序本身是一种很费时的运算。既使采用高效率的排序方法也做仿要花费 O(n lg n) 的时间。

二分查找只适用顺序存储结构。为保持表的有序性,在顺序结构里插入和删除都必须移动大量的结点。因此,二分查找特别适用于那种一经建立就很少改动、而又经常需要查找的线性表。

对那些查找少而又经常需要改动的线性表,可采用链表作存储结构,进行顺序查找。链表上无法实现二分查找

二分查找的C#实现代码:

using System;

using System.Collections.Generic;

using System.Text;

namespace BinschDemo

{

public class BinschDemo

{

public static int Binsch(int[] a, int key)

{

int low = 1;

int high = a.Length;

while (low = high)

{

int mid = (low + high) / 2;

if (key == a[mid])

{

return mid; //返回找到的索引值

}

else

{

if (key a[mid])

high = mid - 1;

else

low = mid + 1;

}

}

return -1; //查找失败

}

static void Main(string[] args)

{

Console.WriteLine("请输入10个递增数字: ");

int[] list = new int[10];

for (int i = 0; i 10; i++)

{

Console.Write("数字 : ", i);

list = Convert.ToInt32(Console.ReadLine());

}

Console.Write("请输入一个你要查找的数字:");

int find = Convert.ToInt32(Console.ReadLine());

int result = Binsch(list, find);

Console.WriteLine(result);

}

}

}

分块查找又索引查找,它主要用于“分块有序”表的查找。所谓“分块有序”是指将线性表L(一维数组)分成m个子表(要求每个子表的长度相等),且第i+1个子表中的每一个项目均大于第i个子表中的所有项目。“分块有序”表应该包括线性表L本身和分块的索引表A。因此,分块查找的关键在于建立索引表A。

(1)建立索引表A(二维数组)

索引表包括两部分:关键字项(子表中的最大值)和指针项(子表的第一项在线性表L中位置)

索引表按关键字有序的。

例如:线性表L(有序)为:1 2 3 4 5 6 7 8 9 10 11 12

分成m=3个子表:

索引表A:二维数组:第一列为每个子表的最大值 ,第二列为每个子表的起始地址

即: 4 0

8 4

12 8

(2)利用索引表A,确定待查项X所在的子表(块)。

(3)在所确定的子表中可以用“折半查找”法搜索待查项X;若找到则输出X;否则输出未找到信息。

如何使用Java Weka开源项目,实现J48决策树、支持向量机算法,在10个UCI数据集上对这两个算法进行性能?

public static void Regular() throws Exception {

File inputfile = new File("F:\\weka\\eucalyptus_Train.arff");

ArffLoader loader = new ArffLoader();

丛凯 loader.setFile(inputfile);

Instances insTrain = loader.getDataSet();

insTrain.setClassIndex(insTrain.numAttributes()-1);

inputfile = new File("F:\\weka\\eucalyptus_Test.arff");

loader.setFile(inputfile);

Instances insTest = loader.getDataSet();

insTest.setClassIndex(insTest.numAttributes()-1);

double sum = insTest.numInstances();

int right = 0;

Classifier clas = new J48();

//Classifier clas = new weka.classifiers.bayes.BayesNet();

clas.buildClassifier(insTrain);

燃郑碧 for(int i = 0; i  sum; i++) {

皮举          if(clas.classifyInstance(insTest.instance(i)) == insTest.instance(i).classValue()) {

right++;

}

System.out.println(clas.classifyInstance(insTest.instance(i))+" : "+insTest.instance(i).classValue());

}

System.out.println("分类准确率:"+right/sum);

}

svm的话,要用一个wlsvm的包。 代码是一样的,就是Classifier class= new J48()这里要用svm的实例

如何用Java实现树形结构啊?

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历

*

* 参考资料0:数据结构(C语言版)严蔚敏

*

* 参考资料1:

*

* 参考资料2:

*

* @author ocaicai@yeah点虐 @date: 2011-5-17

*

*/

public class BinTreeTraverse2 {

private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

private static ListNode nodeList = null;

/**

* 内部类:节点

*

* @author ocaicai@yeah点虐 @date: 2011-5-17

*

*/

private static class Node {

Node leftChild;

Node rightChild;

int data;

Node(int newData) {

leftChild = null;

rightChild = null;

data = newData;

}

}

public void createBinTree() {

nodeList = new LinkedListNode();

// 将一个数组的值依次转换为Node节点

for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {

nodeList.add(new Node(array[nodeIndex]));

}

// 对前lastParentIndex-1个父节点按照父节点与孩子核卜陪节点的数弊历字关系建立二叉树

for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {

// 左孩子

nodeList.get(parentIndex).leftChild = nodeList

.get(parentIndex * 2 + 1);

// 右孩子

nodeList.get(parentIndex).rightChild = nodeList

.get(parentIndex * 2 + 2);

}

// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理

int lastParentIndex = array.length / 2 - 1;

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1);

// 右孩子,如果数组的长度为奇数才建立右孩子改蠢

if (array.length % 2 == 1) {

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2);

}

}

/**

* 先序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

* 遍历的节点

*/

public static void preOrderTraverse(Node node) {

if (node == null)

return;

System.out.print(node.data + " ");

preOrderTraverse(node.leftChild);

preOrderTraverse(node.rightChild);

}

/**

* 中序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

* 遍历的节点

*/

public static void inOrderTraverse(Node node) {

if (node == null)

return;

inOrderTraverse(node.leftChild);

System.out.print(node.data + " ");

inOrderTraverse(node.rightChild);

}

/**

* 后序遍历

*

* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已

*

* @param node

* 遍历的节点

*/

public static void postOrderTraverse(Node node) {

if (node == null)

return;

postOrderTraverse(node.leftChild);

postOrderTraverse(node.rightChild);

System.out.print(node.data + " ");

}

public static void main(String[] args) {

BinTreeTraverse2 binTree = new BinTreeTraverse2();

binTree.createBinTree();

// nodeList中第0个索引处的值即为根节点

Node root = nodeList.get(0);

System.out.println("先序遍历:");

preOrderTraverse(root);

System.out.println();

System.out.println("中序遍历:");

inOrderTraverse(root);

System.out.println();

System.out.println("后序遍历:");

postOrderTraverse(root);

}

}


当前标题:决策树Java代码实现 决策树 java
网站网址:http://cdkjz.cn/article/ddpopgd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220