Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package adtpp;
- /**
- *
- * @author Denis
- */
- public class BinaryTree {
- private BinaryNode root;
- private int positions = 0;
- public BinaryTree()
- {
- }
- public BinaryTree(Person p)
- {
- root = new BinaryNode(p);
- positions = 3;
- }
- public void add(Person p)
- {
- if(root == null)
- {
- root = new BinaryNode(p);
- positions = 3;
- return;
- }
- BinaryNode temp = root;
- while(true)
- {
- if(temp.getData().getAge() > p.getAge())
- {
- if(temp.getLeft() == null)
- {
- temp.setLeft(new BinaryNode(p));
- positions = positions + 2;
- return;
- }
- temp = temp.getLeft();
- }
- else
- {
- if(temp.getRight() == null)
- {
- temp.setRight(new BinaryNode(p));
- positions = positions + 2;
- return;
- }
- temp = temp.getRight();
- }
- }
- }
- public String toString()
- {
- if(root != null)
- {
- return toString(root.getLeft()) + " " + root.getData() + " " + toString(root.getRight());
- }
- return "empty";
- }
- private String toString(BinaryNode btnode)
- {
- if(btnode != null)
- {
- return toString(btnode.getLeft()) + " " + btnode.getData() + " " + toString(btnode.getRight());
- }
- return "";
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package adtpp;
- /**
- *
- * @author Denis
- */
- public class DList {
- private Node head;
- public DList(Person p)
- {
- head = new Node(p);
- }
- public void add(Person p)
- {
- if(head == null)
- {
- head = new Node(p);
- return;
- }
- if(head.getData().getAge() >= p.getAge())
- {
- head = new Node(p, head);
- return;
- }
- Node temp = head;
- if(temp.getNext() != null && temp.getNext().getData().getAge() < p.getAge())
- {
- temp = temp.getNext();
- }
- Node n = new Node(p, temp.getNext());
- temp.setNext(n);
- }
- public Person remove(String name, int age)
- {
- Person returnedPerson = null;
- if(head == null)
- {
- return null;
- }
- if(head.getData().getName().equals(name) && head.getData().getAge() == age)
- {
- returnedPerson = head.getData();
- head = head.getNext();
- return returnedPerson;
- }
- Node temp = head;
- while(temp.getNext() != null)
- {
- if(temp.getNext().getData().getName().equals(name) && temp.getNext().getData().getAge() == age)
- {
- returnedPerson = temp.getNext().getData();
- temp.setNext(temp.getNext().getNext());
- return returnedPerson;
- }
- temp = temp.getNext();
- }
- return returnedPerson;
- }
- public String toString()
- {
- String s = "";
- Node temp = head;
- while(temp != null)
- {
- s = s + temp.getData() + " ";
- temp = temp.getNext();
- }
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement