Binary Search Tree Motivation Why we need BST? We learned binary search before, if we search for an element within a sorted array is fast. The search will be in O(logn), but arrays suffer from being slow, O(n), to insert new elements . So we need a search trees attempt to fix the inefficiency of insertion whilst keeping good O(log n) properties for search. A binary search tree is a binary tree storing key-value entries at its internal nodes and satisfying the following “search tree” property: let u, v and w be any three nodes such that u is in the left subtree of v and w is in the right subtree of v. Then we must have key(u)<= key(v) <= key(w) Now we are going to implement the binary search tree. Similar to the previous implementation, when we want to implement a tree-structure, what we should do first is to define Node in tree. Search Search for a BST is relatively easy, we have learned binary search before, so we use the similar style. To search for...
评论
发表评论