数据结构:广义表的实现(Java)
广义表的简单理解在这篇博文中:https://blog.csdn.net/lishanleilixin/article/details/87364496,在此不做赘述。
Java实现广义表:
package 广义表;
import java.util.Stack;
public class Test {
public final int TAG_TABLE = 1;
public final int TAG_ITEM = 0;
private char mStartSymb = '(';
private char mEndSymb = ')';
private Node mGenTable;
class Node {
int tag;
Object data;
Node mPh;
Node mPt;
public Node(Node ph, Node pt, int tag, Object data) {
this.mPh = ph;
this.mPt = pt;
this.tag = tag;
this.data = data;
}
}
public Test(String genTable) {
if(genTable == null)
throw new NullPointerException("genTable is null");
initTable(genTable);
}
public Test() {
mGenTable = new Node(null, null, TAG_TABLE, null);
}
public Test(Test b) {
if(b != null) {
mGenTable = b.mGenTable;
}
}
public void initTable(String genTable) {
String ts = genTable.replaceAll("\\s", "");
int len = ts.length();
Stack<Character> symStack = new Stack<Character>();
Stack<Node> nodeStack = new Stack<Node>();
initSymbolicCharactor(ts);
mGenTable = new Node(null, null, TAG_TABLE, null);
Node itemNode, tableNode = mGenTable, tmpNode;
for (int i = 0; i < len; i++) {
if(ts.charAt(i) == mStartSymb) {
tmpNode = new Node(null, null ,TAG_TABLE, null);
symStack.push(ts.charAt(i));
if(symStack.size() > 1) {
nodeStack.push(tableNode);
tableNode.mPh = tmpNode;
tableNode = tableNode.mPh;
}
else {
tableNode.mPt = tmpNode;
tableNode = tableNode.mPt;
}
}
else if(ts.charAt(i) == mEndSymb) {
if(symStack.isEmpty()) {
throw new NullPointerException(
"IllegalArgumentException in constructor GeneralizedTable!...");
}
if(symStack.size() > 1) {
tableNode = nodeStack.pop();
}
symStack.pop();
}
else if(ts.charAt(i) == ',') {
tableNode.mPt = new Node(null, null, TAG_TABLE, null);
tableNode = tableNode.mPt;
}
else {
itemNode = new Node(null, null, TAG_ITEM, ts.charAt(i));
tableNode.mPh = itemNode;
}
}
if(!symStack.isEmpty()) {
throw new NullPointerException(
"IllegalArgumentException in constructor GeneralizedTable!...");
}
}
public void initSymbolicCharactor(String ts) {
mStartSymb = ts.charAt(0);
switch (mStartSymb) {
case '(':
mEndSymb = ')';
break;
case '{':
mEndSymb = '}';
break;
case '[':
mEndSymb = ']';
break;
default:
throw new IllegalArgumentException(
"IllegalArgumentException ---> initSymbolicCharactor");
}
}
public void print() {
print(mGenTable);
}
private void print(Node node) {
if(node == null) return;
if(node.tag == 0) System.out.print(node.data.toString() + "\t");
print(node.mPh);
print(node.mPt);
}
public int depth() {
if(mGenTable == null)
throw new NullPointerException("Generalized Table is null !.. ---> method depth");
return depth(mGenTable);
}
private int depth(Node node) {
if(node == null || node.tag == 0) return 0;
int depHeader = 0, depTear = 0;
depHeader = 1 + depth(node.mPh);
depTear = depth(node.mPt);
return depHeader > depTear ? depHeader : depTear;
}
public int length() {
if(mGenTable == null || mGenTable.mPt == null) return -1;
int len = 0;
Node node = mGenTable;
while(node.mPt != null) {
node = node.mPt;
if(node.mPh == null && node.mPt == null) break;
len++;
}
return len;
}
public Test getHeader() {
if(isEmpty()) return null;
Node node = mGenTable.mPt;
Test test = new Test();
test.mGenTable.mPt = node.mPh;
return test;
}
public Test getTear() {
if(mGenTable == null) return null;
Node node = mGenTable.mPt;
Test test = new Test();
test.mGenTable.mPt = node.mPt;
return test;
}
public boolean isEmpty() {
if(mGenTable == null) return true;
Node node = mGenTable.mPt;
return node.mPh == null || node.mPt == null;
}
public static void main(String[] args) {
Test test = new Test("(c,a,b,(a,b,c),(a,(a,b),c))");
test.print();
System.out.println();
System.out.println("该广义表的深度为:" + test.depth());
System.out.println("该广义表的长度为:" + test.length());
Test t2 = test.getTear();
t2.print();
}
}
数据结构:广义表的实现(Java)的更多相关文章
- 【C/C++】实现数据结构广义表
1. 广义表的定义 每个元素可以为Atom,原子,也可以为线性表. 线性表的推广.线性表元素有唯一的前驱和后继,为线性表,而广义表是多层次的线性表 表头:第一个元素,可能是 ...
- 数据结构(C语言第2版)-----数组,广义表,树,图
任何一个算法的设计取决于选定的数据结构,而算法的实现依赖于采用的存储结构. 之前线性表的数据元素都是非结构的原子类型,元素的值是不可再分的.下面学习的这两个线性表是很特殊的,其中数据元素本身也可能是一 ...
- 数据结构算法C语言实现(十九)--- 5.5&5.6&5.7广义表
一.简述 传说Lisp的基本数据结构就是广义表,广义表也是具有典型递归属性的数据结构,此外,由于建表要处理字符串,用C语言处理起来也是一脸懵逼.....最后自己还想写一个将广义表还原成字符串的函数,一 ...
- javascript实现数据结构:广义表
原文:javascript实现数据结构:广义表 广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...
- 数据结构(C语言版)-第4章 串、数组和广义表
补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...
- 数据结构28:广义表及M元多项式
广义表,又称为列表.记作: LS = (a1,a2,…,an) ;( LS 为广义表的名称, an 表示广义表中的数据). 广义表可以看作是线性表的推广.两者区别是:线性表中的数据元素只能表示单个数据 ...
- 数据结构之---C语言实现广义表头尾链表存储表示
//广义表的头尾链表存储表示 //杨鑫 #include <stdio.h> #include <malloc.h> #include <stdlib.h> #in ...
- 数据结构 c++ 广义表
// CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- 广义表操作 (ava实现)——广义表深度、广义表长度、打印广义表信息
广义表是对线性表的扩展——线性表存储的所有的数据都是原子的(一个数或者不可分割的结构),且所有的数据类型相同.而广义表是允许线性表容纳自身结构的数据结构. 广义表定义: 广义表是由n个元素组成的序列: ...
随机推荐
- CloudStack 4.1快照测试
目前4.1只支持存储快照,4.2能支持内快照 1. 选中实例 2. 查看实例卷 3. 创建快照 4. 通过快照创建模板 5. 查看通过快照创建的模板 6. 通过快照创建模板生成的实例 7. 自动定制创 ...
- Linux confluence5.8.10 wiki安装
选择首先需要java环境 其次需要mysql mysql安装请参考: http://www.cnblogs.com/syuf/p/7818710.html 安装好mysql之后,创建一个库 # mys ...
- Smart Pointe
http://blog.chinaunix.net/uid-625789-id-2720884.html
- R语言中的四类统计分布函数
R语言中提供了四类有关统计分布的函数(密度函数,累计分布函数,分位函数,随机数函数).分别在代表该分布的R函数前加上相应前缀获得(d,p,q,r).如: 1)正态分布的函数是norm,命令dnorm( ...
- Thrift结构分析及增加取客户端IP功能实现
目录 目录 1 1. 前言 1 2. 示例Service 1 3. 网络部分类图 2 4. 线程模式 3 4.1. IO线程 3 4.2. 工作线程 4 4.2.1. 工作线程类图 4 4.2.2. ...
- 阿里杨传辉的访问节选(oceanbase)
皮皮(Q4): OceanBase第一个应用是收藏夹.最近,听说支付宝交易也用到了OceanBase.能否结合阿里的应用谈谈OceanBase的优势. 杨传辉(A4):相比传统的关系数据库,谈及Oce ...
- 软件工程:java实现wordcount基本功能
github链接:https://github.com/Nancy0611/wc 一:项目相关要求 该项目能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的功 ...
- [label][Apache] VirtualHost
<VirtualHost *:80> ServerName localhost DocumentRoot "D:\www"</VirtualHos ...
- SqlServer循环执行存储过程
begin --申明变量 ) declare @zycs int --赋值变量 --申明游标 declare order_cursor cursor for (select blh, zycs fro ...
- 执行计划--WHERE条件的先后顺序对执行计划的影响
在编写SQL时,会建议将选择性高(过滤数据多)的条件放到WHERE条件的前面,这是为了让查询优化器优先考虑这些条件,减少生成最优(或相对最优)的执行计划的时间,但最终的执行计划生成过滤顺序还是决定这些 ...