Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. public class Binärbaum<T extends Comparable<T>> {
  2.  
  3. Knoten<T> root;
  4. Knoten<T> wurzelGerade;
  5.  
  6. public void add(T content) {
  7.  
  8. if (root == null) {
  9. root = new Knoten<T>(content);
  10. wurzelGerade = root;
  11. }
  12.  
  13. // Linker Teilbaum
  14. else if (content.compareTo(wurzelGerade.content) < 0) {
  15. if (wurzelGerade.links == null) {
  16. wurzelGerade.links = new Knoten<T>(content);
  17. wurzelGerade = root;
  18. } else {
  19. wurzelGerade = wurzelGerade.links;
  20. add(content);
  21. }
  22. }
  23. // Rechter Teilbaum
  24. else if (content.compareTo(wurzelGerade.content) > 0) {
  25. if (wurzelGerade.rechts == null) {
  26. wurzelGerade.rechts = new Knoten<T>(content);
  27. wurzelGerade = root;
  28. } else {
  29. wurzelGerade = wurzelGerade.rechts;
  30. add(content);
  31. }
  32. }
  33. }
  34.  
  35. public void ausgeben() {
  36. int ebene = 0;
  37. ausgeben(root, ebene, " Wurzel", 0);
  38. }
  39.  
  40. public void ausgeben(Knoten<T> temp, int ebene, String linksRechts, int seite) {
  41. ebene++;
  42. if(seite == 1) linksRechts = linksRechts + " Links ";
  43. if(seite == 2) linksRechts = linksRechts + " Rechts ";
  44.  
  45. if (temp.links != null) {
  46. ausgeben(temp.links, ebene, linksRechts, 1);
  47. }
  48. if (temp.rechts != null) {
  49. ausgeben(temp.rechts, ebene, linksRechts, 2);
  50. }
  51. System.out.println("Ebene " +(ebene-1) +": " +temp + linksRechts);
  52. }
  53.  
  54. public static class Knoten<E> {
  55.  
  56. Knoten<E> links;
  57. Knoten<E> rechts;
  58. E content;
  59.  
  60. public Knoten(E content) {
  61. this.content = content;
  62. }
  63.  
  64. public String toString() {
  65. return String.valueOf(content);
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement