Exemple #1
0
void InsertToTree(Node** node, int x) {
    if(*node == NULL) *node = new Node(x);

    if(x < (*node)->data) {
        InsertToTree( &((*node)->left), x );
    } else if ((*node)->data < x) {
        InsertToTree( &((*node)->right), x );
    }
}
Exemple #2
0
//二叉搜索树的插入
SearchTree InsertToTree(ElementType x, SearchTree tree) {
	if (tree == NULL) {
		tree = (struct TreeNode*)malloc(sizeof(struct TreeNode));
		if (tree == NULL) {
			cout << "malloc failed" << endl;
		}
		else {
			tree->element = x;
			tree->left = NULL;
			tree->right = NULL;
		}
	}
	else if (x < tree->element)
		tree->left = InsertToTree(x, tree->left);
	else if (x > tree->element)
		tree->right = InsertToTree(x, tree->right);
	return tree;
}
Exemple #3
0
//二叉搜索树的创建
SearchTree CreateSearchTree(SearchTree tree) {
	int tree_element;
	while (cin >> tree_element) {
		if (tree_element == '*')
			break;
		else
			tree = InsertToTree(tree_element, tree);
	}
	return tree;
}