Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bianry Tree Code Used in Java public class BinaryTree { private T data; private BinaryTree parent; private BinaryTree left; private BinaryTree right; public BinaryTree() {

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Bianry Tree Code Used in Java

public class BinaryTree

{

private T data;

private BinaryTree parent;

private BinaryTree left;

private BinaryTree right;

public BinaryTree()

{

parent = left = right = null;

data = null;

}

public void makeRoot(T data)

{

if (!isEmpty())

{

System.out.println("Can't make root. Already exists");

}

else

this.data = data;

}

public void setData(T data)

{

this.data = data;

}

public void setLeft(BinaryTree tree)

{

left = tree;

}

public void setRight(BinaryTree tree)

{

right = tree;

}

public void setParent(BinaryTree tree)

{

parent = tree;

}

public T getData()

{

return data;

}

public BinaryTree getParent()

{

return parent;

}

public BinaryTree getLeft()

{

return left;

}

public BinaryTree getRight()

{

return right;

}

public void attachLeft(BinaryTree tree)

{

if (tree==null) return;

else if (left!=null || tree.getParent()!=null)

{

System.out.println("Can't attach");

return;

}

else

{

tree.setParent(this);

this.setLeft(tree);

}

}

public void attachRight(BinaryTree tree)

{

if (tree==null) return;

else if (right!=null || tree.getParent()!=null)

{

System.out.println("Can't attach");

return;

}

else

{

tree.setParent(this);

this.setRight(tree);

}

}

public BinaryTree detachLeft()

{

if (this.isEmpty()) return null;

BinaryTree retLeft = left;

left = null;

if (retLeft!=null) retLeft.setParent(null);

return retLeft;

}

public BinaryTree detachRight()

{

if (this.isEmpty()) return null;

BinaryTree retRight = right;

right =null;

if (retRight!=null) retRight.setParent(null);

return retRight;

}

public boolean isEmpty()

{

if (data == null)

return true;

else

return false;

}

public void clear()

{

left = right = parent =null;

data = null;

}

public BinaryTree root()

{

if (parent == null)

return this;

else

{

BinaryTree next = parent;

while (next.getParent()!=null)

next = next.getParent();

return next;

}

}

public static void preorder(BinaryTree t)

{

if (t!=null)

{

System.out.print(t.getData()+"\t");

preorder(t.getLeft());

preorder(t.getRight());

}

}

public static void inorder(BinaryTree t)

{

if (t!=null)

{

inorder(t.getLeft());

System.out.print(t.getData() + "\t");

inorder(t.getRight());

}

}

public static void postorder(BinaryTree t)

{

if (t!=null)

{

postorder(t.getLeft());

postorder(t.getRight());

System.out.print(t.getData() + "\t");

}

}

}

This is java program and need to put some new methods in the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago