Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void add(T element) {
- if(size == 0) {
- root = new Node<T>(element); //if the tree is empty, element = root
- size++;
- return;
- }
- if(contains(element)) {
- return;
- }
- Node<T> currentNode = root;
- int temp = currentNode.data.compareTo(element);
- while(currentNode != null) {
- if(temp == -1) { //currentNode < element
- if(currentNode.right == null) {
- currentNode.right = new Node<T>(element); //this is where the element is put in the tree (if the tree was not empty)
- size++;
- break; //går ut ur while-loopen
- }
- currentNode = currentNode.right;
- }
- if(temp == 1) { //currentNode > element
- if(currentNode.left == null) {
- currentNode.left = new Node<T>(element); //this is where the element is put in the tree (if the tree was not empty)
- size++;
- break;
- }
- currentNode = currentNode.left;
- }
- temp = currentNode.data.compareTo(element);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement