//public void Remove(Node root, ) public void Insert(Node root, Node newNode) { while (root != null) { if (newNode.data > root.data) { if (root.rightNode == null) { root.rightNode = newNode; break; } root = root.rightNode; } else { if (root.leftNode == null) { root.leftNode = newNode; break; } root = root.leftNode; } } }
public bool Search(int searchValue) { bool searchflag = false; current_Node = root; while (true) { if (current_Node.NodeValue == searchValue) { searchflag = true; break; } else if (current_Node.NodeValue > searchValue) { if (current_Node.lesserSubNode == null) { searchflag = false; break; } else { current_Node = current_Node.lesserSubNode; } } else if (current_Node.NodeValue < searchValue) { if (current_Node.greaterSubNode == null) { searchflag = false; break; } else { current_Node = current_Node.greaterSubNode; } } } return searchflag; }
public Node AddNode(int data) { Node temp = new Node(data); if (root == null) { root = temp; } count++; return temp; }
public bool Add(int data) { Node toAdd = new Node (); toAdd.NodeValue = data; toAdd.lesserSubNode = null; toAdd.greaterSubNode = null; toAdd.levelindex = 0; levelCalculator = 0; if (root == null) { root = toAdd; current_Node = toAdd; return true; } current_Node = root; while (true) { if (current_Node.NodeValue == data) { return false; } else if (current_Node.NodeValue > data) { levelCalculator++; if (current_Node.lesserSubNode == null) { toAdd.levelindex = levelCalculator; current_Node.lesserSubNode = toAdd; current_Node = toAdd; count++; if (level < levelCalculator) level = levelCalculator; return true; } else { current_Node = current_Node.lesserSubNode; } } else { levelCalculator++; if (current_Node.greaterSubNode == null) { toAdd.levelindex = levelCalculator; current_Node.greaterSubNode = toAdd; current_Node = toAdd; count++; if (level < levelCalculator) level = levelCalculator; return true; } else { current_Node = current_Node.greaterSubNode; } } } }
public void InsertintoBST(int data) { Node Newnode = new Node(); Newnode.data = data; if(root == null) { Newnode.left = null; Newnode.right = null; root = new Node(); } else { } }
public void add(Person p) { //trivial case if (root == null) { root = new Node(p); return; } Node current = root; while (current != null) { if (String.Compare(p.Name,current.person.Name) <= 0) { //if node to the right is empty, the current's left node is instantiated with the new data. //otherwise, current traverses left of the tree. if (current.left == null) { current.left = new Node(p); break; } else current = current.left; } else { if (current.right == null) { current.right = new Node(p); break; } else current = current.right; } } }
public void postOrder(Node root) { //lrv pattern if (root == null) return; Node left = root.left; Node right = root.right; postOrder(left); postOrder(right); root.print(); }
public void preOrder(Node root) { //vlr pattern if (root == null) return; Node left = root.left; Node right = root.right; root.print(); preOrder(left); preOrder(right); }
public void inOrder(Node root) { //lvr pattern if (root == null) return; Node left = root.left; Node right = root.right; inOrder(left); records.Add(root); root.print(); inOrder(right); }
public int Leaves(Node root) { //trivial case if (root == null) return 0; if (root.left == null && root.right == null) return 1; int leftleaves = Leaves(root.left); int rightleaves = Leaves(root.right); return leftleaves + rightleaves; }
public void Delete(int deleteValue) { root = DeleteKey (root, deleteValue); }
public Node(int data) { this.Value = data; left = null; right = null; }
public Node(int data) { NodeValue = data; greaterSubNode = null; lesserSubNode = null; }
public void Print(Node root) { if(root != null) { Print(root.leftNode); Console.WriteLine(root.data + " "); Print(root.rightNode); } }
//recursively counts the left height and right height of the tree. //returns the max height of tree. public int Height(Node root) { if (root == null) return -1; int leftht = Height(root.left); int rightht = Height(root.right); if (leftht > rightht) return leftht + 1; return rightht + 1; }
public Node(int d) { this.data = d; leftNode = null; rightNode = null; }
private void BuildBST(int data) { var node = new Node() { Data = data, LChild = null, RChild = null }; if (null == Root) { Root = node; return; } //指向当前节点。从根节点开始查找可以插入的位置。 //如果需要创建的值小于该节点,则往当前节点的左子树查找。否则往右子树查找。 //如果在左子树查找,且左子树的LChlid为空,说明这就是需要插入的位置 //右子树也是同样的道理 var current = Root; while (current != null) { if (data < current.Data) { if (null == current.LChild) { current.LChild = node; break; } else { current = current.LChild; } } else { if (null == current.RChild) { current.RChild = node; break; } else { current = current.RChild; } } } }
/// <summary> /// 先序遍历 /// </summary> /// <param name="node"></param> public void PreOrder(Node node) { if (null != node) { Console.Write(node.Data+","); PreOrder(node.LChild); PreOrder(node.RChild); } }
/// <summary> /// 中序遍历(先输出根节点,再输出左子树,最后输出右子树) /// 中序遍历输出的顺序总是有序的。 /// </summary> public void InOrder(Node node) { if (null != node) { InOrder(node.LChild); Console.Write(node.Data+","); InOrder(node.RChild); } }
public string search(Node root,string name) { //trivial case - if tree's root is empty OR if subroot is empty, return an empty string. if (root == null) return ""; string p = root.person.Name; if (p.Equals(name)) return p + "'s weight: " + root.person.Weight; string left = search(root.left, name); string right = search(root.right, name); return left + right; }
public Node(Node theinputnode) { greaterSubNode = theinputnode.greaterSubNode; lesserSubNode = theinputnode.lesserSubNode; NodeValue = theinputnode.NodeValue; }
void print(Node root, double x1, double y1) { if (root != null) { UILabel myLabels = new UILabel (new CGRect (x1,y1,25,25)); myLabels.Text = root.NodeValue.ToString (); myScroll.Add (myLabels); // print (root.lesserSubNode, x1 - (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50); // print (root.greaterSubNode, x1 + (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50); // // print (root.lesserSubNode, x1 - (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50); print (root.greaterSubNode, x1 + (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50); } }
public Tree() { root = null; }
private Node DeleteKey(Node theRootNode, int Key) { if (theRootNode == null) return theRootNode; if (Key < theRootNode.NodeValue) { theRootNode.lesserSubNode = DeleteKey (theRootNode.lesserSubNode, Key); } else if (Key > theRootNode.NodeValue) { theRootNode.greaterSubNode = DeleteKey (theRootNode.greaterSubNode, Key); } else { if (theRootNode.lesserSubNode == null) { return theRootNode.greaterSubNode; } else if (theRootNode.greaterSubNode == null) { return theRootNode.lesserSubNode; } // node with two children: smallest in the right subtree Node temp = new Node (theRootNode.greaterSubNode); while (true) { if (temp.lesserSubNode == null) break; temp = temp.lesserSubNode; } // Copy the inorder successor's content to this node theRootNode.NodeValue = temp.NodeValue; // Delete the inorder successor theRootNode.greaterSubNode = DeleteKey (theRootNode.greaterSubNode, temp.NodeValue); } return theRootNode; }
public BinarySearchTree() { count = 0; root = null; current_Node = root; }