二叉搜索樹的插入

【分析】:關鍵是找到元素應該插入的位置,能夠採用與Find相似的方法。spa

 1 BinTree Insert(ElementType x,BinTree BST)
 2 {
 3   if(!BST)
 4   {
 5     BST=malloc(sizeof(struct TreeNode));
 6     BST->Data=x;
 7     BST->Left=BST-Right=NULL;
 8   }
 9   else
10   {
11     if(x>BST->Data)
12        BST->Right=Insert(x,BST->Right);
13     else if(x<BST->Data)
14       BST->Left=Insert(x,BST->Left);
15    }
16    return BST;
17 }
相關文章
相關標籤/搜索