private BinarySearchTreeNode Successor(BinarySearchTreeNode NodeDelete) { BinarySearchTreeNode successorParent = NodeDelete; BinarySearchTreeNode successor = NodeDelete; BinarySearchTreeNode current = NodeDelete.right; while (current != null) { successorParent = successor; successor = current; current = current.left; } if (successor != NodeDelete.right) { successorParent.left = successor.right; successor.right = NodeDelete.right; } return(successor); }
private BinarySearchTreeNode SearchInt(BinarySearchTreeNode node, string key) { if (node == null) { return(null); } else if ((string)node.data == key) { return(node); } /*else if ((string)node.data > key) * { * return (SearchInt(node.left, key)); * }*/ else { return(SearchInt(node.right, key)); } }
public void Insert(Product product) { // New node parent BinarySearchTreeNode tempParent = new BinarySearchTreeNode(); // Start from the root and go BinarySearchTreeNode tempSearch = root; while (tempSearch != null) { tempParent = tempSearch; // Value already, exist if (product == tempSearch.data) { return; } else if (tempSearch.left != null) { tempSearch = tempSearch.left; } else { tempSearch = tempSearch.left; } } BinarySearchTreeNode goingToInsert = new BinarySearchTreeNode(product); // Tree empty, add node if (root == null) { root = goingToInsert; } else if (tempParent.left == null) { tempParent.left = goingToInsert; } else { tempParent.right = goingToInsert; } }
public void Insert(Product product) { //Yeni eklenecek düğümün parent'ı BinarySearchTreeNode tempParent = new BinarySearchTreeNode(); //Kökten başla ve ilerle BinarySearchTreeNode tempSearch = root; while (tempSearch != null) { tempParent = tempSearch; //Deger zaten var, çık. if (product == tempSearch.data) { return; } else if (tempSearch.left != null) { tempSearch = tempSearch.left; } else { tempSearch = tempSearch.left; } } BinarySearchTreeNode goingToInsert = new BinarySearchTreeNode(product); //Ağaç boş, köke ekle if (root == null) { root = goingToInsert; } else if (tempParent.left == null) { tempParent.left = goingToInsert; } else { tempParent.right = goingToInsert; } }
private void btnSearch_Click(object sender, EventArgs e) { BinarySearchTreeNode node = new BinarySearchTreeNode(); Product product = new Product(); string key = txtSearch.Text; foreach (BinarySearchTree itemTree in virtualMarketOperations.CategoryList()) { foreach (BinarySearchTreeNode itemNode in itemTree.SearchNodes()) { node = itemNode; product = (Product)itemNode.data; if (product.Brand == key || product.Category == key || product.Model == key || product.ProductDescription == key || product.ProductType == key) { MessageBox.Show("Product ;\n --> Category : " + product.Category + "\n --> Type : " + product.ProductType + "\n --> Brand/Model : " + product.Brand + " / " + product.Model + "\n --> Price : " + product.Price + "\n --> Quantity : " + product.Quantity + "\n --> Description : " + product.ProductDescription); break; } } } if (node.data == null) { MessageBox.Show(txtSearch.Text + " not found.."); } }
public bool Delete(int value) { BinarySearchTreeNode current = root; BinarySearchTreeNode parent = root; bool isLeft = true; // Find node while ((int)current.data != value) { parent = current; if (value < (int)current.data) { isLeft = true; current = current.left; } else { isLeft = false; current = current.right; } if (current == null) { return(false); } } // STATE 1: LEAF NODE if (current.left == null && current.right == null) { if (current == root) { root = null; } else if (isLeft) { parent.left = null; } else { parent.right = null; } } //STATE 2: SINGLE CHILDREN NODE else if (current.right == null) { if (current == root) { root = current.left; } else if (isLeft) { parent.left = current.left; } else { parent.right = current.left; } } else if (current.left == null) { if (current == root) { root = current.right; } else if (isLeft) { parent.left = current.right; } else { parent.right = current.right; } } //DURUM 3: TWO CHILDREN NODE else { BinarySearchTreeNode successor = Successor(current); if (current == root) { root = successor; } else if (isLeft) { parent.left = successor; } else { parent.right = successor; } successor.left = current.left; } return(true); }
public BinarySearchTree(BinarySearchTreeNode root) { this.root = root; }
private void Visit(BinarySearchTreeNode node) { nodes += node.data + " "; }
public BinarySearchTreeNode(object data) { this.data = data; left = null; right = null; }
public void AddToDefaultCategories() { for (int i = 0; i < 8; i++) { BinarySearchTree tree; BinarySearchTreeNode node = new BinarySearchTreeNode(); Product product; switch (i) // Product categories are determined here { case 0: product = new Product(); product.ProductType = "Computer"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 1: product = new Product(); product.ProductType = "White Goods"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 2: product = new Product(); product.ProductType = "Dress"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 3: product = new Product(); product.ProductType = "Stationary"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 4: product = new Product(); product.ProductType = "Decoration"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 5: product = new Product(); product.ProductType = "Garden"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 6: product = new Product(); product.ProductType = "Textile"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; case 7: product = new Product(); product.ProductType = "Foods"; node.data = product; tree = new BinarySearchTree(node); Category.Add(tree); break; } } }
public bool Delete(int value) { BinarySearchTreeNode current = root; BinarySearchTreeNode parent = root; bool isLeft = true; //DÜĞÜMÜ BUL while ((int)current.data != value) { parent = current; if (value < (int)current.data) { isLeft = true; current = current.left; } else { isLeft = false; current = current.right; } if (current == null) { return(false); } } //DURUM 1: YAPRAK DÜĞÜM if (current.left == null && current.right == null) { if (current == root) { root = null; } else if (isLeft) { parent.left = null; } else { parent.right = null; } } //DURUM 2: TEK ÇOCUKLU DÜĞÜM else if (current.right == null) { if (current == root) { root = current.left; } else if (isLeft) { parent.left = current.left; } else { parent.right = current.left; } } else if (current.left == null) { if (current == root) { root = current.right; } else if (isLeft) { parent.left = current.right; } else { parent.right = current.right; } } //DURUM 3: İKİ ÇOCUKLU DÜĞÜM else { BinarySearchTreeNode successor = Successor(current); if (current == root) { root = successor; } else if (isLeft) { parent.left = successor; } else { parent.right = successor; } successor.left = current.left; } return(true); }