private void AddTo(AvlTreeNode <T> node, T item)
 {
     if (item.CompareTo(node.Item) < 0)
     {
         if (node.Left == null)
         {
             node.Left = new AvlTreeNode <T>(item, node, this);
         }
         else
         {
             AddTo(node.Left, item);
         }
     }
     else
     {
         if (node.Right == null)
         {
             node.Right = new AvlTreeNode <T>(item, node, this);
         }
         else
         {
             AddTo(node.Right, item);
         }
     }
     node.Balance();
 }
        private void Traverse(AvlTreeNode <T> node, int level, ref string s, Action <T> action)
        {
            if (node == null)
            {
                return;
            }
            var reverse = TraverseMode == TraverseModeEnum.ReverseInOrder;

            if (TraverseMode == TraverseModeEnum.PreOrder)
            {
                s += string.Empty.PadLeft(level, ' ') + node.Item + "\n";
                action?.Invoke(node.Item);
            }
            Traverse(reverse ? node.Right : node.Left, level + 2, ref s, action);
            if (TraverseMode == TraverseModeEnum.InOrder || TraverseMode == TraverseModeEnum.ReverseInOrder)
            {
                s += string.Empty.PadLeft(level, ' ') + node.Item + "\n";
                action?.Invoke(node.Item);
            }
            Traverse(reverse ? node.Left : node.Right, level + 2, ref s, action);
            if (TraverseMode != TraverseModeEnum.PostOrder)
            {
                return;
            }
            s += string.Empty.PadLeft(level, ' ') + node.Item + "\n";
            action?.Invoke(node.Item);
        }
 public void Add(T item)
 {
     if (Root == null)
     {
         Root = new AvlTreeNode <T>(item, null, this);
     }
     else
     {
         AddTo(Root, item);
     }
     Count++;
 }
Ejemplo n.º 4
0
 public AvlTreeNode(TNode item, AvlTreeNode <TNode> parent, AvlTree <TNode> tree)
 {
     Item   = item;
     Parent = parent;
     _tree  = tree;
 }