private SplayTreeNode <T> Add(SplayTreeNode <T> paNode) { var newNode = paNode; var actualNode = _root; int compareResult; if (_root == null) { _root = newNode; _root.Parent = null; Count++; return(newNode); } while (true) { compareResult = Comparator.Compare <T>(actualNode.Data, newNode.Data); switch (compareResult) { case -1: if (actualNode.RightChild == null) { newNode.Parent = actualNode; actualNode.RightChild = newNode; Count++; return(newNode); } actualNode = actualNode.RightChild; break; case 0: return(null); case 1: if (actualNode.LeftChild == null) { newNode.Parent = actualNode; actualNode.LeftChild = newNode; Count++; return(newNode); } actualNode = actualNode.LeftChild; break; } } }
public bool MoveNext() { while (_stack.Count > 0 || CurrentNode != null) { if (CurrentNode != null) { _stack.Push(CurrentNode); CurrentNode = CurrentNode.LeftChild; } else { CurrentNode = _stack.Pop(); _ouput = CurrentNode; CurrentNode = CurrentNode.RightChild; return(true); } } return(false); }
public InOrderEnumerator(SplayTreeNode <T> paRoot) { _stack = new Stack <SplayTreeNode <T> >(); CurrentNode = paRoot; }
private bool LeftPosition(SplayTreeNode <T> paNode) { return(paNode.Parent.LeftChild == paNode); }
public void Delete(T paData) { var deleted = FindNode(paData, false); if (deleted == null) { return; } Count--; var parent = deleted.Parent; SplayTreeNode <T> findRes; if (deleted.RightChild != null) { findRes = deleted.RightChild; while (true) { if (findRes.LeftChild != null) { findRes = findRes.LeftChild; } else { break; } } deleted.Data = findRes.Data; if (findRes.RightChild != null) { if (LeftPosition(findRes)) { findRes.Parent.LeftChild = findRes.RightChild; } else { findRes.Parent.RightChild = findRes.RightChild; } findRes.RightChild.Parent = findRes.Parent; } else { if (LeftPosition(findRes)) { findRes.Parent.LeftChild = null; } else { findRes.Parent.RightChild = null; } } } else if (deleted.LeftChild != null) { findRes = deleted.LeftChild; while (true) { if (findRes.RightChild != null) { findRes = findRes.RightChild; } else { break; } } deleted.Data = findRes.Data; if (findRes.LeftChild != null) { if (LeftPosition(findRes)) { findRes.Parent.LeftChild = findRes.LeftChild; } else { findRes.Parent.RightChild = findRes.LeftChild; } findRes.LeftChild.Parent = findRes.Parent; } else { if (LeftPosition(findRes)) { findRes.Parent.LeftChild = null; } else { findRes.Parent.RightChild = null; } } } else { if (parent == null) { _root = null; } else { if (LeftPosition(deleted)) { deleted.Parent.LeftChild = null; } else { deleted.Parent.RightChild = null; } } } Splay(parent); }