Пример #1
0
 public void Insert(int data)
 {
     if (root == null)
     {
         root = new TreeNode(data);
         root.ParentID = null;
         root.NodeID = AddInDB(root);
         return;
     }
     TreeNode parent = FindParent(data);
     if (data < parent.value)
     {
         parent.left = new TreeNode(data);
         parent.left.ParentID = parent.NodeID;
         parent.left.NodeID = AddInDB(parent.left);
         return;
     }
     parent.right = new TreeNode(data);
     parent.right.ParentID = parent.NodeID;
     parent.right.NodeID = AddInDB(parent.right);
     return;
 }
Пример #2
0
 public TreeNode(int i)
 {
     value = i;
     left = null;
     right = null;
 }
Пример #3
0
 public BinaryTree()
 {
     root = null;
 }
Пример #4
0
 private TreeNode RemoveRec(TreeNode root, TreeNode nodeToDelete, bool removedFromDB = false)
 {
     if (root == null)
     {
         return root;
     }
     if (nodeToDelete.value < root.value)
     {
         root.left = RemoveRec(root.left, nodeToDelete, removedFromDB);
     }
     if (nodeToDelete.value > root.value)
     {
         root.right = RemoveRec(root.right, nodeToDelete, removedFromDB);
     }
     if (nodeToDelete.value == root.value)
     {
         if (root.left == null && root.right == null)
         {
             if (!removedFromDB)
             {
                 RemoveFromDB(root, true);
             }
             root = null;
             return root;                    
         }
         else if (root.left == null)
         {
             if (!removedFromDB)
             {
                 RemoveFromDB(root, true);
             }
             root.right.ParentID = root.ParentID;
             root = root.right;
         }
         else if (root.right == null)
         {
             if (!removedFromDB)
             {
                 RemoveFromDB(root, false);
             }
             root.left.ParentID = root.ParentID;
             root = root.left;
         }
         else
         {
             TreeNode mostLeft = root.right;
             while (mostLeft.left != null)
             {
                 mostLeft = mostLeft.left;
             }
             if (!removedFromDB)
             {
                 RemoveFromDB(root, true, mostLeft);
             }
             root.value = mostLeft.value;
             root.NodeID = mostLeft.NodeID;
             root.right = RemoveRec(root.right, mostLeft, true);
             if(root.right != null)
             {
                 root.right.ParentID = root.NodeID;
             }                
             if (root.left != null)
             {
                 root.left.ParentID = root.NodeID;
             }  
         }
     }
     return root;
 }
Пример #5
0
 public void Remove(int data)
 {
     TreeNode nodeToDelete = new TreeNode(data);
     root = RemoveRec(root, nodeToDelete);
 }
Пример #6
0
        private void UpdateIDs(TreeNodeContext db, bool needRightChild, TreeNode nodeToRemove, TreeNode mostLeft)
        {
            var firstChildOfRemovingNode = needRightChild ? db.TreeNodes.SingleOrDefault(node => node.ParentID == nodeToRemove.NodeID && node.value >= nodeToRemove.value) 
                                                          : db.TreeNodes.SingleOrDefault(node => node.ParentID == nodeToRemove.NodeID && node.value < nodeToRemove.value);

            var leftChild = needRightChild ? db.TreeNodes.SingleOrDefault(node => node.ParentID == nodeToRemove.NodeID && node.value < nodeToRemove.value) : null;

            if (firstChildOfRemovingNode != null)
            {
                if(mostLeft != null)
                {
                    var mostLeftInDB = db.TreeNodes.SingleOrDefault(node => node.NodeID == mostLeft.NodeID);
                    mostLeftInDB.ParentID = nodeToRemove.ParentID;
                    if (leftChild != null)
                    {
                        leftChild.ParentID = mostLeft.NodeID;
                    }
                    if(mostLeftInDB.NodeID == firstChildOfRemovingNode.NodeID)
                    {
                        db.SaveChanges();
                        return;
                    } 
                    firstChildOfRemovingNode.ParentID = mostLeftInDB.NodeID;
                    db.SaveChanges();
                    return;
                }
                firstChildOfRemovingNode.ParentID = nodeToRemove.ParentID;
                db.SaveChanges();
            }
        }
Пример #7
0
 private void RemoveFromDB(TreeNode node, bool needRightChild, TreeNode mostLeft = null)
 {
     using (TreeNodeContext db = new TreeNodeContext())
     {
         UpdateIDs(db, needRightChild, node, mostLeft);
         var nodeToRemove = db.TreeNodes.SingleOrDefault(n => n.NodeID == node.NodeID);
         if (nodeToRemove != null)
         {
             db.TreeNodes.Remove(nodeToRemove);
         }
         db.SaveChanges();
     }
 }
Пример #8
0
 private int AddInDB(TreeNode node)
 {
     using (TreeNodeContext db = new TreeNodeContext())
     {
         db.TreeNodes.Add(node);
         db.SaveChanges();
         return node.NodeID;
     }
 }