Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BinaryTreeActual { //Integer Binary Tree
  8.   class Program {
  9.     static void Main(string[] args) {
  10.       Tree tree = new Tree(new IntNode(8, null));
  11.       tree.InsertInt(7);
  12.       tree.InsertInt(9);
  13.       tree.InsertInt(10);
  14.       Console.WriteLine(tree.getDepth(10));
  15.       Console.WriteLine(tree.root.childRight.childRight.value);
  16.       Console.ReadLine();
  17.  
  18.       foreach (string meme in tree.printFuckingEverything()) {
  19.         Console.WriteLine(meme);
  20.       }
  21.  
  22.       Console.ReadLine();
  23.     }
  24.   }
  25.  
  26.   public class Utility {
  27.     public Tree insert(Tree tree, IntNode nodeToAdd, IntNode currentNode = null) {
  28.       if (currentNode == null) {
  29.         currentNode = tree.root;
  30.       }
  31.  
  32.       if (currentNode.value >= nodeToAdd.value) {
  33.         if (currentNode.childLeft == null) {
  34.           currentNode.childLeft = nodeToAdd;
  35.           return tree;
  36.         }
  37.         else {
  38.           return insert(tree, nodeToAdd, currentNode.childLeft);
  39.         }
  40.       }
  41.       else {  //(currentNode.value < nodeToAdd.value)
  42.         if (currentNode.childRight == null) {
  43.           currentNode.childRight = nodeToAdd;
  44.           return tree;
  45.         }
  46.         else {
  47.           return insert(tree, nodeToAdd, currentNode.childRight);
  48.         }
  49.       }
  50.     }
  51.   }
  52.  
  53.   public class Tree {
  54.     public IntNode root;
  55.     public Tree(IntNode root) {
  56.       this.root = root;
  57.     }
  58.  
  59.     public void InsertRawNode(IntNode nodeToAdd, IntNode currentNode = null) {
  60.       if (currentNode == null) {
  61.         currentNode = root;
  62.       }
  63.  
  64.       if (currentNode.value >= nodeToAdd.value) {
  65.         if (currentNode.childLeft == null) {
  66.           currentNode.childLeft = nodeToAdd;
  67.         }
  68.         else {
  69.           InsertRawNode(nodeToAdd, currentNode.childLeft);
  70.         }
  71.       }
  72.       else {  //(currentNode.value < nodeToAdd.value)
  73.         if (currentNode.childRight == null) {
  74.           currentNode.childRight = nodeToAdd;
  75.         }
  76.         else {
  77.          InsertRawNode(nodeToAdd, currentNode.childRight);
  78.         }
  79.       }
  80.     }
  81.  
  82.     public void InsertInt(int number, IntNode currentNode = null) {
  83.       if (currentNode == null) {
  84.         currentNode = root;
  85.       }
  86.  
  87.       if (currentNode.value > number) {
  88.         if (currentNode.childLeft == null) {
  89.           currentNode.childLeft = new IntNode(number, currentNode);
  90.         }
  91.         else {
  92.           InsertInt(number, currentNode.childLeft);
  93.         }
  94.       }
  95.       else if (currentNode.value != number){  //(currentNode.value < nodeToAdd.value)
  96.         if (currentNode.childRight == null) {
  97.           currentNode.childRight = new IntNode(number, currentNode);
  98.         }
  99.         else {
  100.           InsertInt(number, currentNode.childRight);
  101.         }
  102.       }else {
  103.         Console.WriteLine("NO DUPLICATES YOU KNOB");
  104.       }
  105.     }
  106.  
  107.     public int getDepth(int number, IntNode currentNode = null, int depth = 0) {
  108.       if (currentNode == null) {
  109.         currentNode = root;
  110.       }
  111.  
  112.       if (currentNode == null) {
  113.         return -1;
  114.       }
  115.       else if (currentNode.value > number) {
  116.         return getDepth(number, currentNode.childLeft, depth + 1);
  117.       }
  118.       else if (currentNode.value < number) {
  119.         return getDepth(number, currentNode.childRight, depth + 1);
  120.       }
  121.       else {  // (currentNode.value == number)
  122.         return depth;
  123.       }
  124.     }
  125.  
  126.     public List<string> printFuckingEverything(List<string> result = null, IntNode currentNode = null) {
  127.       if (currentNode == null) {
  128.         currentNode = root;
  129.       }
  130.       if (result == null) {
  131.         result = new List<string>();
  132.       }
  133.       string toCheck = getDepth(currentNode.value).ToString() + ".";
  134.       //I KNOW THIS IS UGLY AND SLOW BUT IndexOf doesnt work and neither does Contains for some bloody reason
  135.       if (result.Count == 0) {
  136.         result.Add(toCheck + " " + currentNode.value.ToString());
  137.       }
  138.       else if (result.Count > 0) {
  139.         bool addValue = false;
  140.         for (int i = 0; i <= result.Count - 1; i++) {
  141.           addValue = false;
  142.           if (result[i].Contains(toCheck)) {
  143.             string direction;
  144.             if (currentNode == currentNode.parent.childRight) {
  145.               direction = "R";
  146.             }
  147.             else {
  148.               direction = "L";
  149.             }
  150.             result[i] += " " + currentNode.value.ToString() + direction + currentNode.parent.value.ToString();
  151.           }else {
  152.             addValue = true;
  153.           }
  154.         }
  155.  
  156.         if (addValue) {
  157.           string direction;
  158.           if (currentNode == currentNode.parent.childRight) {
  159.             direction = "R";
  160.           }
  161.           else {
  162.             direction = "L";
  163.           }
  164.           result.Add(toCheck + " " + currentNode.value.ToString() + direction + currentNode.parent.value.ToString());
  165.         }
  166.       }
  167.  
  168.       if (currentNode.childLeft != null) {
  169.         printFuckingEverything(result, currentNode.childLeft);
  170.       }
  171.       if (currentNode.childRight != null) {
  172.         printFuckingEverything(result, currentNode.childRight);
  173.       }
  174.       return result;
  175.      
  176.     }
  177.  
  178.   }
  179.  
  180.   public class IntNode {
  181.     public int value;
  182.     public IntNode childLeft, childRight, parent;
  183.     public IntNode(int value, IntNode parent) {
  184.       this.value = value;
  185.       this.parent = parent;
  186.       childLeft = null;
  187.       childRight = null;
  188.     }
  189.     public void setLeftC(IntNode node) {
  190.       childLeft = node;
  191.     }
  192.     public void setRightC(IntNode node) {
  193.       childRight = node;
  194.     }
  195.   }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement