"Algorithm" fourth version of the source code: http://algs4.cs.princeton.edu/code/, looked at or very fruitful!
Same point:
1, Linked list: node includes (value (not necessarily what type!!!) ), Next, N (can add a parameter)) Binary Lookup tree node (value (a key value pair, key is responsible for node operation, value is responsible for node storage information), right, left, N (the number of nodes with that root node's child node))
2, linked list (necessary element head): The entrance is head, every time we want to operate the chain list, we must ensure that the new list of the head of the correctness;
Binary tree (essential element root): The entrance is root, the binary tree is the same, always ensure that the original root, and what parameters are compared with root;
3, why use two fork search tree: Set two points to find and list the advantages of a!
4, two-point search:
Two-point search of iterative thought, first sort, then find
public int rank (key key) {
int lo = 0;
int hi = N-1;
while (Loint mid = (lo + hi)/2;
The CompareTo here can be compared to any class that extends to comparable!!!
int cmp = Key.compareto (Key[mid]);
if (cmp = = 0) return mid;
if (CMP < 0) Hi = mid-1;
if (cmp > 0) lo = mid + 1;
}
return lo;
}
5, the operation of the linked list:
1, set the speed of the pointer (a first go, a and so on);
2. Set two synchronous pointers (only two pointers are different steps, one step at a time, one at a time);
3. Declare a new node. The node to be operated on will be stored inside, and then prepare for the next time after the operation is finished;
//especially to do operation on a node, because there is a connection between the two nodes! The latter node also uses the relationship before the node changes.
Operation on tree: recursive, middle sequence traversal;
When we call Detele, we call the following method, but this method does not return a value, so we can not take advantage of the recursive operation node, so we could overload a private method to encapsulate the logic of the operation, when called, there is a special method to take charge of the call;
There is a return value, so recursion is simpler!!
public void Delete (key key) {
Root = delete (root, key);
}
Always operate as root
To understand the meaning of recursion!
PrivateNode Delete (node x, key key) {
if (x = = null) return null;
Find key first
int cmp = Key.compareto (X.key);
if (cmp > 0) x.right = delete (X.right, key); This place to note that recursion is a stack, the x in parentheses is different from the outside X, which is equivalent to the x!! of the upper layer of the stack.
if (CMP < 0) X.left = delete (X.left, key);
else if {
When found, judge its left and right two sub-nodes
Node t = x; Here's a new node that stores the X information to be deleted!!!! To represent the original x.
if (x.right = = null) return x.left; This step shows the disconnection; This step shows that the left node of X is attached to the upper layer of x in place of the x position;
if (X.left = = null) return x.right;
If the two child nodes are not empty!
x = min (t.right);
X.right = T.right;
X.left = T.left;
}
X.N = Size (x.left) + size (x.right) +1;
return x;
}
Outside the question:
1, to a class (extends comparable this interface, using (rewrite) One of the CompareTo method, find the class of two objects of comparison principle, return 0,-1,1)
2. These objects are then sorted by using the sort () method for these objects (usually in arrays or collections);
3, a good order, you can import the corresponding search efficient data structure;
4, in these data structures, using the method provided by the operation;
Questions:
1, ordinary binary tree, how to convert to two forks to find the tree,,, (perhaps, how to put a sorted array-> Two fork to find the tree?). Middle order traversal)
Linked list and two-fork lookup tree