Preface :
A binary search tree is a binary tree in which each element resides on a unique path from the root. The tree can store objects that have a natural order. This means that two object satisfy a comparison relation < (less than), == (equal to), or > (greater than). The ordering of objects creates an ordering among nodes in the tree.
Search Tree Ordering :
For each node in the tree, the values in its left subtree are less than the value of the node and the values in its right subtree are greater than the value of the node. Below figure illustrates binary search tree. Note that the order relation on the subtrees dictates that a binary search tree cannot have duplicate values :
A binary search tree has algorithms that maintain the ordering when inserting or deleting an element. Assuming the tree already exists, the insert algorithm becomes the basic for locating an element. Let us explore algorithms using a set of examples.
Building a Binary Search Tree :
Elements are inserted into a binary search tree using a strategy that maintains the ordering among the subtrees. The first element becomes the root node. All subsequent elements are added as leaf nodes at the end of a unique path. Identifying the path is an iterative process that begins at the root. Each step involves comparing the value of the new element, called item, with the value in the current node on the path. The following is the insertion strategy :
The figure below builds a binary search tree by adding integer values from the sequence {35, 18, 25, 48, 25, 20} :
Locating an Object in a Binary Search Tree :
Each node enters the tree along a specific path. A search for an object uses an iterative process that scans the same path. Compare the value of the root node with the item. If a match occurs, we identify the location of the object in the tree. If the object is less than the node value, the search continues with the left child; otherwise, the search continues with the right child. If the search ends at any empty tree, the object is not in the tree. Let us search for the element 32, 60 and 12 in the below binary search tree :
- Find 32 :
- Find 60 :
- Find 12 :
Because a search tree is a binary tree, we can apply any of the different scan algorithms to visit the nodes. The inorder scan is particular significant. The order of the visit to a node and its children is LNR, where the values are ordered left child < node < right childe. So the scan visits the elements in ascending order.
With a RNL (inorder_right) scan, the first node visited is along the path of right children and has the maximum value. The scan visits the nodes in descending order.
Removing a Binary Search Tree Node :
In a linked list, removing an element involves unlinking the node and connecting its predecessor to the next node. The process is a little different with a binary search tree. Without detailing the algorithm, let us look at an example that illustrate some of the issues that will concern us as we develop binary search tree as storage collections. In the following search tree, we delete the element 25 in the root node. Note immediately that the root node cannot be removed from the tree because we would be left with two orphaned subtrees.
We need to find a replacement value for 25. At first glance, we might want to use a child of the node, 10 or 37, as the replacement value. Unfortunately either choice destroys the search ordering of the tree because a subtree would be on the wrong side of the root. (Below figure (a) & (b)) We need a strategy that locates a replacement value that is "nearest to 25". Because the example is relatively small, we can identify that 15 in the left subtree of 30 in the right subtree would be a good choice. Let us look at 30 in the right subtree. We locate 30 by moving to the right child of 25 namely 37, and then scanning down the path of left children. Assign 30 as the new root value and splice out (delete) node 30 from the tree by connecting its right subtree (33) as the left child of the parent 37 (below figure (c)). As the example indicates, developing a general algorithm to delete a node in a search tree is challenging since it must maintain the ordering of the tree.
A binary search tree is a binary tree in which each element resides on a unique path from the root. The tree can store objects that have a natural order. This means that two object satisfy a comparison relation < (less than), == (equal to), or > (greater than). The ordering of objects creates an ordering among nodes in the tree.
Search Tree Ordering :
For each node in the tree, the values in its left subtree are less than the value of the node and the values in its right subtree are greater than the value of the node. Below figure illustrates binary search tree. Note that the order relation on the subtrees dictates that a binary search tree cannot have duplicate values :
A binary search tree has algorithms that maintain the ordering when inserting or deleting an element. Assuming the tree already exists, the insert algorithm becomes the basic for locating an element. Let us explore algorithms using a set of examples.
Building a Binary Search Tree :
Elements are inserted into a binary search tree using a strategy that maintains the ordering among the subtrees. The first element becomes the root node. All subsequent elements are added as leaf nodes at the end of a unique path. Identifying the path is an iterative process that begins at the root. Each step involves comparing the value of the new element, called item, with the value in the current node on the path. The following is the insertion strategy :
The figure below builds a binary search tree by adding integer values from the sequence {35, 18, 25, 48, 25, 20} :
Locating an Object in a Binary Search Tree :
Each node enters the tree along a specific path. A search for an object uses an iterative process that scans the same path. Compare the value of the root node with the item. If a match occurs, we identify the location of the object in the tree. If the object is less than the node value, the search continues with the left child; otherwise, the search continues with the right child. If the search ends at any empty tree, the object is not in the tree. Let us search for the element 32, 60 and 12 in the below binary search tree :
- Find 32 :
- Find 60 :
- Find 12 :
Because a search tree is a binary tree, we can apply any of the different scan algorithms to visit the nodes. The inorder scan is particular significant. The order of the visit to a node and its children is LNR, where the values are ordered left child < node < right childe. So the scan visits the elements in ascending order.
With a RNL (inorder_right) scan, the first node visited is along the path of right children and has the maximum value. The scan visits the nodes in descending order.
Removing a Binary Search Tree Node :
In a linked list, removing an element involves unlinking the node and connecting its predecessor to the next node. The process is a little different with a binary search tree. Without detailing the algorithm, let us look at an example that illustrate some of the issues that will concern us as we develop binary search tree as storage collections. In the following search tree, we delete the element 25 in the root node. Note immediately that the root node cannot be removed from the tree because we would be left with two orphaned subtrees.
We need to find a replacement value for 25. At first glance, we might want to use a child of the node, 10 or 37, as the replacement value. Unfortunately either choice destroys the search ordering of the tree because a subtree would be on the wrong side of the root. (Below figure (a) & (b)) We need a strategy that locates a replacement value that is "nearest to 25". Because the example is relatively small, we can identify that 15 in the left subtree of 30 in the right subtree would be a good choice. Let us look at 30 in the right subtree. We locate 30 by moving to the right child of 25 namely 37, and then scanning down the path of left children. Assign 30 as the new root value and splice out (delete) node 30 from the tree by connecting its right subtree (33) as the left child of the parent 37 (below figure (c)). As the example indicates, developing a general algorithm to delete a node in a search tree is challenging since it must maintain the ordering of the tree.
This message was edited 10 times. Last update was at 21/10/2010 14:26:55
沒有留言:
張貼留言