public bool Find(int item) { //return Find(item, root); BSNode temp = root; while (true) { if (temp == null) { return(false); } if (temp.Data == item) { return(true); } if (item > temp.Data) { temp = temp.RightChild; } else { temp = temp.LeftChild; } } }
public bool Delete(int item) { BSNode temp = root; while (true) { if (temp == null) { return(false); } if (temp.Data == item) { Delete(temp); return(true); } if (item > temp.Data) { temp = temp.RightChild; } else { temp = temp.LeftChild; } } }
private void MiddleTraversal(BSNode node) //中序遍历 { if (node == null) { return; } MiddleTraversal(node.LeftChild); Console.Write(node.Data + " "); MiddleTraversal(node.RightChild); }
/// <summary> /// 中序遍历 /// </summary> /// <param name="root"></param> private void InorderTraversal(BSNode root) { if (root == null) { return; } InorderTraversal(root.LeftNode); Console.Write(root.data + " "); InorderTraversal(root.RightNode); }
private BSNode SearchMaxNode(BSNode root) { if (root == null) { return(null); } if (root.RightNode == null) { return(root); } else { return(SearchMaxNode(root.RightNode)); } }
private BSNode SearchMinNode(BSNode root) { if (root == null) { return(null); } if (root.LeftNode == null) { return(root); } else { return(SearchMinNode(root.LeftNode)); } }
public void Delete(BSNode node) { if (node.LeftChild == null && node.RightChild == null) //该节点是叶子节点时 { if (node.Parent == null) { root = null; } else if (node.Parent.LeftChild == node) { node.Parent.LeftChild = null; } else if (node.Parent.RightChild == node) { node.Parent.RightChild = null; } return; } if (node.LeftChild == null && node.RightChild != null) //该节点只有右孩子时,好像有问题 { node.Data = node.RightChild.Data; node.RightChild = null; return; } if (node.LeftChild != null && node.RightChild == null) //该节点只有左孩子时 { node.Data = node.LeftChild.Data; node.LeftChild = null; return; } BSNode temp = node.RightChild; //该节点左右孩子都有时,取其右子树上的最小值补在该节点 while (true) { if (temp.LeftChild != null) { temp = temp.LeftChild; } else { break; } } node.Data = temp.Data; Delete(temp); }
public void Add(int item) { BSNode newNode = new BSNode(item); if (root == null) { root = newNode; root.Parent = null; nodeCount = 1; } else { //如果已经存在,则不进行插入 if (SearchNode(root, item) != null) { Console.WriteLine(item + " 已存在"); return; } BSNode tempNode = null; BSNode rootNode = root;//这里只是将root的指针传给了rootNode while (rootNode != null) { tempNode = rootNode; newNode.Parent = tempNode; //这里只是更改了rootNode的指向,并不影响root rootNode = rootNode.data > item ? rootNode.LeftNode : rootNode.RightNode; } //Console.WriteLine("rootNode:" + (rootNode == null) + "===" + tempNode.data + "~~~" + root.data); if (item < tempNode.data) { tempNode.LeftNode = newNode; } else { tempNode.RightNode = newNode; } nodeCount++; } }
public void TreeTraversal(BSNode root = null) { if (root != null) { BreadthFirstTravel(root); } else { if (this.root == null) { return; } else { BreadthFirstTravel(this.root); } } }
private bool Find(int item, BSNode node) { if (node == null) { return(false); } if (node.Data == item) { return(true); } else if (item > node.Data) { return(Find(item, node.RightChild)); } else { return(Find(item, node.LeftChild)); } }
public void Add(int item) { BSNode newNode = new BSNode(item); if (root == null) { root = newNode; } else { BSNode temp = root; while (true) { if (item >= temp.Data) //放在temp的右边 { if (temp.RightChild == null) { temp.RightChild = newNode; newNode.Parent = temp; break; } else { temp = temp.RightChild; } } else //放在temp的左边 { if (temp.LeftChild == null) { temp.LeftChild = newNode; newNode.Parent = temp; break; } else { temp = temp.LeftChild; } } } } }
public BSNode SearchNode(BSNode root, int key) { if (root == null) { return(null); } if (key > root.data) { return(SearchNode(root.RightNode, key)); } else if (key < root.data) { return(SearchNode(root.LeftNode, key)); } else { return(root); } }
private void BreadthFirstTravel(BSNode root) { Queue <BSNode> que = new Queue <BSNode>(); que.Enqueue(root); while (que.Count != 0) { root = que.Peek(); Console.Write(root.data + " "); que.Dequeue(); if (root.LeftNode != null) { que.Enqueue(root.LeftNode); } if (root.RightNode != null) { que.Enqueue(root.RightNode); } } }
private void Delete(BSNode root, int data) { //root = null;//没有效果 //this.root = null;//这个才能真正赋值为null BSNode tempRoot = root; if (root == null) { return; } //如果找到了指定节点 if (data == tempRoot.data) { //如果node是叶子节点 if (tempRoot.LeftNode == null && tempRoot.RightNode == null) { //Console.WriteLine("设置为空......"); //tempRoot = null;//为什么没有效果 // root = null;//为什么没有效果 //为什么这样就可以 if (tempRoot.Parent == null) { //tempRoot = null;//没有效果 //root = null;//同样没有效果 this.root = null; } else if (tempRoot.Parent.LeftNode != null) { tempRoot.Parent.LeftNode = null; } else if (tempRoot.Parent.RightNode != null) { tempRoot.Parent.RightNode = null; } } else { //如果左右节点都不为空 if (tempRoot.LeftNode != null && tempRoot.RightNode != null) { //method1:以右子树内的最小节点取代该节点 BSNode minR = tempRoot.RightNode; if (minR.LeftNode == null) { tempRoot.data = minR.data; tempRoot.RightNode = null; //minR.data = 70;//进行了改变啊 //minR = null;//直接使用miR = null为什么不行? } else { BSNode minRParent = null; while (minR.LeftNode != null) { minRParent = minR; minR = minR.LeftNode; } minRParent.LeftNode = null; tempRoot.data = minR.data; tempRoot.RightNode = minRParent; } //method2:以左子树内的最大节点取代该节点 } //如果左节点不为空 else if (tempRoot.LeftNode != null && tempRoot.RightNode == null) { tempRoot = tempRoot.LeftNode; } //如果右节点不为空 else if (tempRoot.LeftNode == null && tempRoot.RightNode != null) { tempRoot = tempRoot.RightNode; } } } else if (data > tempRoot.data) { Delete(tempRoot.RightNode, data); } else if (data < tempRoot.data) { Delete(tempRoot.LeftNode, data); } }