Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Binary Search Tree

Battistella Stefano edited this pageFeb 3, 2015 ·7 revisions

In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure where each node has a comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right sub-tree. Each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node. BSTs are also dynamic data structures, and the size of a BST is only limited by the amount of free memory in the operating system. The main advantage of binary search trees is that it remains ordered, which provides quicker search times than many other data structures.

Wikipedia

vartree=newBSTree();//an empty tree

Methods

getIterator()

This method returns an iterator for scanning the tree. The iterator is useful in order to get a full decoupling in your classes. This avoid, to the class that uses the iterator, to know what type of data structure stores the data.

vartree=newBSTree();varit=tree.getIterator();for(it.first();!it.isDone();it.next()){varitem=it.getItem();//do something}

The iterator starts from the most left item of the tree.

Complexity:O(1)

insert(key, item)

This method insert the item into the tree. The item could be whatever you want.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2

Complexity:O(logn)

search(key, node)

This method search the item relatives to the key in the tree. The search could start from a desired node of the tree but this parameter could be omitted. The search returns undefined if the key there isn't in the tree.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2tree.search(0);//8tree.search(0,tree.maximum());//undefined

Complexity:O(logn)

minimum(node)

This method returns the node relatives to the minimum key of the tree. The parameter node is optional, but it's necessary if you desire the minimum key of the tree represented by the node as root.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2tree.minimum();//8tree.minimum(tree.maximum());//2

Complexity:O(logn)

maximum(node)

This method returns the node relatives to the maximum key of the tree. The parameter node is optional, but it's necessary if you desire the maximum key of the tree represented by the node as root.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2tree.maximum();//2tree.maximum(tree.minimum());//8

Complexity:O(logn)

successor(node)

This method returns the node that's the successor in the tree. If the node correspond with the maximum, null will be returned.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2tree.successor(tree.minimum());//2tree.successor(tree.maximum());//null

Complexity:O(logn)

predecessor(node)

This method returns the node that's the predecessor in the tree. If the node correspond with the minimum, null will be returned.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2tree.successor(tree.maximum());//8tree.successor(tree.minimum());//null

Complexity:O(logn)

deleteNode(node)

This method delete the node from the tree.

vartree=newBSTree();tree.insert(0,8);//tree contains 8tree.insert(1,2);//tree contains 8, 2tree.deleteNode(tree.maximum());//tree contains 2

Complexity:O(logn)

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp