- 浏览: 2574315 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Interview(4)Sequence Iterator and Tree
Sequence
public interface Sequence extends Vector, List {}
public class SequenceDLNode extends ListDLNode implements Sequence {
protected void checkRank(int r, int n) throws ExceptionBoundaryViolation {
if ( r<0 || r >=n){
throw new ExceptionBoundaryViolation(“Error: r should between 0 -> n”);
}
}
//O(n)
public Position rank2Pos(int r) throws ExceptionBoundaryViolation {
DLNode node;
checkRank(r, getSize());
if(r <=getSize()/2){ // r is small, from header
node = header.getNext();
for( int i = 0;i<r;i++) {
node = node.getNext();
}
}else{
// r is big, from trailer
node = trailer.getPrev();
for (int i = 1;i<getSize() -r; i++) {
node = node.getPrev();
}
}
}
public int post2Rank(Position p) throws ExceptionPositionInvalid {
DLNode node = header.getNext();
int r = 0;
while (node != trailer){
if(node == p) {
return (r);
}
node = node.getNext();
r++;
}
throw new ExceptionPositionInvalid(“error: p is not in sequence”);
}
//O(n)
public Object getAtRank(int r) throws ExceptionBoundaryViolation {
checkRank(r, getSize());
return rank2Pos(r).getElem();
}
public Object replaceAtRank(int r, Object obj) throws ExceptionBoundaryViolation {
checkRank(r, getSize());
return replace(rank2Pos(r), obj);
}
public Object insertAtRank(int r, Object obj) throws ExceptionBoundaryViolation {
checkRank(r, getSize() +1);
if (getSize() == r ) { insertLast(obj); }
else { insertBefore(rank2Pos(r), obj); }
return obj;
}
public Object removeAtRank(int r) throws ExceptionBoundaryViolation {
checkRank(r, getSize());
return remove(rank2Pos(r));
}
}
Iterator
hasNext()
getNext()
java.util.Iterator
java.util.Enumeration - hasMoreElements() nextElement()
Iterator Implementation
public class IteratorPosition implements Iterator {
private List list; //
private Position nextPosition;
public IteratorPosition() { list = null; }
public IteratorPosition(List l) {
list = l;
if(list.isEmpty()){
nextPosition = null;
}else{
nextPosition = list.first();
}
}
public boolean hasNext() { return (nextPosition != null); }
public Object getNext() throws ExceptionNoSuchElement {
if(!hasNext()) throw new ExceptionNoSuchElement(“Error, no next element");
Position currentPosition = nextPosition;
if(currentPosition == list.last()){
//reach the end
nextPosition = null;
}else{
nextPosition = list.getNext(currentPosition);
}
return currentPosition;
}
}
Tree
Most important data structure, Array and LinkedList
Array is built for lookup the rank, index. Insert remove will be harder.
LinkedList is built for insert/remove, but it will be hard for lookup and iteration.
Array and LinkedList are linear structures.
Tree - concepts - Root, Parent, Node, Child, Edge, Sibling, Degree, Internal Node, External node(Leaf), Path
- Ancestor, Descendent, subtree, Empty tree,
Ordered Tree
M Branch Tree
Binary Tree - Proper Binary Tree, Improper Binary Tree
Full Binary Tree
Tree Class - parent, data, firstChild, nextsibling
public class TreeLinkedList implements Tree {
private Object element; //root node
private TreeLinkedList parent, firstChild, nextSibling;
//single node tree
public TreeLinkedList(){
this(null, null, null, null);
}
public TreeLinkedList(Object e, TreeLinkedList p, TreeLinkedList c, TreeLinkedList s){
element = e;
parent =p;
firstChild = c;
nextSibling = s;
}
public Object getElem(){ return element; }
public Object setElement(Object obj) {
Object bak = element;
element = obj;
return bak;
}
//return parent node; for root, return null
public TreeLinkedList getParent(){ return parent; }
//return first child, if no children, return null
public TreeLinkedList getFirstChild(){
return firstChild;
}
//return next sibling, return null if no sibling
public TreeLinkedList getNextSibling(){
return nextSibling;
}
// branches size
public int getSize(){
int size = 1; //current node is its own child
TreeLinkedList subtree = firstChild;
while (null != subtree) {
size += subtree.getSize();
subtree = subtree.getNextSibling();
}
return size;
}
public int getHeight(){
int height = -1;
TreeLinkedList subtree = firstChild;
while ( null != subtree) {
height = Math.max(height, subtree.getHeight()); //get the max height of children
subtree = subtree.getNextSibling();
}
return height+1;
}
public int getDepth(){
int depth = 0;
TreeLinkedList p = parent;
while(null != p) {
depth++;
p = p.getParent();
}
return depth;
}
}
Traversal
Preorder traversal - Postorder traversal
Method: PreorderTraversal(v)
Inputs: tree node v
Outputs: all v children preorder traversal
if(null != v){
for(u = v.getFirstChild(); null != u; u = u.getNextSibling()){
PreorderTraversal(u);
}
}
Method: PostorderTraversal(v)
inputs: tree node v
outputs: children of v postorder traversal
{
if(null != v) {
for ( u = v.getFirstChild(); null != u; u= u.getNextSibling()) {
PostorderTraversal(u);
}
//visit v after visit all children
}
}
Traversal by Level
Method: LevelorderTraversal(v)
Inputs: tree node v
Output: Tree node v, traversal by level
{
if ( null != v) {
//create a queue
Q.enqueue(v); //root node in queue
while (!Q.isEmpty()){
u = Q.dequeue();
//visit u
for( w = u.getFirstChild(); null != w; w= w.nextSibling()) {
Q.enqueue(w);
} //for
} //while
} //if
}
IteratorTree Class
public class IteratorTree implements Iterator {
private List list;
private Position nextPosition;
public IteratorTree() {
list = null;
}
public void elementsPreorderIterator(TreeLinkedList T) {
if(null == T){ return ; }
list.insertLast(T); //current node
TreeLinkedList subtree = T.getFirstChild(); //first child
while( null != subtree) {
this.elementsPreorderIterator(subtree);
subtree = subtree.getNextSibling();
}
}
public void elementsPostorderIterator(TreeLinkedList T){
if ( null == T) { return ; }
TreeLinkedList subtree = T.getFirstChild();
while (null != subtree){
this.elementsPostorderIterator(subtree);
subtree = subtree.getNextSibling();
}
list.insertLast(T); //visit current/‘root’ after loop
}
public void levelTraversalIterator(TreeLinkedList T) {
if ( null == T) { return ; }
QueueList queue = new QueueList();
queue.enqueue(T);
while(!queue.isEmpty()){
TreeLinkedList tree = (TreeLinkedList) (queue.dequeue());
list.insertLast(tree);
TreeLinkedList subtree = tree.getFirstChild();
while(null != subtree) {
queue.enqueue(subtree);
subtree = subtree.getNextSibling();
}
}
}
public boolean hasNext() { return (null != nextPosition); }
public Object getNext() throws ExceptionNoSuchElement {
if(!hasNext()) { throw new ExceptionNoSuchElement(“No next position”); }
Position currentPosition = nextPosition;
if(currentPosition == list.last()){
nextPosition = null;
}else{
nextPosition = list.getNext(currentPosition);
}
return currentPosition.getElem();
}
}
O(n) for preorder/postorder/by level traversal
References:
http://sillycat.iteye.com/blog/2380702
Sequence
public interface Sequence extends Vector, List {}
public class SequenceDLNode extends ListDLNode implements Sequence {
protected void checkRank(int r, int n) throws ExceptionBoundaryViolation {
if ( r<0 || r >=n){
throw new ExceptionBoundaryViolation(“Error: r should between 0 -> n”);
}
}
//O(n)
public Position rank2Pos(int r) throws ExceptionBoundaryViolation {
DLNode node;
checkRank(r, getSize());
if(r <=getSize()/2){ // r is small, from header
node = header.getNext();
for( int i = 0;i<r;i++) {
node = node.getNext();
}
}else{
// r is big, from trailer
node = trailer.getPrev();
for (int i = 1;i<getSize() -r; i++) {
node = node.getPrev();
}
}
}
public int post2Rank(Position p) throws ExceptionPositionInvalid {
DLNode node = header.getNext();
int r = 0;
while (node != trailer){
if(node == p) {
return (r);
}
node = node.getNext();
r++;
}
throw new ExceptionPositionInvalid(“error: p is not in sequence”);
}
//O(n)
public Object getAtRank(int r) throws ExceptionBoundaryViolation {
checkRank(r, getSize());
return rank2Pos(r).getElem();
}
public Object replaceAtRank(int r, Object obj) throws ExceptionBoundaryViolation {
checkRank(r, getSize());
return replace(rank2Pos(r), obj);
}
public Object insertAtRank(int r, Object obj) throws ExceptionBoundaryViolation {
checkRank(r, getSize() +1);
if (getSize() == r ) { insertLast(obj); }
else { insertBefore(rank2Pos(r), obj); }
return obj;
}
public Object removeAtRank(int r) throws ExceptionBoundaryViolation {
checkRank(r, getSize());
return remove(rank2Pos(r));
}
}
Iterator
hasNext()
getNext()
java.util.Iterator
java.util.Enumeration - hasMoreElements() nextElement()
Iterator Implementation
public class IteratorPosition implements Iterator {
private List list; //
private Position nextPosition;
public IteratorPosition() { list = null; }
public IteratorPosition(List l) {
list = l;
if(list.isEmpty()){
nextPosition = null;
}else{
nextPosition = list.first();
}
}
public boolean hasNext() { return (nextPosition != null); }
public Object getNext() throws ExceptionNoSuchElement {
if(!hasNext()) throw new ExceptionNoSuchElement(“Error, no next element");
Position currentPosition = nextPosition;
if(currentPosition == list.last()){
//reach the end
nextPosition = null;
}else{
nextPosition = list.getNext(currentPosition);
}
return currentPosition;
}
}
Tree
Most important data structure, Array and LinkedList
Array is built for lookup the rank, index. Insert remove will be harder.
LinkedList is built for insert/remove, but it will be hard for lookup and iteration.
Array and LinkedList are linear structures.
Tree - concepts - Root, Parent, Node, Child, Edge, Sibling, Degree, Internal Node, External node(Leaf), Path
- Ancestor, Descendent, subtree, Empty tree,
Ordered Tree
M Branch Tree
Binary Tree - Proper Binary Tree, Improper Binary Tree
Full Binary Tree
Tree Class - parent, data, firstChild, nextsibling
public class TreeLinkedList implements Tree {
private Object element; //root node
private TreeLinkedList parent, firstChild, nextSibling;
//single node tree
public TreeLinkedList(){
this(null, null, null, null);
}
public TreeLinkedList(Object e, TreeLinkedList p, TreeLinkedList c, TreeLinkedList s){
element = e;
parent =p;
firstChild = c;
nextSibling = s;
}
public Object getElem(){ return element; }
public Object setElement(Object obj) {
Object bak = element;
element = obj;
return bak;
}
//return parent node; for root, return null
public TreeLinkedList getParent(){ return parent; }
//return first child, if no children, return null
public TreeLinkedList getFirstChild(){
return firstChild;
}
//return next sibling, return null if no sibling
public TreeLinkedList getNextSibling(){
return nextSibling;
}
// branches size
public int getSize(){
int size = 1; //current node is its own child
TreeLinkedList subtree = firstChild;
while (null != subtree) {
size += subtree.getSize();
subtree = subtree.getNextSibling();
}
return size;
}
public int getHeight(){
int height = -1;
TreeLinkedList subtree = firstChild;
while ( null != subtree) {
height = Math.max(height, subtree.getHeight()); //get the max height of children
subtree = subtree.getNextSibling();
}
return height+1;
}
public int getDepth(){
int depth = 0;
TreeLinkedList p = parent;
while(null != p) {
depth++;
p = p.getParent();
}
return depth;
}
}
Traversal
Preorder traversal - Postorder traversal
Method: PreorderTraversal(v)
Inputs: tree node v
Outputs: all v children preorder traversal
if(null != v){
for(u = v.getFirstChild(); null != u; u = u.getNextSibling()){
PreorderTraversal(u);
}
}
Method: PostorderTraversal(v)
inputs: tree node v
outputs: children of v postorder traversal
{
if(null != v) {
for ( u = v.getFirstChild(); null != u; u= u.getNextSibling()) {
PostorderTraversal(u);
}
//visit v after visit all children
}
}
Traversal by Level
Method: LevelorderTraversal(v)
Inputs: tree node v
Output: Tree node v, traversal by level
{
if ( null != v) {
//create a queue
Q.enqueue(v); //root node in queue
while (!Q.isEmpty()){
u = Q.dequeue();
//visit u
for( w = u.getFirstChild(); null != w; w= w.nextSibling()) {
Q.enqueue(w);
} //for
} //while
} //if
}
IteratorTree Class
public class IteratorTree implements Iterator {
private List list;
private Position nextPosition;
public IteratorTree() {
list = null;
}
public void elementsPreorderIterator(TreeLinkedList T) {
if(null == T){ return ; }
list.insertLast(T); //current node
TreeLinkedList subtree = T.getFirstChild(); //first child
while( null != subtree) {
this.elementsPreorderIterator(subtree);
subtree = subtree.getNextSibling();
}
}
public void elementsPostorderIterator(TreeLinkedList T){
if ( null == T) { return ; }
TreeLinkedList subtree = T.getFirstChild();
while (null != subtree){
this.elementsPostorderIterator(subtree);
subtree = subtree.getNextSibling();
}
list.insertLast(T); //visit current/‘root’ after loop
}
public void levelTraversalIterator(TreeLinkedList T) {
if ( null == T) { return ; }
QueueList queue = new QueueList();
queue.enqueue(T);
while(!queue.isEmpty()){
TreeLinkedList tree = (TreeLinkedList) (queue.dequeue());
list.insertLast(tree);
TreeLinkedList subtree = tree.getFirstChild();
while(null != subtree) {
queue.enqueue(subtree);
subtree = subtree.getNextSibling();
}
}
}
public boolean hasNext() { return (null != nextPosition); }
public Object getNext() throws ExceptionNoSuchElement {
if(!hasNext()) { throw new ExceptionNoSuchElement(“No next position”); }
Position currentPosition = nextPosition;
if(currentPosition == list.last()){
nextPosition = null;
}else{
nextPosition = list.getNext(currentPosition);
}
return currentPosition.getElem();
}
}
O(n) for preorder/postorder/by level traversal
References:
http://sillycat.iteye.com/blog/2380702
发表评论
-
Interview(8)Dictionary and Tree
2017-07-28 04:22 513Interview(8)Dictionary and Tree ... -
Interview(7)Map and Hash Table
2017-07-21 06:10 617Interview(7)Map and Hash Table ... -
Interview(6)Priority Queue
2017-07-15 04:41 525Interview(6)Priority Queue Pri ... -
Interview(5)Binary Tree
2017-07-06 04:23 643Interview(5)Binary Tree Binary ... -
Interview(3)Vector List
2017-06-21 04:06 619Interview(3)Vector List Stack ... -
Interview(2)Stack and Queue
2017-06-13 04:15 601Interview(2)Stack and Queue St ... -
Interview(1)Algorithm Book
2017-06-09 04:37 578Interview(1)Algorithm Book Cha ... -
Mysql Database Event and Procedure(1)MySQL Event
2016-12-07 03:42 707Mysql Database Event and Proced ... -
Algorithm Basic(1)Algorithm in Java
2014-10-02 12:20 671Algorithm Basic(1)Algorithm i ... -
Search and Parse Keyword(1)JACKSON for JSON and Jsoup for URL Fetch
2014-09-23 07:13 1322Search and Parse Keyword(1)JA ... -
Compiler Principle(2)Go and NodeJS
2014-08-06 22:10 612Compiler Principle(2)Go and Nod ... -
JAVA基础(一)equals和==和hashCode
2010-06-07 22:32 1336JAVA基础(一)equals和==和hashCode 根据 ... -
ThreadLocal笔记
2010-01-05 10:15 1312ThreadLocal笔记 网上搜索的结果,实践验证后记录 ... -
摘:JAVA面试(四)
2010-01-05 10:15 1394摘:JAVA面试(四) 1、RDBM ... -
摘:JAVA面试题目(三)
2010-01-05 10:14 1305JAVA面试题目(三) 1、两个对象值相同(x.equals ... -
摘:JAVA面试题目(二)
2010-01-05 10:14 1498JAVA面试题目(二) 1、erro ... -
摘:JAVA面试题目(一)
2010-01-05 10:14 1499JAVA面试题目(一) 水平一流,但往往栽在基础知识的问题上 ...
相关推荐
这是第二部分Bioinformatics. Sequence and Genome Analysis
An inorder binary tree traversal ... Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
在Microsoft Dynamics AX(现称为Dynamics 365 Finance and Operations)这款企业资源规划(ERP)软件中,"Number Sequence"功能尤为关键。下面将详细解释如何在AX中使用Number Sequence。 首先,我们需要理解...
4. 验证与测试:设计完成后,可以使用 Activiti 的API或工作流引擎进行模拟运行,检查流程是否按照预期进行。 四、SequenceFlow的应用场景 1. 逻辑分支:SequenceFlow 可以与决策网关配合,实现基于不同条件的流程...
《序列图绘制工具sequence-diagram-js的深度解析与应用》 序列图,作为一种重要的系统建模工具,广泛应用于软件设计和开发中,它清晰地展示了系统内各对象间交互的顺序。sequence-diagram-js是一个基于JavaScript的...
根据提供的信息,《Magnetic Resonance Imaging - Physical Principles and Sequence Design》是一本关于磁共振成像(MRI)的经典教材。本书由Ernst M. Haacke等专家编写,旨在为读者提供深入理解MRI物理原理及序列...
In this paper we examine the question of designing and allocating transmission sequences to users in a mobile ad hoc network that has no spatially boundary. A basic tenet of the transmission sequence ...
WHERE SUBSTR(a.sequence_name, 5, 100) = b.table_name AND b.constraint_type = 'P' ) LOOP SELECT func_getseq(cur.table_name) INTO max1 FROM DUAL; EXECUTE IMMEDIATE 'DROP SEQUENCE ' || cur.sequence_...
《生物信息学:序列与基因组分析》是D.W Mount所著的一本深入探讨生物信息学领域的经典之作,尤其在蛋白质和DNA序列分析方面提供了详尽的指导。本书不仅覆盖了历史背景、理论基础,还介绍了多种实用的分析工具和技术...
RNA序列、结构和功能的计算与生物信息学方法 RNA(核糖核酸)是生物信息学研究中的一个重要对象,它在遗传信息的传递、基因表达的调控以及蛋白质合成等生命过程中扮演着关键角色。随着二代测序技术的飞速发展,RNA...
4. **NO CACHE**:为了防止数据丢失,可以在创建`sequence`时使用`NOCACHE`选项。 #### 六、修改Sequence 要修改`sequence`的属性,需要拥有该`sequence`的所有权或者`ALTER ANY SEQUENCE`权限。可以通过`ALTER ...
SequenceDiagram-3.0.5.zip 是一个包含 Sequence Diagram 相关工具或资源的压缩包文件,主要用于绘制序列图,这是UML(统一建模语言)中的一种图表,用于描述对象之间的交互和消息传递顺序。在软件开发过程中,序列...
《Sequence to Sequence Learning with Neural Networks》是一篇由Ilya Sutskever, Oriol Vinyals和Quoc V. Le共同撰写的论文,三位作者都来自Google公司。这篇论文在自然语言处理领域有着重要的影响,特别是在序列...
在“list and sequence table .zip_OJ4_Table_list_sequence”这个压缩包中,包含了对这两种数据结构基本操作的实现,主要通过C++语言进行编写,文件名为“顺序表.cpp”和“链表.cpp”。 首先,我们来了解链表。...
nature,2011,Genome sequence and analysis of the tuber crop potato.pdf
memory networks, are extremely appealing for sequence-tosequence learning tasks. Despite their great success, they typically suffer from a fundamental shortcoming: they are prone to generate ...
4. **更新插入操作**:在进行插入操作时,不再显式指定`IDENTITY`列的值,而是依赖于`Sequence`。 ```sql INSERT INTO Employees (FirstName, LastName) VALUES ('John', 'Doe'); ``` #### 表的迁移 除了`...
Invalid Multibyte Character Sequence 警告解析 在编程中,特别是在嵌入式系统开发中,我们经常会遇到Invalid Multibyte Character Sequence 警告。这个警告通常来自于编译器,告知我们存在非法的多字节字符序列。...
机器学习之sequence to sequence learning。(Sequence Generation-----Hung-yi Lee 李宏毅.ppt)