004 -- Binary search Tree
Posted ReedyLi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了004 -- Binary search Tree相关的知识,希望对你有一定的参考价值。
000 - search tree
#include <stdio.h>
#include <stdlib.h>
#define datatype int
typedef struct Node{
datatype data;
struct Node *lchild;
struct Node *rchild;
}bintree;
//create tree
bintree* createbinarytree(data)
{
bintree* t = (bintree*)malloc(sizeof(bintree));
if(!t)
{
printf("Not enough space\\n");
return NULL;
}
t->lchild = NULL;
t->rchild = NULL;
t->data = data;
return t;
}
bintree search_tree(bintree t, datatype x){ if(!t) {return NULL;} if(t->data == x) return t; else: { if(!search_tree(t->lchild,x)) {return search_tree(t->rchild,x);} return t; } }
002 -- count the nodes in the tree
int count_tree(bintree t) { if(t) { return (count_tree(t->lchild)+ count_tree(t->rchild)+1); } return 0; }
003 -- compare 2 binary trees
int is_equal(bintree t1, bintree t2) { if(!t1 && !t2) { return 1 ;} // both tree is empty, equal if(t1 && t2 && t1-->data == t2->data) //one tree is empty or there is one data is different , then break { if(is_equal(t1->lchild,t2->lchild)) if(is_equal(t1->rchild, t2->rchild)) { return 1;} } return 0; }
004 - degree check of the tree
int hight_tree(bintree t) { int h, left, right; if(!t) { return 0; } left = high_tree(t->lchild); right = high_tree(t->rchild); h = (left>right ? left: right) +1; return h; }
005- find a element in the binary search tree
bintree* Find(datatype x, bintree t) { while(t) { if(x>t->data) t = t->rchild; else if(x < t->data) t = t->lchild; else return t; } return NULL: }
006 - find the min data in the binary search tree
bintree* FindMin(bintree* t) { if(t) { while(t->lchild) t = t->lchild; } return t; }
// same for the max
bintree* FindMax(bintree* t)
{
if(t)
{
while(t->rchild)
t = t->rchild;
}
return t;
}
007 -- insert a node to the binary search tree
bintree* Insert(bintree* t, datatype x) { if(!t) { t = createbinarytree(x); t->lchild = NULL; t->rchild = NULL; } else { if(x>t->data) t ->rchild = Insert(x, t->rchild); else if t->lchild = Insert(x, t->lchild); } return t; }
008 -- delete a node
BinaryTree* Delete(ElementType x, BinaryTree* BST) { BinaryTree* Temp; if (!BST) printf("\\ndidn\'t find the node to delete%d", x); else if (x < BST->data) BST->leftSubTree = Delete(x, BST->leftSubTree); else if (x > BST->data) BST->rightSubTree = Delete(x, BST->rightSubTree); else { // find the node to be deleted if (BST->leftSubTree && BST->rightSubTree) { //the node has lchild and rchild Temp = FindMin(BST->rightSubTree); //find the min rchild to replace the deleted node BST->data = Temp->data; BST->rightSubTree = Delete(BST->data, BST->rightSubTree); //delete the rchild of the deleted node } else { //the deleted node has 1 or no child node Temp = BST; if (!BST->leftSubTree) //has rchild or no child BST = BST->rightSubTree; else if (!BST->rightSubTree) //has lchild or no child BST = BST->leftSubTree; free(Temp); } } return BST; }
int main(int argc, const char * argv[]) { bintree* t = createbinarytree(18); Insert(10, t); Insert(20, t); Insert(7, t); Insert(15, t); Insert(9, t); Insert(22, t); bintree* findBT1 = Find(15, t); if (findBT1) printf("find1:%d", findBT1->data); bintree* findBT2 = Find(22, root); if (findBT2) printf("\\nfind2:%d", findBT2->data); Delete(10, root); bintree* findBT3 = Find(10, root); if (findBT3) printf("\\nfind3:%d", findBT3->data); bintree* findBT4 = Find(9, root); if (findBT4) printf("\\nfind4:%d", findBT4->data); Delete(9, root); bintree* findBT5 = Find(9, root); if (findBT5) printf("\\find5:%d", findBT5->data); return 0; }
More details:
http://www.cnblogs.com/elaron/archive/2013/04/11/3015155.html
http://www.cnblogs.com/fu11211129/p/4214047.html
以上是关于004 -- Binary search Tree的主要内容,如果未能解决你的问题,请参考以下文章
[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree
Binary Tree和Binary Search Tree
[LeetCode] 173. Binary Search Tree Iterator_Medium_tag: Binary Search Tree
Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree