#include<iostream>
using namespace std;

struct Node{
	int data;
	Node *left;
	Node *right;	
	Node(int data, Node *left, Node *right):
		data(data), left(left), right(right){}
};

void Insert(Node *root, int data){
	if(data >= root->data){
		if(root->right == NULL){
			root->right = new Node(data, NULL, NULL);
		}
		else
			Insert(root->right, data);
	}
	else{
		if(root->left == NULL){
			root->left = new Node(data, NULL, NULL);
		}
		else
			Insert(root->left, data);	
	}
}
void print(Node *root){
	if(root != NULL)
  		cout<<root->data<<endl;
	if(root->left != NULL)
		print(root->left);
	if(root->right != NULL)
		print(root->right);	
}

int main(){
	Node *root = new Node(5, NULL, NULL);
	Insert(root, 8);
	Insert(root, 6);
	Insert(root, 9);
	Insert(root, 7);
	Insert(root, 10);
	Insert(root, 3);
	Insert(root, 2);
	Insert(root, 4);
	Insert(root, 1);
	print(root);
	return 0;	
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-10-20
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
猜你喜欢
  • 2021-06-18
  • 2021-12-07
  • 2022-12-23
  • 2021-08-22
  • 2021-06-19
  • 2021-10-30
  • 2022-12-23
相关资源
相似解决方案