用Java实现我自己的树迭代器

用Java实现我自己的树迭代器,java,data-structures,tree,iterator,inorder,Java,Data Structures,Tree,Iterator,Inorder,我正在尝试实现树遍历的迭代器接口。我收到以下错误。“for(Integer node:tr)和“treeIterator.java使用未经检查或不安全的操作”处的不兼容类型。我无法修复此错误。有人能指出问题所在吗 //class to implement the Iterator interace. class InorderItr implements Iterator { public InorderItr(Node root) { st = new Stack&

我正在尝试实现树遍历的迭代器接口。我收到以下错误。“for(Integer node:tr)和“treeIterator.java使用未经检查或不安全的操作”处的不兼容类型。我无法修复此错误。有人能指出问题所在吗

//class to implement the Iterator interace.
class InorderItr implements Iterator {



    public InorderItr(Node root) {
       st = new Stack<Node>();
       this.root = root;
    }

    @Override
    public boolean hasNext() {
        //has Next
    }

    @Override
    public Integer next(){
          //next node
    }

    @Override 
    public void remove(){
        throw new java.lang.UnsupportedOperationException("Remove not supported.");
    }
}

//This class just makes sure that we use the foreach loop.
class InorderTreeIterator implements Iterable {

    Node root = null;

    public InorderTreeIterator(Node root){
        this.root = root;
    }

    @Override
    public Iterator<Integer> iterator(){
        try{
            return new InorderItr(this.root);
        } catch(UnsupportedOperationException e){
            System.out.println(e.getMessage());
            return null;
        }
    }
}


class treeIterator {

    public static void main(String arg[]){
        treeIterator obj = new treeIterator();
        //create tree.
        InorderTreeIterator tr = new InorderTreeIterator(obj.root);
        for(Integer node : tr ){
            System.out.println(node);
        }
    }
}
//类来实现迭代器交互。
类inoorderiter实现迭代器{
公共索引器(节点根){
st=新堆栈();
this.root=根;
}
@凌驾
公共布尔hasNext(){
//下一个
}
@凌驾
公共整数next(){
//下一节点
}
@凌驾
公共空间删除(){
抛出新的java.lang.UnsupportedOperationException(“删除不支持”);
}
}
//这个类只是确保我们使用foreach循环。
类InOrderTreiterator实现Iterable{
节点根=空;
公共索引重新迭代器(节点根){
this.root=根;
}
@凌驾
公共迭代器迭代器(){
试一试{
返回新的索引项(this.root);
}捕获(不支持操作异常e){
System.out.println(e.getMessage());
返回null;
}
}
}
类树运算符{
公共静态void main(字符串arg[]){
treeIterator obj=新的treeIterator();
//创建树。
InorderTreeIterator tr=新的InorderTreeIterator(obj.root);
for(整数节点:tr){
System.out.println(节点);
}
}
}
PS:这是我第一次尝试实现迭代器接口。如果有任何我没有遵循的标准实践,请指出


谢谢

Iterable
是一个界面。这意味着,除非您给它一个类型参数,否则它将是一个原始类型,并且底层数据将被视为
对象

更改此项:

class InorderItr implements Iterator
class InorderTreeIterator implements Iterable
对下列事项:

class InorderItr implements Iterator<Integer>
class InorderTreeIterator implements Iterable<Integer>
类inorderiter实现迭代器
类InOrderTreiterator实现Iterable

这样,它就不再是一个原始类型(并且可以获得编译器当前提供的未检查和不安全操作的警告信息),它告诉编译器,
Iterator
的底层数据类型将是
Integer
(由于
迭代器
接口中的类型参数是其基础数据类型),因此类型匹配。

迭代器是泛型类型。如果使用原始
迭代器
类型,迭代器返回对象,循环应为

for (Object node : tr)
您应该改用类型安全的泛型迭代器

类inorderiter实现迭代器{
...
类InOrderTreiterator实现Iterable{

另外,为类选择更好的名称。
inorderiter
应该是
inorderiiter
inorderiiterator
应该是
inorderiiteratable
。不要命名“迭代器”什么不是迭代器。以大写字母开始类名,以小写字母开始方法名。

假设Java 5或更高版本:迭代器实际上被声明为迭代器,其中“E”表示迭代器上的next()返回的类。我想您想要的是

InOrderIterator implements Iterator<Node>
我不明白为什么您的代码中有整数;很明显,您正在试图迭代一个节点树。

在空间中(类似于)在C中的实现++

#include<iostream>
using namespace std;
struct node
{
    node *left;
    node *right;
    node *parent;
    int data;
} ;

class bst
{
public:
    node *root;
    node *currNode;

    bst()
    {
        root=NULL;
        currNode = NULL;
    }
    int isempty() 
    {
        return(root==NULL);
    }
    void insert(int item);
    void inordertrav();
    void inorder(node *);
    int next();
    node *nextNode(node *root);
    void bft();
};
void bst::insert(int item)
{
    node *p=new node;
    node *parent;
    p->data=item;
    p->left=NULL;
    p->right=NULL;
    p->parent = NULL;
    parent=NULL;
    if(isempty())
        root=p;
    else
    {
        node *ptr;
        ptr = root;
        while(ptr!=NULL)
        {
            parent = ptr;
            if(item > ptr->data)        
                ptr = ptr->right;
            else
                ptr=ptr->left;
        }   
        if(item < parent->data)
        {
            parent->left = p;
        }
        else
            parent->right = p;
        p->parent = parent;
    }
}
void bst::inordertrav()
{
    inorder(root);
}
void bst::inorder(node *ptr)
{
    if(ptr!=NULL)
    {
        inorder(ptr->left);
        cout<<"  "<<ptr->data<<"     ";
        inorder(ptr->right);
    }
}

int bst::next()
{
// If currNode is NULL and find the left most node using a while loop.
    if (currNode == NULL)
    {
        cout << "First node data" << endl;
        node *tmp = root;
        while (tmp->left != NULL)
            tmp = tmp->left;

        currNode = tmp;
        return currNode->data;
    }
    cout << endl << "Current Node is - " << currNode->data << endl;
    if (currNode->right)
    {
        node *tmp = currNode->right;
        while (tmp->left) // find the leftmost node for this right subtree in recursive way without recursion
            tmp = tmp->left;
        currNode = tmp;
        return currNode->data;
    }

    if (! currNode->right) // currNode does not have right child
    {
        if ( currNode->parent->left && currNode->data == currNode->parent->left->data) // CurrNode is the left child
        {
            currNode = currNode->parent;
            return currNode->data;
        }
        if (currNode->parent->right && currNode->data == currNode->parent->right->data) //CurrNode is the right child of the parent
        {
            //If the parent of the currNode (which is right child) is also a right child
            //then this currNode is actually a leaf node and it nothing should be returned.
            if (currNode->parent->parent->right && 
                    currNode->parent->parent->right->data == currNode->parent->data)
            {
                cout << "The tree has been travered fully";
                return -1;
            }
            currNode = currNode->parent->parent;
            return currNode->data;
        }
    }
}


int main()
{
    bst b;
    b.insert(52);
    b.insert(25);
    b.insert(50);
    b.insert(40);
    b.insert(45);
    b.insert(20);
    b.insert(75);
    b.insert(65);
    b.insert(78);
    b.insert(23);
    b.insert(15);

    cout << "---- In order traversal using iterator -----" << endl;
    int i;
    do
    {
        i = b.next();   
        cout << " " << i << " ";
    }while (i != -1);
    cout << endl;
    cout<<endl;
}
#包括
使用名称空间std;
结构节点
{
节点*左;
节点*右;
节点*父节点;
int数据;
} ;
bst级
{
公众:
节点*根;
节点*当前节点;
bst()
{
root=NULL;
currNode=NULL;
}
int isempty()
{
返回(root==NULL);
}
无效插入(整数项);
在ordertrav()中无效;
按顺序无效(节点*);
int next();
节点*下一个节点(节点*根节点);
void bft();
};
void bst::insert(int项)
{
node*p=新节点;
节点*父节点;
p->data=项目;
p->left=NULL;
p->right=NULL;
p->parent=NULL;
parent=NULL;
if(isempty())
根=p;
其他的
{
节点*ptr;
ptr=根;
while(ptr!=NULL)
{
父母=ptr;
如果(项目>ptr->数据)
ptr=ptr->右侧;
其他的
ptr=ptr->左;
}   
如果(项<父项->数据)
{
父->左=p;
}
其他的
父->右=p;
p->parent=parent;
}
}
void bst::inordertrav()
{
顺序(根);
}
void bst::索引(节点*ptr)
{
如果(ptr!=NULL)
{
顺序(ptr->左);
coutright)//currNode没有正确的子节点
{
如果(currNode->parent->left&&currNode->data==currNode->parent->left->data)//currNode是左边的子节点
{
currNode=currNode->parent;
返回节点->数据;
}
如果(currNode->parent->right&&currNode->data==currNode->parent->right->data)//currNode是父节点的正确子节点
{
//如果currNode的父节点(即右子节点)也是右子节点
//那么这个currNode实际上是一个叶节点,它不应该返回任何内容。
如果(当前节点->父节点->父节点->右&
currNode->parent->parent->right->data==currNode->parent->data)
{
cout parent->parent;
返回节点->数据;
}
}
}
int main()
{
bst b;
b、 插入(52);
b、 插入(25);
b、 插入(50);
b、 插入(40);
b、 插入(45);
b、 插入(20);
b、 插入(75);
b、 插入(65);
b、 插入(78);
b、 插入(23);
b、 插入(15);

非常感谢…解决了我的问题..现在在按顺序执行时似乎出现了逻辑错误…现在我将研究它..谢谢!!:)我将返回节点的int部分而不是节点本身..这就是我选择使用Integer的原因..迭代器通常通过给定类的集合进行;您过去定义了节点,但现在您已经不,你的定义显示了
public Node next()
#include<iostream>
using namespace std;
struct node
{
    node *left;
    node *right;
    node *parent;
    int data;
} ;

class bst
{
public:
    node *root;
    node *currNode;

    bst()
    {
        root=NULL;
        currNode = NULL;
    }
    int isempty() 
    {
        return(root==NULL);
    }
    void insert(int item);
    void inordertrav();
    void inorder(node *);
    int next();
    node *nextNode(node *root);
    void bft();
};
void bst::insert(int item)
{
    node *p=new node;
    node *parent;
    p->data=item;
    p->left=NULL;
    p->right=NULL;
    p->parent = NULL;
    parent=NULL;
    if(isempty())
        root=p;
    else
    {
        node *ptr;
        ptr = root;
        while(ptr!=NULL)
        {
            parent = ptr;
            if(item > ptr->data)        
                ptr = ptr->right;
            else
                ptr=ptr->left;
        }   
        if(item < parent->data)
        {
            parent->left = p;
        }
        else
            parent->right = p;
        p->parent = parent;
    }
}
void bst::inordertrav()
{
    inorder(root);
}
void bst::inorder(node *ptr)
{
    if(ptr!=NULL)
    {
        inorder(ptr->left);
        cout<<"  "<<ptr->data<<"     ";
        inorder(ptr->right);
    }
}

int bst::next()
{
// If currNode is NULL and find the left most node using a while loop.
    if (currNode == NULL)
    {
        cout << "First node data" << endl;
        node *tmp = root;
        while (tmp->left != NULL)
            tmp = tmp->left;

        currNode = tmp;
        return currNode->data;
    }
    cout << endl << "Current Node is - " << currNode->data << endl;
    if (currNode->right)
    {
        node *tmp = currNode->right;
        while (tmp->left) // find the leftmost node for this right subtree in recursive way without recursion
            tmp = tmp->left;
        currNode = tmp;
        return currNode->data;
    }

    if (! currNode->right) // currNode does not have right child
    {
        if ( currNode->parent->left && currNode->data == currNode->parent->left->data) // CurrNode is the left child
        {
            currNode = currNode->parent;
            return currNode->data;
        }
        if (currNode->parent->right && currNode->data == currNode->parent->right->data) //CurrNode is the right child of the parent
        {
            //If the parent of the currNode (which is right child) is also a right child
            //then this currNode is actually a leaf node and it nothing should be returned.
            if (currNode->parent->parent->right && 
                    currNode->parent->parent->right->data == currNode->parent->data)
            {
                cout << "The tree has been travered fully";
                return -1;
            }
            currNode = currNode->parent->parent;
            return currNode->data;
        }
    }
}


int main()
{
    bst b;
    b.insert(52);
    b.insert(25);
    b.insert(50);
    b.insert(40);
    b.insert(45);
    b.insert(20);
    b.insert(75);
    b.insert(65);
    b.insert(78);
    b.insert(23);
    b.insert(15);

    cout << "---- In order traversal using iterator -----" << endl;
    int i;
    do
    {
        i = b.next();   
        cout << " " << i << " ";
    }while (i != -1);
    cout << endl;
    cout<<endl;
}