The document describes a binary search tree (BSTree) data structure implemented in Java. It includes a BSTree class that constructs the root node and provides methods for insertion, retrieval, removal and traversal. It also includes a BSTNode class that represents individual nodes, storing their key, element, parent and child references. Methods are provided to access and update these node properties, check types of nodes, and perform tree operations like searching and traversal.
The document describes a binary search tree (BSTree) data structure implemented in Java. It includes a BSTree class that constructs the root node and provides methods for insertion, retrieval, removal and traversal. It also includes a BSTNode class that represents individual nodes, storing their key, element, parent and child references. Methods are provided to access and update these node properties, check types of nodes, and perform tree operations like searching and traversal.
The document describes a binary search tree (BSTree) data structure implemented in Java. It includes a BSTree class that constructs the root node and provides methods for insertion, retrieval, removal and traversal. It also includes a BSTNode class that represents individual nodes, storing their key, element, parent and child references. Methods are provided to access and update these node properties, check types of nodes, and perform tree operations like searching and traversal.
public BSTree(BSTNode<K, E> node) { root = node; }
@Override public BSTNode<K, E> getRoot() { return root; }
@Override public void insert(K key, E elem) { BSTNode<K, E> newNode = new BSTNode<K, E>(key, elem); if (root == null) { root = newNode; return; } BSTNode.insert(newNode, root); }
@Override public E getElementByKey(K key) { return BSTNode.getElementByKey(root, key); }
@Override public void remove(K key) { if (root == null) { System.out.println("Cannot remove: The tree is empty"); return; } root = BSTNode.remove(key, root); }
@Override public IList<E> getPreorder() { IList<E> list = new SList<E>(); BSTNode.getPreorder(root, list); return list; }
@Override public boolean containsKey(K key) { return BSTNode.containsKey(root, key); }
@Override public boolean containsElement(E element) { return BSTNode.containsElement(root, element); }
public class BSTNode<K extends Comparable<K>, E> implements IBSTNode<K, E>{ K key; E element;
@Override public E getElement() { return element; }
@Override public BSTNode<K, E> getParent() { return parent; }
@Override public BSTNode<K, E> getLeftChild() { return leftChild; }
@Override public BSTNode<K, E> getRightChild() { return rightChild; }
@Override public boolean hasLeftChild() { if (leftChild != null) { return true; } return false; }
@Override public boolean hasRightChild() { if (rightChild != null) { return true; } return false; }
@Override public boolean isInternal() { if (leftChild == null && rightChild == null) { return false; } return true; }
@Override public boolean isExternal() { if (leftChild == null && rightChild == null) { return true; } return false; }
@Override public IBSTNode<K, E> getRoot() { if (parent == null) { return this; } else { return parent.getRoot(); } }
@Override public boolean isRoot() { if (parent == null) { return true; } else { return false; } }
@Override public int getSize() { return getSize(this); }
int getSize(BSTNode<K, E> node) { if (node==null) return 0; else return 1 + getSize(node.leftChild) + getSize(node.rightChild); }
@Override public int getHeight() { return getHeight(this); }
static<K extends Comparable<K>,E> int getHeight(BSTNode<K, E> node) { if (node==null) return -1; else return 1 + Math.max(getHeight(node.leftChild) , getHeight(node.rightChild)); }
@Override public int getDepth() { return getDepth(this); }
static<K extends Comparable<K>,E> int getDepth(BSTNode<K, E> node) { if (node==null) return -1; else return 1+getDepth(node.parent); }
@Override public IList<IBSTNode<K, E>> getChildren() { IList<IBSTNode<K, E>> children = new SList<IBSTNode<K, E>>(); if (hasLeftChild()) children.addLast(leftChild); if (hasRightChild()) children.addLast(rightChild); return children; }
@Override public IList<E> getPreorder() { IList<E> list = new SList<E>(); getPreorder(this,list); return list; }