Tag: binary search tree

如何迭代地找到BST的高度?

public void HeightIterative() { int counter = 0; int counter2 = 0; TreeNode current=root; if(current != null) { while(current.LeftNode!=null) { counter++; current = current.LeftNode; } while(current.RightNode!=null) { counter2++; current = current.RightNode; } } int res = 1+Math.Max(counter, counter2); Console.WriteLine(“The Height Of Tree Is: “+res); } 我写了迭代方法,来计算树的高度。 但在某些情况下它不能正常工作。 如案例:10 1 2 3 4 5 18 17 […]

C#二叉树和字典

我正在努力解决何时使用二叉搜索树以及何时使用字典的概念。 在我的应用程序中,我做了一个小实验,使用了C5库TreeDictionary (我相信是一个红黑二叉搜索树)和C#字典。 字典在添加/查找操作时总是更快,并且总是使用更少的内存空间。 例如,在16809 条目中,字典使用342 KiB,而树使用723 KiB。 我认为BST应该是更高效的内存,但似乎树的一个节点需要比字典中的一个条目更多的字节。 是什么赋予了? BST比词典更好吗? 另外,作为一个附带问题,有没有人知道是否存在更快+更高内存效率的数据结构,用于存储字典类型访问的对,而不是上述任何一种结构?