Beispiel #1
0
        public void AddElement(IComparable key, object data)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            TreeItem      newItem = CreateItem(key, data);
            TreeItemProxy root    = _retriever.GetRoot();

            if (root == null)
            {
                TreeItemProxy newItemProxy = _retriever.CreateItemProxy(newItem);
                _retriever.SetRoot(newItemProxy);
            }
            else
            {
                TreeItemProxy insertElement = root.GetParentElement(key);
                TreeItemProxy newItemProxy  = _retriever.CreateItemProxy(newItem);
                if (key.CompareTo(insertElement.Item.Key) < 0)
                {
                    insertElement.LeftChild = newItemProxy;
                }
                else
                {
                    insertElement.RightChild = newItemProxy;
                }
            }
        }
Beispiel #2
0
        public void ShowStructure()
        {
            TreeItemProxy tip = _retriever.GetRoot();

            if (tip.Item == null)
            {
                Console.WriteLine("empty");
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Root = " + tip.Item.Key.ToString() + "\r\n");
                tip.ShowStructure(sb);
                Console.WriteLine(sb.ToString());
            }
        }
Beispiel #3
0
        public void RemoveElement(IComparable key)
        {
            TreeItemProxy root = _retriever.GetRoot();

            if (root == null)
            {
                throw new ArgumentException("key does not exist");
            }
            TreeItemProxy tip = root.GetElement(key);

            if (tip == null)
            {
                throw new ArgumentException("key does not exist");
            }

            // Make element leaf
            if (tip.LeftChild != null)
            {
                TreeItemProxy swapElement = tip.LeftChild;
                while (swapElement.RightChild != null)
                {
                    swapElement = swapElement.RightChild;
                }
                SwapElements(tip, swapElement);
            }
            else
            if (tip.RightChild != null)
            {
                TreeItemProxy swapElement = tip.RightChild;
                while (swapElement.LeftChild != null)
                {
                    swapElement = swapElement.LeftChild;
                }
                SwapElements(tip, swapElement);
            }
        }
Beispiel #4
0
        private TreeItemProxy _root;         // null if not set

        public InMemoryTreeItemRetriever()
        {
            // An in-memory tree is always empty on start
            _root = null;
        }
Beispiel #5
0
 public void SetRoot(TreeItemProxy rootItem)
 {
     _root = rootItem;
 }
Beispiel #6
0
 public TreeItemProxy(TreeItem item, TreeItemProxy parent, bool isLeftChild)
 {
     this._item        = item;
     this._isLeftChild = isLeftChild;
 }
Beispiel #7
0
 public void SetRoot(TreeItemProxy rootItem)
 {
     // TODO:  Add DiskTreeItemRetriever.SetRoot implementation
 }
Beispiel #8
0
 public void SwapElements(TreeItemProxy innerNode, TreeItemProxy leaf)
 {
 }