分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
/*二叉搜索树的建树方法。简单的递归结构。关于树的算法设计一定要联想到递归,因为树本身就是递归定义的。(递归的缺点:递归可能会导致栈的溢出)*/#include <iostream>#define NULL 0using namespace std;template<class T>struct BTNode{ T data; BTNode<T> *lChild, *rChild; BTNode(); BTNode(const T &val, BTNode<T> *childL = NULL, BTNode<T> *childR = NULL) { data = val; lChild = childL; rChild = childR; } BTNode<T>* CopyTree() { BTNode<T> *nl, *nr, *nn; if(&data == NULL) { return NULL; } nl = lChild->CopyTree(); nr = rChild->CopyTree(); nn = new BTNode<T>(data, nl, nr); return nn; }};template<class T>BTNode<T>::BTNode(){ lChild = rChild = NULL;}template<class T>void insertNode(BTNode<T> **r, T val){ BTNode<T>* newNode = (BTNode<T>*)malloc(sizeof(BTNode<T>)); newNode->data = val; newNode->lChild = NULL; newNode->rChild = NULL; if(!(*r)) { *r = newNode; } else if(newNode->data < (*r)->data) { insertNode(&(*r)->lChild, val); } else { insertNode(&(*r)->rChild, val); }}void main(){ int c[7] = {3, 1, 2, 6, 4, 5}; BTNode<int> *b = NULL; for(int i = 0; i < 6; i++) { insertNode(&b, c[i]); }}// Output:/**/
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e19ffb3303a490d20ea5d00dd397156.gif)