The search tree data structure supports many dynamic-set operations, includingSEARCH,MINIMUM,MAXIMUM,PREDECESSOR,SUCCESSOR,INSERT, andDELETE. Thus, we can use a search tree both as a dictionary and as a priority queue.
1.Definition:
a binary search tree (BST), sometimes also called anordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- The left and right subtree each must also be a binary search tree.
- There must be no duplicate nodes.
Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.
The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.
The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.
Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.
Binary-search-tree property:
Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key < x.key. If y is a node in the right subtree of x, then y.key > x.key.
2. operations
2.1 Traversal(遍历)
遍历分为三种,分别是
- inorder tree walk,print out all the keys in a binary search tree in sorted order by a simple recursive algorithm.
- preorder tree walk ,prints the root before the values in either subtree.
- postorder tree walk ,print the root after the values in its subtrees.
inorder tree walk的伪代码:
遍历即将树的所有结点访问且仅访问一次。按照根节点位置的不同分为前序遍历,中序遍历,后序遍历。