[Tree] Some operations on a binary search tree
[qads]
Binary search tree (CNPTK) is a binary tree in which each node, key lock button is greater consideration of all the nodes of the tree and left the smaller keys of all the nodes of the tree must.
Here is an example of a binary search tree:
Content
Tree structure
Add elements to the tree
Enter tree
Browse Tree
Find a node in the tree
Deleting a node in the tree
Code Complete Reference
Thanks key constraints on CNPTK, finding becomes oriented. Further, by the search tree structure becomes significantly faster. If N is the number of nodes in the tree, the average search cost about log2N.
Tree structure:
typedef int item; //kieu item la kieu nguyen struct Node { item key; //truong key cua du lieu Node *Left, *Right; //con trai va con phai }; typedef Node *Tree; //cay
So we have a tree structure Tree. Now we will take a number of actions.
More 1 element in the tree
Adding an element X in the tree must satisfy the constraint of CNPTK. You can add many different places on the tree, but if you add a leaf node will be the most convenient because we can perform the same search operation. Upon termination of the search process is also time to find room for more.
int insertNode(Tree &T, item x) // chen 1 Node vao cay { if (T != NULL) { if (T->key == x) return -1; // Node nay da co if (T->key > x) return insertNode(T->Left, x); // chen vao Node trai else if (T->key < x) return insertNode(T->Right, x); // chen vao Node phai } T = (Node *) malloc(sizeof(Node)); if (T == NULL) return 0; // khong du bo nho T->key = x; T->Left = T->Right = NULL; return 1; // ok }
Enter tree
The tree is entering the repeated insertion 1 element into the tree to a certain condition, then stop. In the code below the stop condition input element = 0.
void CreateTree(Tree &T) // nhap cay { int x; while (1) { printf("Nhap vao Node: "); scanf("%d", &x); if (x == 0) break; // x = 0 thi thoat int check = insertNode(T, x); if (check == -1) printf("Node da ton tai!"); else if (check == 0) printf("Khong du bo nho"); } }
The import has finished, Although many people like that, but the code does not know what type of tree entry :3 (I have been so)
Browse Tree
Manipulation browse tree on binary search tree is exactly like the binary tree. Especially when browsing the order between, browse button sequence will give a series of buttons in ascending order of the key. (1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 20, 23)
void LNR(Tree T) { if(T!=NULL) { LNR(T->Left); printf("%d ",T->key); LNR(T->Right); } }
The browser in order before or after you do the same.
Find a Node key = x in the tree
Node* searchKey(Tree T, item x) // tim nut co key x { if (T!=NULL) { if (T->key == x) { Node *P = T; return P;} if (T->key > x) return searchKey(T->Left, x); if (T->key < x) return searchKey(T->Right, x); } return NULL; }
Finding himself mentioned above is very simple and quick significantly.
Delete Node key = x in the tree
Disposing of an element X from the tree to ensure the constraint of CNPTK. Have 3 the case of a termination node X may occur:
– X is a leaf node: Simply cancel the X because it is not hooked up to any other element.
– X only 1 with (left or right): Before canceling X X we hook father to his only child.
– X for all 2 with: It can not be canceled directly by X have enough 2 with. I will destroy indirect. Instead of canceling X, we will find a network element that Q. This element has a maximum. Information stored in Q will be transferred to the store at X. Later, button will be canceled Q really like 2 the first case. The problem is to choose Q such that when the flow Q in place of X, plant remains CNPTK.
Have 2 element satisfactory:
+ The smallest element (leftmost) on seedlings to.
+ The largest element (rightmost) on the left tree.
The selection element is the network element that is completely dependent on the whim of programming. In this code I select element to most.
int delKey(Tree &T, item x) // xoa nut co key x { if (T==NULL) return 0; else if (T->key > x) return delKey(T->Left, x); else if (T->key < x) return delKey(T->Right, x); else // T->key == x { if (T->Left == NULL) T = T->Right; // Node chi co cay con phai else if (T->Right == NULL) T = T->Left; // Node chi co cay con trai else // Node co ca 2 con { Node *Q = T->Left; while (Q->Right != NULL) { Q = Q->Right; } T->key = Q->key; delKey(T->Left, Q->key); } } return 1; }
So has finished deleting a node.
Reference Code:
#include<stdlib.h> #include<stdio.h> typedef int item; //kieu item la kieu nguyen struct Node { item key; //truong key cua du lieu Node *Left, *Right; //con trai va con phai }; typedef Node *Tree; //cay int insertNode(Tree &T, item x) // chen 1 Node vao cay { if (T != NULL) { if (T->key == x) return -1; // Node nay da co if (T->key > x) return insertNode(T->Left, x); // chen vao Node trai else if (T->key < x) return insertNode(T->Right, x); // chen vao Node phai } T = (Node *) malloc(sizeof(Node)); if (T == NULL) return 0; // khong du bo nho T->key = x; T->Left = T->Right = NULL; return 1; // ok } void CreateTree(Tree &T) // nhap cay { int x; while (1) { printf("Nhap vao Node: "); scanf("%d", &x); if (x == 0) break; // x = 0 thi thoat int check = insertNode(T, x); if (check == -1) printf("Node da ton tai!"); else if (check == 0) printf("Khong du bo nho"); } } // Duyet theo LNR void LNR(Tree T) { if(T!=NULL) { LNR(T->Left); printf("%d ",T->key); LNR(T->Right); } } Node* searchKey(Tree T, item x) // tim nut co key x { if (T!=NULL) { if (T->key == x) { Node *P = T; return P;} if (T->key > x) return searchKey(T->Left, x); if (T->key < x) return searchKey(T->Right, x); } return NULL; } int delKey(Tree &T, item x) // xoa nut co key x { if (T==NULL) return 0; else if (T->key > x) return delKey(T->Left, x); else if (T->key < x) return delKey(T->Right, x); else // T->key == x { if (T->Left == NULL) T = T->Right; // Node chi co cay con phai else if (T->Right == NULL) T = T->Left; // Node chi co cay con trai else // Node co ca 2 con { Node *Q = T->Left; while (Q->Right != NULL) { Q = Q->Right; } T->key = Q->key; delKey(T->Left, Q->key); } } return 1; } int main() { Tree T; T=NULL; //Tao cay rong CreateTree(T); //Nhap cay //duyet cay printf("Duyet cay theo LNR: \n"); LNR(T); printf("\n"); Node *P; item x; printf("Nhap vao key can tim: "); scanf("%d", &x); P = searchKey(T, x); if (P != NULL) printf("Tim thay key %d\n", P->key); else printf("Key %d khong co trong cay\n", x); if (delKey(T, x)) printf("Xoa thanh cong\n"); else printf("Khong tim thay key %d can xoan", x); printf("Duyet cay theo LNR: \n"); LNR(T); printf("\n"); return 0; }
Read more: Jump binary tree into a binary search tree
Good blog! I really love how it is easy on my eyes and the data are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your feed which must do the trick! Have a nice day! efdecfbbekke
Thank you very much. You take care my blog. If you have a wordpress account and want follow me, you can click button “Follow” at top:
or you can back Home page and see at bottom. Enter your email and click button “Subscribe”. I will send to your email whenever a new post has been made:
he asked me that I do not understand the code by java.nhung 1 space is T = (Node *) malloc(sizeof(Node)); think what a?
I shudder TNode root and ;
class TNode{
int data;
TNode left,right;
TNode(int x){
data=x;
left=right=null;
}
TNode(int x,TNode ll,TNode rr){
data=x;
left=ll;
right=rr;
}
}
public class Demo {
TNode root;
int insertNode(TNode T,int x){
if(T!=null){
if(T.data==x)
return -1;
else{
if(T.data<x)
return insertNode(T.right,x);
else
return insertNode(T.left,x);
}
}
if (T == null) return 0; // Not enough memory
T.data = x;
T.left = T.right = null;
return 1; // ok
}
/*int insert(int x){
return insertNode(root,x);
}*/
void nhapcay(TNode T){
int x;
Scanner kb = new Scanner(System.in);
while(true){
System.out.print("x= ");
x = kb.nextInt();
if(x==0)
break;
int check=insertNode(T,x);
if (check == -1) System.out.println("Node da ton tai!");
else if (check == 0) System.out.println("Khong du bo nho");
}
}
void nhap(){
nhapcay(root);
}
void xuat(TNode T){
xuat(T.left);
System.out.print(T.data);
xuat(T.right);
}
void duyet(){
xuat(root);
}
public static void main(String[] early){
Demo a = new Demo();
a.nhap();
a.duyet();
}
}
Means that the memory allocation for the Pointers T nhé.
it is allocated memory for node T is the size of the size of struct node size nhé you !!
why not make 1 all born by C eh binary him ..!!
or a combination of such adjustments:
Kids are 1 the optimal article, but do not know as yet optimal.?
=)) Do it. time is limited, I do not write everything.
Evening ..!!
this situation is:
đây là code in ra chỉnh hợp chập k của n, nhìn hơi cùi bắp..^^
và vấn đề là hai vòng FOR () em đã đánh dấu k biết làm cách nào để tối ưu hóa..!
Đã ngưỡng mộ võ công tiền bối lâu nay, mong cao nhân chỉ giáo phương cách, nên dùng cái gì để cải tiến….!!
int k,n;
int a[200];
void ChinhHop ( int j){
int dem =0;
if (j== k){
// for (int i = 0 ; in < to – 1; i ){
// for(int j = i + 1; j < to; j )
if ( the[in] == a[j] )
the ;
}
if (dem == 0 ){
for (int i=0 ; in<to ; i ){
printf ("%d ", the[in]) ;
}
printf("\n");
}
dem=0;
}else{
for (int i=1 ; in<=n ; i ){
the[j]=i;
ChinhHop (j+1);
}
}
}
int main (){
scanf ("%d%d",&to,&n);
ChinhHop ( 0 );
return 0;
}
Hello!
First e thank a share of a about in his blog.
Monday, a can share more about learning methods for programming, ways of learning how to progress is not it? E ask, in programming, What is most important, sir? E not so clever, whether good school to become a programmer is not it? 🙁
Kind 3, f forward a document to share some learning that a supposedly good and useful.
E thanks A A! 🙂
How to learn programming, each person has a way, depending on ability, hobby… often see others code (or see another code) then code again, finish code to run ok then edited into their code by asking yourself the same exercises…
In programming, it also depends. Most importantly, always learning & make new ones. Some areas such as application development, it is necessary to add algorithms.
Not clever how they know when the new school…
Where is her whole google… never read always…
ok
cho em hỏi sao cứ nhập X mãi thế anh… :3
Cái này là nhập đến khi nào nhập số 0 thì dừng lại mà.
🙂 @@@… em mới vào nghề nên không hiểu lắm.. hi… thank anh 😛
e cảm ơn anh vì bài viết rất bổ ích với e. nhưng khi e chạy chương trình của anh, in case Node has both left and right, when you browse the tree to see the error. His answer was no sir help e.
That you try to watch. Test your running ok then give up that.
he asked me , in the number of admissions 0 that right , but I want to have the element in the tree by 0 then how ạ ? thanks you
Then you enter a certain number of any. For example, very large or very small noa. 999999999 such.
lost , I mean for example you : I want the tree in as 2 3 4 0 5 6 7;
then if according to his code, it just browsing 2 3 4 severance sir , hope you help !
Right then because making the entries 0 is stop that. You stop changing conditions is ok
ok , thank you !!!
e asked a dear,,in that a á InsertNode,,now I do not use anymore that use recursive loop format,,a see stars? and ZIP format is how repeat?
This probably is but I have not done ever so well you do not know anymore.
Britain reviewing the deleted node goes he. Assuming the tree as shown. If you remove the node(12) then
P -> node(12);
S-> node(12);
Q-> node(9);
Then P->key = 9;
S->right (node(23)) = Q->left (NULL)
So take node(23) and node(20) r a
Thank you. Mình đã sửa lỗi nhé.
At Node delete function on trees if program just enter the 3 any number (eg: 3, 4, 5) the program will fail.
Thank you. Mình đã sửa lỗi nhé.
Ham remove Node * S = T, *Q = S->Left;
// S la cha cua Q, Q la Node fading of spicy crab P son
while (Q->Right != NULL)
{
S = Q;
Q = Q->Right;
}
P->key = Q->key;
S->Right = Q->Left;
delete Q;
problem. run error
Thank you. Mình đã sửa lỗi nhé.
Her guidance helped me remember this bt
Binary tree search for money each node is an integer
the. Night view of how many trees ghost button its value along with the value of two of its direct seedlings are also prime numbers
T b.tim seedlings largest total ( find a tree you put ) Let's just find the value total
code phần delete key cua anh nếu xét trường hợp cây con bên trái trỏ đến phải là NULL thì bị sai, anh xem có đúng không. Thank anh!
Thank you much for your time. Mình sẽ kiểm tra lại 😉
Hello. I wanted to ask jaw insertNode.
When T == NULL then create node T then assign values T->key, T->left and T->right, but I do not see this command on node node hook else. You can explain not?
When T == NULL ie hollow tree, the tree is empty, simply insert the T values on only because it is the original. Meanwhile the left and the right would figure nothing NULL.
Thank you. You can explain it more clearly is not? Try a specific example such.
Such is the most obvious and you. Initial hollow tree when inserts will insert his original. Boy, children must receive null.
But in my code does not see the connection between this node hook with another node (the two other node NULL) but still runs. You can explain more is not?
Always net initial tree, ie T = NULL. Then insert the first element x1 in it because T = NULL should recognize the value T is x1, T->left = T-right = NULL. The next time the T inserted != NULL then, so depending on the value of x2 next tree inserted L-> bt (if x2 < x1), hay T->right (x2> x1). Meanwhile recall recursion insertNote(T->left) (assuming inserted left), while it considered T-> left is 1 the new T and do so.
Waiter, code tính số nút, the tree height NPTK sir ntn? There are varieties of k a binary tree?
Like Honey. 🙂
Thanks the author,article very rewarding for me!
Ask yourself why declared as part InsertNode:
int insertNode(Tree &T, item x)
which is not:
int insertNode(Tree T, item x)
from 2 there used to be no,2 How differently?
Thank you!
Have &T is to save the value of trees after out function T. if the way 2 then after the tree out function T will remain at not insert items into.
Hello,
In function delKey(Tree &T, item x) {} at line `S->Right = Q->Left;`Mean sir. this time
Q is the right node of the tree fruit, and S is the parent of Q.
Thank you. Mình đã sửa lỗi nhé.
in the code to delete a node, if x ==(The last node of the tree) the last node will point to NULL, but a forgotten memory area freed that place gone.
no way out of the top button 1 not his goods.
there are so many blogs, but her whole self-learning on your blog because writing is easy to understand, receptive
Thank you offline.