c++二叉搜索树

1 typedef struct Node *Tree;
2 struct Node
3 {
4     int data;
5     Tree left, right;
6 };

插入:

 1 int main()
 2 {
 3     cin >> N;
 4     Tree root = NULL;
 5     for (j = 0; j < N; j++)
 6     {
 7         cin >> t;
 8         root = insert(root, t);
 9     }
10 }
11 
12 Tree insert(Tree T, int data)
13 {
14     if (T == NULL)
15     {
16         T = new Node;
17         T->data = data;
18         T->left = T->right = NULL;
19     }
20     else if (data < T->data)
21         T->left = insert(T->left, data);
22     else
23         T->right = insert(T->right, data);
24     return T;
25 }

猜你喜欢

转载自www.cnblogs.com/lxc1910/p/9506968.html