In data structure, I want to make code about printing this java tree codes in preorder Traversal and postorder Traversal. My Node code and Tree Code is here import java.util.ArrayList;
In data structure, I want to make code about printing this java tree codes in preorder Traversal and postorder Traversal. My Node code and Tree Code is here import java.util.ArrayList;
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
100%
In data structure, I want to make code about printing this java tree codes in preorder Traversal and postorder Traversal.
My Node code and Tree Code is here
import java.util.ArrayList;
public class MyNode {
private Object element;
private MyNode parent;
private ArrayList children;
public MyNode() {
this.element = null;
this.parent = null;
this.children = null;
}
public MyNode(Object e) {
this.element = e;
this.parent = null;
this.children = new ArrayList();
this.children.add(null);
this.children.add(null);
}
public Object element() {
return this.element;
}
public MyNode parent() {
return this.parent;
}
public ArrayList children() {
return this.children;
}
public int degree() {
return this.children.size();
}
public void setElement(Object e) {
this.element = e;
}
public void setParent(MyNode p) {
this.parent = p;
}
public void setChildren(ArrayList c) {
this.children = c;
}
}
import java.util.ArrayList;
public class MyTree {
private MyNode root;
private int totalSize;
public MyTree() {
this.root = null;
this.totalSize = 0;
}
public MyTree(Object e) {
this.root = new MyBinNode(e);
this.totalSize = 1;
}
public int size() {
return this.totalSize;
}
public MyNode root() {
return this.root;
}
public ArrayList children(MyNode v) {
return v.children();
}
public boolean isExternal(MyNode v) {
return v.children().isEmpty();
}
public MyNode addRoot(Object e) {
MyNode temp = this.root;
this.root = new MyBinNode(e);
this.totalSize = 1;
return temp;
}
public MyNode addNode(Object e) {
MyNode newNode = new MyBinNode(e);
newNode.setParent(this.root);
this.root.children().add(newNode);
this.totalSize++;
return newNode;
}
public MyNode addChild(MyNode v, Object e) {
MyNode newNode = new MyBinNode(e);
newNode.setParent(v);
v.children().add(newNode);
this.totalSize++;
return newNode;
}
public MyNode addChild(MyNode v, int i, Object e) {
MyNode newNode = new MyBinNode(e);
newNode.setParent(v);
v.children().add(i, newNode);
this.totalSize++;
return newNode;
}
public MyNode setChild(MyNode v, int i, Object e) {
MyNode newNode = new MyBinNode(e);
newNode.setParent(v);
v.children().set(i, newNode);
return newNode;
}
public MyNode removeChild(MyNode v, int i) {
this.totalSize--;
return (MyNode)v.children().remove(i);
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY