I dont know how to finish public class InorderIterator implements Iterator publi
ID: 3754886 • Letter: I
Question
I dont know how to finish public class InorderIterator implements Iterator
public class BinaryTree {
//Implements a Binary Tree of Strings
private class Node {
private Node left;
private String data;
private Node right;
private Node parent; // reference to the parent node
// the parent is null for the root node
private Node(Node L, String d, Node r, Node p) {
left = L;
data = d;
right = r;
parent = p;
}
}
private Node root;
public BinaryTree() {
// create an empty tree
root = null;
}
public BinaryTree(String d) {
// create a tree with a single node
root = new Node(null, d, null, root);
}
public class InorderIterator implements Iterator {
// An iterator that returns data in the tree in an in order pattern
// the implementation must use the parent pointer and must not use an
// additional data structure
public InorderIterator() {
}
public boolean hasNext() {
}
public String next() {
}
public void remove() {
// optional method not implemented
throw new UnsupportedOperationException();
}
}
public Iterator inorder() {
// return a new in order iterator object
}
Explanation / Answer
Here is the simple program for inorder traversel please go through it and if any queries or any modifications needed regarding the program please comment below. Thank you.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
node* L;
node* R;
};
struct node* root;
struct node* insert(struct node* r, int data);
void inOrder(struct node* r);
int main()
{
root = NULL;
int x, val;
printf("How many data's do you want to insert ?x");
scanf("%d", &x);
for(int i=0; i<x; i++){
printf("Data %d: ", i+1);
scanf("%d", &val);
root = insert(root, val);
}
printf("Inorder Traversal: ");
inOrder(root);
printf("x");
return 0;
}
struct node* insert(struct node* r, int data)
{
if(r==NULL)
{
r = (struct node*) malloc(sizeof(struct node));
r->data = data;
r->L = NULL;
r->R = NULL;
}
else if(data < r->data){
r->L = insert(r->L, data);
}
else {
r->R = insert(r->R, data);
}
return r;
}
void inOrder(struct node* r)
{
if(r!=NULL){
inOrder(r->L);
printf("%d ", r->data);
inOrder(r->R);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.