public List <BinNode <T> > TraverseLevel(BinNode <T> node) { List <BinNode <T> > list = new List <BinNode <T> >(); if (null == node) { return(list); } Queue <BinNode <T> > queue = new Queue <BinNode <T> >(); queue.Enqueue(node); while (queue.Count > 0) { node = queue.Dequeue(); Console.Write(node.Element.ToString() + " "); list.Add(node); if (node.HasLChild()) { queue.Enqueue(node.LeftChild); } if (node.HasRChild()) { queue.Enqueue(node.RightChild); } } return(list); }
public static void Log(BinNode <T> rootNode, bool isRedBlackTree, bool showParent) { List <BTreeLogNode <T> > logNodeList = new List <BTreeLogNode <T> >(); GetLogNode(rootNode, null, false, logNodeList, isRedBlackTree); LogBinTree <T> .Log(logNodeList.ToArray(), showParent); }
//中序遍历:先左->跟->右 迭代实现 public void TraverseIn(BinNode <T> node) { if (null == node) { return; } Stack <BinNode <T> > stack = new Stack <BinNode <T> >(); stack.Push(node); while (stack.Count > 0) { while (null != node) { if ((node = node.LeftChild) != null) { stack.Push(node); } } node = stack.Pop(); Console.Write(node.Element.ToString() + " "); if (null != node && ((node = node.RightChild) != null)) { stack.Push(node); } } }
//后序遍历:先左->右->跟 迭代实现 public void TraversePost(BinNode <T> node) { if (null == node) { return; } Stack <BinNode <T> > result = new Stack <BinNode <T> >(); Stack <BinNode <T> > stack = new Stack <BinNode <T> >(); stack.Push(node); while (stack.Count > 0) { node = stack.Pop(); result.Push(node); if (node.HasLChild()) { stack.Push(node.LeftChild); } if (node.HasRChild()) { stack.Push(node.RightChild); } } while (result.Count > 0) { BinNode <T> temp = result.Pop(); Console.Write(temp.Element.ToString() + " "); } Console.WriteLine(); }
// 更新高度 protected void UpdateHeightAbove(BinNode <T> node) { while (null != node) // 从node出发,覆盖历代祖先 { UpdateHeight(node); node = node.ParentNode; } }
public BinNode <T> InsertAsRc(BinNode <T> node) { _rightChild = node; if (null != node) { node.ParentNode = this; } return(_rightChild); }
//后序遍历:先左->右->跟 递归实现 public void TraverseiPostRecursion(BinNode <T> node) { if (null == node) { return; } TraverseiPostRecursion(node.LeftChild); TraverseiPostRecursion(node.RightChild); Console.Write(node.Element.ToString() + " "); }
// node 为二叉树中的合法位置 public virtual bool Remove(BinNode <T> node) { if (node.IsRoot()) { Root = null; return(true); } if (node.IsLChild()) { node.ParentNode.LeftChild = null; } else { node.ParentNode.RightChild = null; } UpdateHeightAbove(node.ParentNode); return(true); }
// 层序遍历:按层从上到下,每层从左到右依次遍历 public void TraverseiLevelRecursion(BinNode <T> node) { Queue <BinNode <T> > queue = new Queue <BinNode <T> >(); queue.Enqueue(node); while (queue.Count > 0) { BinNode <T> temp = queue.Dequeue(); Console.Write(node.Element.ToString() + " "); if (null != temp.LeftChild) { queue.Enqueue(temp.LeftChild); } if (null != temp.RightChild) { queue.Enqueue(temp.RightChild); } } }
private static void GetLogNode(BinNode <T> node, BTreeLogNode <T> parent, bool left, List <BTreeLogNode <T> > logNodeList, bool isRedBlackTree) { if (null == node) { return; } BTreeLogNode <T> logNode = new BTreeLogNode <T>(); logNode.index = logNodeList.Count; logNode.element = node.Element; logNode.Color = node.Color; logNode.isRedBlackTree = isRedBlackTree; if (null != parent) { logNode.parentIndex = parent.index; if (left) { parent.leftChildIndex = logNode.index; } else { parent.rightChildIndex = logNode.index; } } logNodeList.Add(logNode); if (null != node.LeftChild) { GetLogNode(node.LeftChild, logNode, true, logNodeList, isRedBlackTree); } if (null != node.RightChild) { GetLogNode(node.RightChild, logNode, false, logNodeList, isRedBlackTree); } }
//先序遍历:先跟->左->右 迭代实现 public void TraversePre(BinNode <T> node) { if (null == node) { return; } Stack <BinNode <T> > stack = new Stack <BinNode <T> >(); stack.Push(node); while (stack.Count > 0) { node = stack.Pop(); Console.Write(node.Element.ToString() + " "); if (node.HasRChild()) { stack.Push(node.RightChild); } if (node.HasLChild()) { stack.Push(node.LeftChild); } } }
// 作为当前节点的右孩子插入新节点 public BinNode <T> InsertAsRc(T t) { BinNode <T> node = new BinNode <T>(t); return(InsertAsRc(node)); }
public BinNode(T value, BinNode <T> parent) { _element = value; ParentNode = parent; }
private BinNode <T> _root = null; // 跟节点 public BinTree() { _root = null; }
public static void Test() { BinTree <int> binTree = new BinTree <int>(); Console.WriteLine("insert:" + 8); BinNode <int> root = binTree.InsertAsRoot(8); Console.WriteLine("insert:" + 5); BinNode <int> node1 = binTree.InsertAsLc(root, 5); Console.WriteLine("insert:" + 10); BinNode <int> node2 = binTree.InsertAsRc(root, 10); Console.WriteLine("insert:" + 3); BinNode <int> node3 = binTree.InsertAsLc(node1, 3); Console.WriteLine("insert:" + 6); BinNode <int> node4 = binTree.InsertAsRc(node1, 6); Console.WriteLine("insert:" + 7); BinNode <int> node5 = binTree.InsertAsRc(node4, 7); Console.WriteLine("insert:" + 9); BinNode <int> node6 = binTree.InsertAsLc(node2, 9); Console.WriteLine("insert:" + 11); BinNode <int> node7 = binTree.InsertAsRc(node2, 11); BinTreeLogHelper <int> .Log(binTree.Root, false, false); Console.WriteLine(); List <BinNode <int> > list = binTree.TraverseLevel(binTree.Root); for (int i = 0; i < list.Count; ++i) { Console.WriteLine("heigh:" + list[i].Element.ToString() + " heigh:" + list[i].Height); } binTree.Remove(node2); BinTreeLogHelper <int> .Log(binTree.Root, false, false); Console.WriteLine("先序遍历"); binTree.TraversePreRecursion(binTree.Root); Console.WriteLine(); binTree.TraversePre(binTree.Root); Console.WriteLine(); binTree.TraversePre2(binTree.Root); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("中序遍历"); binTree.TraverseiInRecursion(binTree.Root); Console.WriteLine(); binTree.TraverseIn(binTree.Root); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("后序遍历"); binTree.TraverseiPostRecursion(binTree.Root); Console.WriteLine(); binTree.TraversePost(binTree.Root); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("层序遍历"); list = binTree.TraverseLevel(binTree.Root); for (int i = 0; i < list.Count; ++i) { Console.WriteLine("heigh:" + list[i].Element.ToString() + " heigh:" + list[i].Height); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); }
public virtual void Release() { _root = null; }
protected int NodeHeight(BinNode <T> node) { return((null != node) ? node.Height : -1); }
protected virtual int UpdateHeight(BinNode <T> node) { node.Height = 1 + Math.Max(NodeHeight(node.LeftChild), NodeHeight(node.RightChild)); return(node.Height); }
public virtual BinNode <T> InsertAsRoot(T t) { _root = new BinNode <T>(t); UpdateHeightAbove(_root); return(_root); }
public virtual BinNode <T> InsertAsRc(BinNode <T> node, T t) { node.InsertAsRc(t); UpdateHeightAbove(node); return(node.RightChild); }