二叉查找树的插入节点/打印
#include<iostream> using namespace std; struct node { int data; node *left; node *right; }; //(1)通过插入节点构建二叉查找树 node *insert_node(node *root,int num) { if(root==NULL) { root=new node; root->left=NULL; root->right=NULL; root->data=num; } else { if(num<root->data) { root->left=insert_node(root->left,num); } else root->right=insert_node(root->right,num); } return root; } //(2) 打印二叉查找树 void print_tree(node *root) { if(root->left==NULL) { if(root->right==NULL) cout<<root->data<<endl; else { cout<<root->data<<endl; print_tree(root->right); } } else { print_tree(root->left); cout<<root->data<<endl; if(root->right!=NULL) print_tree(root->right); } } int main() { node *tree; tree=NULL; cout<<"input datas:"<<endl; int x;char c; while(cin>>x) { tree=insert_node(tree,x); cin.get(c); if(c==' ')break; } print_tree(tree); return 0; }