Always for the binary search tree (also known as binary sorting tree, called Binary search tree), no good understanding, decided to take some time to learn and summarize.
Binary search tree is also a kind of binary tree. (Just like a heap is a kind of binary tree ...) )
However, the binary search tree also has other requirements: for all subtrees, the value of its root node is greater than the value of all nodes on the left subtree, and the value of all nodes on the small less right operand subtree.
For the wrong understanding: for all nodes, be larger than the left node, less than the right node. (PS: This understanding is wrong, be sure to pay attention to it.) )
One more thing to note: Our greater than and less than should be strict.
========== The following is mainly aimed at, its establishment, increase the deletion check these operations to explain ==============
====== for all data structures, we should have a clearer understanding of their additions and deletions ==========
First we define the structure of the binary tree (chain structure):
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ># node data structure typedef int ELEMENTTYPE;TYPEDEF struct{ elementtype Element; ElementType *left; ElementType *right;} treenode;# it is necessary to distinguish between nodes and trees. In particular, the point of the knot is that there is data + space. The tree is a pointer to the head node of the tree. Now, I'm going to give you your tree, it's definitely a pointer to the head node. </span></span>
Makeempty:
The so-called Makeempty is a two-fork search tree that is initialized to an empty one. (Note the word ' for ' = =)
Here is the initialization, which we understand as: now give you a tree (probably not an empty tree), how do we initialize it.
========== in one leg =========
It is necessary to explain the differences between the two concepts that I understand in terms of new and initialized.
Like what:
Now I'm going to create a new binary tree:
to create a new two-fork tree, create a new root node (or create a new pointer to the root node).
Now I'm going to initialize a binary tree:
Initialize the binary tree, it is now a binary tree has been built, I want to initialize him. In the absence of any node insertion, initialization is more embarrassing. No space to allocate, no initialization of data.
PPPs: The difference between these two concepts is still to be more deeply understood (especially the tree). Need to learn later.
I have learned a few things about this problem. Here is a post, the discussion of the comparison has >>>>>>http://bbs.csdn.net/topics/360102904
Add your own point of understanding: to create the words, you just need to specify that you have now created a structure of your data, and the initialization is that you need to process your data ...
For example, now you want to create and initialize a tree (no matter what the number):
Create a tree that you want an interface (head node) that gives the tree. Think about giving you a tree, what would you get? It's the tree that's got the head knot.
So what about initialization? To initialize a tree, we need to allocate space and store the data for the nodes that we say.
Now there should be a clear understanding of new and initialized ...
=========================
Code:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > Returned is a tree. Instead of.... treenode* makeempty (treenode* tree) { if (tree! = NULL) { makeempty (tree-left); Makeempty (tree-right); Free (tree); } return NULL;} </span></span>
Find, Findmin and Findmax:
For the lookup, our two-fork search tree has a certain advantage. (This is also why our two-fork search tree is also called a binary lookup tree).
For all types of lookups (no matter what element to find, existence, non-existent, maximum, minimum), our average lookup time is log (n), and n is all nodes in the tree.
<span style= "FONT-SIZE:18PX;" The >//return value is the address of the node. Look for a node with a value of x. treenode* Find (ElementType x, treenode* t) { if (t = = NULL) return null; if (T, Element > x) { return Find (x, TreeNode, left); } else if (T-Element < x) { return Find (x, TreeNode, right); } return T;} Find the lowest-value node: treenode* findmin (treenode* t) { if (t = = null) return null; if (t, left = = NULL) { return t; } Return Findmin (T-left);} Find the node with the maximum value: treenode* Findmax (treenode* t) { if (t = = null) return null; if (t, right = = NULL) { return t; } Return Findmax (T-right);} </span>
For binary search Tree lookup operation is still relatively simple ...
For his recursive writing is very simple, of course, for its circular writing is more simple ...
Insert:
Inserting the words, our best (or simplest) approach is to insert the nodes that need to be inserted into the leaf nodes.
The complexity of time is logn.
<span style= "FONT-SIZE:18PX;" >//Insert treenode* Insert (ElementType x, treenode* t) { if (t = = NULL) { t = (treenode*) malloc (sizeof (TreeNode)); C4/>t, Element = x; T, right = t, left = NULL; } if (x > t, Element) { T, right = Insert (x, t and right); } else if (x < T, Element) { T, left = Insert (x, left); } return T; Do not forget to return the two forks that were inserted after the tree. }</span>
Delete:
For the deletion of binary search tree, it is the hardest one in all operations. (As with other data structures, the hardest thing to do is delete.) )
There are a number of situations where the deletion occurs:
1, the deletion is the leaf knot point:
The simplest is to delete the leaf node. Deleting leaves has no connection to other nodes, so it is possible to delete them directly.
2, the deleted node has only one subtrees tree:
For only one tree node, it is also relatively simple, we directly put the node's only subtree to replace the node can be.
3, the deleted nodes have two subtrees trees:
The most troublesome thing is to delete this type of node, so what exactly should we do? We think that we should remove this node and then who should replace his position, so that still a binary search tree? The node should be smaller than all nodes of its right subtree, which is greater than the node of its left child tree. ------This is our clue. So we should find the largest node in its left subtree, or the smallest node in its right subtree.
Code:
treenode* Delete (ElementType x, treenode* t) { if (t = = NULL) return null; if (x > t, Element) { T, right = Delete (x, t and right); } else if (x < T, Element) { T, left = Delete (x, T-left); } Find the node you want to delete = = if (x, left && x, right) {//There are two subtrees trees //using the node with the lowest value on the tree on the left to replace the junction treenode* submin = F Indmin (T-right); T-element = submin element; T-right = Delete (T-Element, T-right); Replace the node with the node with the largest value on its left subtree treenode* Summax = Findmax (T); T-element = Summax element; T-left = Delete (T-Element, T-left); } else{//has a subtrees tree or no subtree treenode* tmp = T; if (t, left = = NULL) { t = t, right ; } else if (t-right = = NULL) { t = t, left; } Free (TMP);} }
So far, the basic operation of our two-fork search tree is these, but some very basic things, hope to be supplemented later.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure----Binary search tree