轉自:http://blog.csdn.net/dazhong159/article/details/7862774
1、二叉樹的數據結構(數據域+指向左/右孩子節點的指針)
typedef struct tree
{
int data;
struct tree *left;
struct tree *right;
}Tree,*pTree;
2、創建二叉查找樹
- //創建二叉樹(二叉查找樹),返回根節點
- pTree createTree(int *data,int len)
- {
- if (len<1)
- {
- cout<<"創建的樹爲空樹!"<<endl;
- return NULL;
- }
- pTree root=(pTree)malloc(sizeof(Tree));
- root->data=data[0];
- root->left=NULL;
- root->right=NULL;
- pTree current,back;
- for (int i=1;i<len;i++)
- {
- pTree newNode=(pTree)malloc(sizeof(Tree));
- newNode->data=data[i];
- newNode->left=NULL;
- newNode->right=NULL;
- current=root;//每次插入新的節點時,current都指向root
- while(current!=NULL)
- {
- back=current;
- if (current->data>data[i])
- current=current->left;
- else
- current=current->right;
- }
- if(back->data>data[i])
- back->left=newNode;
- else
- back->right=newNode;
- }
- return root;
- }
3、二叉樹節點個數(遞歸法)
- //遞歸求樹的節點個數(左子樹節點個數+右子樹節點個數+根節點個數)
- int getNode(pTree root)
- {
- if (root==NULL)
- return 0;
- else if (root->left==NULL&&root->right==NULL)
- return 1;
- else
- return getNode(root->left)+getNode(root->right)+1;
- }
4、二叉樹的高度(遞歸法)
- //遞歸求樹的高度(左子樹和右子樹高度中的較大的值+根節點高度1)
- int getHeight(pTree root)
- {
- if (root==NULL)
- return 0;
- int lh=getHeight(root->left);
- int rh=getHeight(root->right);
- return lh>rh ? lh+1:rh+1;
- }
5、遞歸遍歷二叉樹
- //遞歸進行前序遍歷
- void preTraverse(pTree root)
- {
- if (root!=NULL)
- {
- cout<<root->data<<" ";
- preTraverse(root->left);
- preTraverse(root->right);
- }
- }
- //遞歸進行中序遍歷
- void midTraverse(pTree root)
- {
- if (root!=NULL)
- {
- midTraverse(root->left);
- cout<<root->data<<" ";
- midTraverse(root->right);
- }
- }
- //遞歸進行後序遍歷
- void postTraverse(pTree root)
- {
- if (root!=NULL)
- {
- postTraverse(root->left);
- postTraverse(root->right);
- cout<<root->data<<" ";
- }
- }
6、非遞歸遍歷二叉樹
- /**************************************************************************
- * 利用棧進行前序遍歷
- * 1、從根節點開始,遍歷左子樹,輸出父節點的內容,並將父節點入棧(這裏用數組代替)
- * 2、遍歷遇到節點爲NULL時,取出棧頂元素,得到其右孩子節點
- * 3、右孩子節點不爲空時,此時將該右節點看作是根節點,重複1、2操作
- * 4、右孩子節點爲空時,繼續出棧,得到右孩子節點,重複3、4操作
- **************************************************************************/
- void prePrint(pTree root)
- {
- pTree p=root;
- int height=getHeight(root);
- pTree *sk=new pTree[height+1];
- int cnt=0;
- while(p!=NULL||cnt!=0)
- {
- if (p!=NULL)
- {
- cout<<p->data<<" ";
- sk[cnt++]=p;
- p=p->left;
- }
- else
- {
- p=sk[--cnt];
- p=p->right;
- }
- }
- delete []sk;
- }
- /****************************************************************************
- * 利用棧進行中序遍歷
- * 1、從根節點開始,遍歷左子樹,將父節點入棧(這裏用數組代替)
- * 2、遍歷遇到節點爲NULL時,取出棧頂元素,輸出節點內容,得到其右孩子節點
- * 3、右孩子節點不爲空時,此時將該右節點看作是根節點,重複1、2操作
- * 4、右孩子節點爲空時,繼續出棧,重複3、4操作
- ****************************************************************************/
- void midPrint(pTree root)
- {
- pTree p=root;
- int height=getHeight(root);
- pTree *sk=new pTree[height+1];
- int cnt=0;
- while(p!=NULL||cnt!=0)
- {
- if (p!=NULL)
- {
- sk[cnt++]=p;
- p=p->left;
- }
- else
- {
- p=sk[--cnt];
- cout<<p->data<<" ";
- p=p->right;
- }
- }
- delete []sk;
- }
- /**********************************************************************
- * 利用棧後序遍歷
- * 1、首先創建一種新的數據結構,包含原來的數據結構和isFirst標誌()
- * 2、從根節點開始,入棧,將isFirst置爲1,訪問左子樹
- * 3、遇到節點爲NULL時,如果isFirst==1,訪問棧頂元素,得到該節點的右孩子
- * 4、該節點的右孩子爲NULL時,出棧,訪問該節點,然後將該節點置爲NULL,使下次循環必定會進入第3步
- * 5、該節點的右孩子不爲NULL時,將該右孩子當作根節點,重複2、3
- **********************************************************************/
- void postPrint(pTree root)
- {
- int height=getHeight(root);
- struct TPostTree //節點新的數據結構
- {
- pTree node;
- int isFirst;
- };
- TPostTree *sk=new TPostTree[height+1];
- int cnt=0;
- pTree p=root;
- while(p!=NULL||cnt!=0)
- {
- if (p!=NULL) //左
- {
- sk[++cnt].node=p;
- sk[cnt].isFirst=1;
- p=p->left;
- }
- else if (sk[cnt].isFirst) //右
- {
- sk[cnt].isFirst=0;
- p=sk[cnt].node->right; //根據sk保存的找到右節點
- }
- else //根節點
- {
- p=sk[cnt--].node;
- cout<<p->data<<" ";
- p=NULL; //保證下次循環必定進入if的else分支
- }
- }
- delete []sk;
- }
7、按層遍歷二叉樹
- /***************************************************************************
- * 按層遍歷
- * 1、訪問根節點,將根節點進隊
- * 2、取出隊列的第一個元素,訪問該元素,並將取出的該元素節點的左右孩子節點(不爲NULL)進隊
- * 3、循環進行第二步,直至隊列爲空
- * 4、下面的代碼用數組替換了隊列
- ***************************************************************************/
- void BTraverse(pTree root)
- {
- int n=getNode(root);
- pTree *sk=new pTree[n],p;
- int front=0,rear=0;
- sk[front]=root;
- while(front!=n)
- {
- p=sk[front];
- cout<<p->data<<" ";
- if (p->left!=NULL)
- {
- sk[rear+1]=p->left;
- rear++;
- }
- if (p->right!=NULL)
- {
- sk[rear+1]=p->right;
- rear++;
- }
- front++;
- }
- cout<<endl;
- delete []sk;
- }