Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- public class Drzewo {
- public Elementy _root;
- public Drzewo() {
- this._root = null;
- }
- public void queueBFS(Elementy v) {
- Queue<Elementy> Q = new LinkedList<>();
- Q.add(v);
- while (!Q.isEmpty()) {
- v = Q.peek();
- Q.poll();
- System.out.println(v.data +" "+v.lista);
- if (v.left != null) Q.add(v.left);
- if (v.right != null) Q.add(v.right);
- }
- System.out.print("\n");
- }
- //dodawanie do drzewa
- public void insertBST(Elementy w) {
- Elementy parent = null;
- Elementy node = _root;
- while (node != null) {
- parent = node;
- node = w.compareTo(parent) <= 0 ? node.left : node.right;
- }
- w.up = parent;
- if (parent == null) {
- _root = w;
- }
- else if (w.compareTo(parent) < 0) {
- parent.left = w;
- }
- else {
- parent.right = w;
- }
- return;
- }
- public void inOrder(Elementy v) {
- if (v != null) {
- inOrder(v.left);
- System.out.println(v.data + " ");
- inOrder(v.right);
- }
- }
- public void wyswietl(Elementy v) {
- if (v != null) {
- wyswietl(v.left);
- // System.out.print(v.data + " ");
- for (Integer i: v.lista)
- // System.out.print(i + ", ");
- // System.out.print("\n");
- wyswietl(v.right);
- }
- }
- public Elementy get_root() {
- return _root;
- }
- public void set_root(Elementy _root) {
- this._root = _root;
- }
- public int maxDepth(Elementy node) {
- if (node == null) {
- return 0;
- }
- return Math.max(maxDepth(node.left),maxDepth(node.right))+1;
- }
- public int minDepth(Elementy node) {
- if (node == null) {
- return 0;
- }
- return Math.min(maxDepth(node.left),maxDepth(node.right))+1;
- }
- public boolean isBinaryTreeBalanced(Elementy root) {
- if (root == null) {
- throw new IllegalArgumentException(
- "Musi istniec korzen drzewa");
- }
- return this.maxDepth(root) - this.minDepth(root) <= 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement