Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AnimalNode<E>
- {
- private E data;
- private AnimalNode<E> left, right;
- public AnimalNode(E initialData, AnimalNode<E> initialLeft, AnimalNode<E> initialRight)
- {
- data = initialData;
- left = initialLeft;
- right = initialRight;
- }
- public E getData()
- {
- return data;
- }
- //left of node
- public AnimalNode<E> getLeft()
- {
- return left;
- }
- //return farthest left of node
- public E getLeftmostData()
- {
- if (left == null)
- return data;
- else
- return left.getLeftmostData();
- }
- //return right of node
- public AnimalNode<E> getRight()
- {
- return right;
- }
- //return farthest right of node
- public E getRightmostData()
- {
- if (left == null)
- return data;
- else
- return left.getRightmostData();
- }
- //print data below node
- public void inorderPrint()
- {
- if (left != null)
- left.inorderPrint();
- System.out.println(data);
- if (right != null)
- right.inorderPrint();
- }
- //find if node is a leaf
- public boolean isLeaf( )
- {
- return (left == null) && (right == null);
- }
- //print data below or at node
- public void preorderPrint( )
- {
- System.out.println(data);
- if (left != null)
- left.preorderPrint( );
- if (right != null)
- right.preorderPrint( );
- }
- public void postorderPrint( )
- {
- if (left != null)
- left.postorderPrint( );
- if (right != null)
- right.postorderPrint( );
- System.out.println(data);
- }
- //0 as RootOfTree and 1 for children, etc.
- public void print(int depth)
- {
- int i;
- // Print the indentation and the data from the current node:
- for (i = 1; i <= depth; i++)
- System.out.print(" ");
- System.out.println(data);
- // Print the left subtree (or a dash if there is a right child and no left child)
- if (left != null)
- left.print(depth+1);
- else if (right != null)
- {
- for (i = 1; i <= depth+1; i++)
- System.out.print(" ");
- System.out.println("--");
- }
- // Print the right subtree (or a dash if there is a left child and no left child)
- if (right != null)
- right.print(depth+1);
- else if (left != null)
- {
- for (i = 1; i <= depth+1; i++)
- System.out.print(" ");
- System.out.println("--");
- }
- }
- //remove farthest node on left
- public AnimalNode<E> removeLeftmost( )
- {
- if (left == null)
- return right;
- else
- {
- left = left.removeLeftmost( );
- return this;
- }
- }
- //remove farthest right node
- public AnimalNode<E> removeRightmost( )
- {
- if (right == null)
- return left;
- else
- {
- right = right.removeRightmost( );
- return this;
- }
- }
- public void setData(E newData)
- {
- data = newData;
- }
- //new left child
- public void setLeft(AnimalNode<E> newLeft)
- {
- left = newLeft;
- }
- //new right child
- public void setRight(AnimalNode<E> newRight)
- {
- right = newRight;
- }
- //copy all of tree
- public static <E> AnimalNode<E> CopyTree(AnimalNode<E> source)
- {
- AnimalNode<E> leftCopy, rightCopy;
- if (source == null)
- return null;
- else
- {
- leftCopy = CopyTree(source.left);
- rightCopy = CopyTree(source.right);
- return new AnimalNode<E>(source.data, leftCopy, rightCopy);
- }
- }
- //keep a count of the nodes in the tree
- public static <E> long treeSize(AnimalNode<E> RootOfTree)
- {
- if (RootOfTree == null)
- return 0;
- else
- return 1 + treeSize(RootOfTree.left) + treeSize(RootOfTree.right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement