zoukankan      html  css  js  c++  java
  • 弄棵树玩玩(二叉树尝试)

    #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;	
    }
    
  • 相关阅读:
    HDU 5710 digit sum
    Fibonacci Tree HDU
    2016CCPC东北赛补题
    紫书水题
    20180415校赛
    随心所欲小游戏
    Alpha版本测试报告
    第四天作业
    第二天作业
    第一天作业
  • 原文地址:https://www.cnblogs.com/lovelyxia/p/1945748.html
Copyright © 2011-2022 走看看