public ZBinaryTreeNode <T> Find(T data) { ZBinaryTreeNode <T> node = null; ZBinaryTreeNode <T> current = Root; while (current != null) { int compareResult = current.Value.CompareTo(data); if (compareResult == 0) { return(current); } else if (compareResult < 0) { current = current.LeftNode; } else { current = current.RightNode; } } return(node); }
private static ZBinaryTreeNode <T> BuildTree(List <T> preorderList, List <T> inorderList, int start, int end, int preIndex) { if (start > end) { return(null); } //Pick current node from preorder traversal using preIndex and increment preIndex ZBinaryTreeNode <T> node = new ZBinaryTreeNode <T>(preorderList[preIndex++]); //If this node has no children then return if (start == end) { return(node); } //Else find the index of this node in inorder traversal int inIndex = Search(inorderList, start, end, node.Value); //Using index in inorder traversal, construct left and right subtrees node.LeftNode = BuildTree(inorderList, preorderList, start, inIndex - 1, preIndex); node.RightNode = BuildTree(inorderList, preorderList, inIndex + 1, end, preIndex); return(node); }
public List <T> LevelOrderList() { List <T> items = new List <T>(); ZQueue <ZBinaryTreeNode <T> > que = new ZQueue <ZBinaryTreeNode <T> >(); if (Root != null) { items.Add(Root.Value); if (Root.LeftNode != null) { que.Enqueue(Root.LeftNode); } if (Root.RightNode != null) { que.Enqueue(Root.RightNode); } } while (que.Count > 0) { ZBinaryTreeNode <T> node = que.Dequeue(); items.Add(node.Value); if (Root.LeftNode != null) { que.Enqueue(Root.LeftNode); } if (Root.RightNode != null) { que.Enqueue(Root.RightNode); } } return(items); }
public ZBinaryTreeNode <T> FindParent(T data) { ZBinaryTreeNode <T> current = Root; ZBinaryTreeNode <T> parent = null; if (Root.Value.Equals(data)) { return(parent); } while (current != null) { int compareResult = current.Value.CompareTo(data); if (compareResult == 0) { return(parent); } else if (compareResult < 0) { parent = current; current = current.LeftNode; } else { parent = current; current = current.RightNode; } } return(parent); }
public void Delete(T data) { ZBinaryTreeNode <T> node = null; ZBinaryTreeNode <T> parent = FindParent(data); if (parent != null) { if (parent.LeftNode != null && parent.LeftNode.Value.Equals(data)) { node = parent.LeftNode; //case1: has no children. if (node.LeftNode == null && node.RightNode == null) { parent.LeftNode = null; } //case2: has left child but no right child. else if (node.RightNode == null) { parent.LeftNode = node.LeftNode; } //case3: has right child and right child has no left child of it's own. else if (node.RightNode.LeftNode == null) { parent.LeftNode = node.RightNode; } //case4: Finally, if the deleted node's right child does have a left child, then the deleted node needs to be replaced by the deleted node's right child's left-most descendant. That is, we replace the deleted node with the deleted node's right subtree's smallest value. else { ZBinaryTreeNode <T> minNode = FindMin(node.RightNode); parent.LeftNode = minNode; } } else if (parent.RightNode != null && parent.RightNode.Value.Equals(data)) { node = parent.RightNode; //case1: has no children. if (node.LeftNode == null && node.RightNode == null) { parent.RightNode = null; } //case2: has left child but no right child. else if (node.RightNode == null) { parent.RightNode = node.LeftNode; } //case3: has right child and right child has no left child of it's own. else if (node.RightNode.LeftNode == null) { parent.RightNode = node.RightNode; } //case4: Finally, if the deleted node's right child does have a left child, then the deleted node needs to be replaced by the deleted node's right child's left-most descendant. That is, we replace the deleted node with the deleted node's right subtree's smallest value. else { ZBinaryTreeNode <T> minNode = FindMin(node.RightNode); parent.RightNode = minNode; } } } }
private void PostOrder(ZBinaryTreeNode <T> node, List <T> items) { if (node != null) { PostOrder(node.LeftNode, items); PostOrder(node.RightNode, items); items.Add(node.Value); } }
public ZBinaryTreeNode <T> FindMin(ZBinaryTreeNode <T> node) { if (node.LeftNode == null) { return(node); } else { return(FindMax(node.LeftNode)); } }
public void Add(T data) { ZBinaryTreeNode <T> current = Root; if (current == null) { Root = new ZBinaryTreeNode <T>(data); } else { while (true) { int compareResult = current.Value.CompareTo(data); if (compareResult < 0) { if (current.RightNode == null) { current.RightNode = new ZBinaryTreeNode <T>(data); return; } else { current = current.RightNode; } } if (compareResult > 0) { if (current.LeftNode == null) { current.LeftNode = new ZBinaryTreeNode <T>(data); return; } else { current = current.LeftNode; } } else { return; } } } }
private int CountChildren(ZBinaryTreeNode <T> node) { if (node == null) { return(0); } int left = CountChildren(node.LeftNode); int right = CountChildren(node.RightNode); if (left >= right) { return(left + 1); } else { return(right + 1); } }
private bool GetAncesters(ZBinaryTreeNode <T> node, T data, List <ZBinaryTreeNode <T> > items) { if (node != null) { if (!node.Value.Equals(data)) { if (GetAncesters(node.LeftNode, data, items) || GetAncesters(node.RightNode, data, items)) { items.Add(node); return(true); } } else { return(true); } } return(false); }
public ZBinaryTreeNode <T> Search(T data) { ZBinaryTreeNode <T> match = null; ZQueue <ZBinaryTreeNode <T> > que = new ZQueue <ZBinaryTreeNode <T> >(); if (Root != null) { if (data.Equals(Root.Value)) { return(Root); } if (Root.LeftNode != null) { que.Enqueue(Root.LeftNode); } if (Root.RightNode != null) { que.Enqueue(Root.RightNode); } } while (que.Count > 0) { ZBinaryTreeNode <T> node = que.Dequeue(); if (data.Equals(node.Value)) { return(node); } if (Root.LeftNode != null) { que.Enqueue(Root.LeftNode); } if (Root.RightNode != null) { que.Enqueue(Root.RightNode); } } return(match); }
public int CountFullNodes() { int total = 0; ZQueue <ZBinaryTreeNode <T> > que = new ZQueue <ZBinaryTreeNode <T> >(); if (Root != null) { if (Root.LeftNode != null && Root.RightNode != null) { total++; } if (Root.LeftNode != null) { que.Enqueue(Root.LeftNode); } if (Root.RightNode != null) { que.Enqueue(Root.RightNode); } } while (que.Count > 0) { ZBinaryTreeNode <T> node = que.Dequeue(); if (Root.LeftNode != null && Root.RightNode != null) { total++; } if (Root.LeftNode != null) { que.Enqueue(Root.LeftNode); } if (Root.RightNode != null) { que.Enqueue(Root.RightNode); } } return(total); }
public ZBinaryTreeNode(T data, ZBinaryTreeNode <T> left, ZBinaryTreeNode <T> right) { this.Value = data; this.LeftNode = left; this.RightNode = right; }