广度优先遍历二叉树
2012-05-05 21:21
274 查看
//3、广度优先遍历二叉树。 #include <stdio.h> #include <string.h> #include <stdlib.h> //int i =0; typedef struct treenode /*树节点结构体定义*/ { treenode(char* name) { strcpy(this->data,name); lchild=NULL; rchild=NULL; } treenode* InsertSbOnMyLeft(treenode* Sb) { this->lchild=Sb; return Sb; } treenode* InsertSbOnMyRight(treenode* Sb) { this->rchild=Sb; return Sb; } char data[256]; struct treenode *lchild; struct treenode *rchild; }node; typedef struct queuenode /*队列节点结构体定义*/ { node* info; struct queuenode *next; }qnode; typedef struct /*将头尾指针封装在一起*/ { qnode *front; qnode *rear; }lqueue; lqueue * initlqueue()// (lqueue *q) /*初始化一个空队列*/ { lqueue *q; q = new lqueue(); if (q) { q -> front = 0; q -> rear = 0; } return q; } void destroylqueue (lqueue *q) /*销毁队列*/ { qnode *p; if(q) { while (q -> front) { p = q -> front; q -> front = q -> front -> next; delete p; }//while (q -> front) delete q; }//if (q) //q = 0; } int isempty (lqueue *q) /*判断队列是否为空*/ { return q -> front == 0; } void enqueue (lqueue *q, node* el) /*入队列(尾插入)*/ { qnode *p; p = new qnode(); if(!p) { printf ( "内存已满! "); exit (0); } p -> info = el; p -> next = 0; if (!isempty(q)) { q-> rear -> next = p; //插入链表尾,作为新的尾 q -> rear =p; // q -> rear -> next; } else q-> front = q -> rear = p; } node* dequeue (lqueue *q) /*出队列(头删除)*/ { node *el = q -> front -> info; qnode *temp = q -> front; if (q -> front == q -> rear) q -> front = q -> rear = 0; else q -> front = q -> front -> next;//头节点往后推 delete temp;//释放出队的节点qnode return el;//返回出队的节点qnode的节点node } void breadthfirst (node *root) /*广度优先遍历*/ { lqueue *q; q = initlqueue();//; (q); node *p = root; if (p) { enqueue (q,p);//只插入了一个 node *p = root 到lqueue *q while (!isempty (q)) { p = dequeue (q);//刚插入一个node就让让它出队列了 printf ( "%s ", p -> data); //下面将左右孩子压入队列 if (p -> lchild != 0) enqueue (q, p -> lchild);/*左孩子入队列*/ if (p -> rchild != 0) enqueue (q, p -> rchild);/*右孩子入队列*/ }//while (!isempty (q)) printf ( "\n "); }//if (p) else printf ( "这是一颗空树!\n "); destroylqueue (q); } void main() { node *A=NULL; A=new node("A"); node * B1=A->InsertSbOnMyLeft(new node("B1")); node * B2=A->InsertSbOnMyRight(new node("B2")); node * C1=B1->InsertSbOnMyLeft(new node("C1")); node * C2=B1->InsertSbOnMyRight(new node("C2")); node * C3=B2->InsertSbOnMyLeft(new node("C3")); node * C4=B2->InsertSbOnMyRight(new node("C4")); node * D1=C1->InsertSbOnMyLeft(new node("D1")); node * D2=C1->InsertSbOnMyRight(new node("D2")); node * D3=C2->InsertSbOnMyLeft(new node("D3")); node * D4=C2->InsertSbOnMyRight(new node("D4")); node * D5=C3->InsertSbOnMyLeft(new node("D5")); node * D6=C3->InsertSbOnMyRight(new node("D6")); node * D7=C4->InsertSbOnMyLeft(new node("D7")); node * D8=C4->InsertSbOnMyRight(new node("D8")); breadthfirst(A); } /* 二叉搜索树的广度优先遍历 A B1 B2 C1 C2 C3 C4 D1 D2 D3 D4 D5 D6 D7 D8 Press any key to continue //*/
相关文章推荐
- java实现二叉树的深度优先遍历和广度优先遍历
- 二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式 二叉树的遍历方式: 1、深度优先:递归,非递归实现方式 1)先序遍历:先访问根节点,再依次访问左子树和右子树 2)中序遍
- 二叉树的深度优先和广度优先遍历
- 二叉树深度优先遍历和广度优先遍历【C++】
- 【算法导论】二叉树的广度优先遍历
- 二叉树的深度优先遍历(栈)和广度优先遍历(队列)
- 二叉树的深度优先和广度优先遍历
- 二叉树 广度优先遍历
- 第五讲 树-1(广度优先遍历二叉树&深度优先遍历二叉树)
- 二叉树的深度优先遍历、广度优先遍历和非递归遍历
- 【剑指 offer】(二十三)—— 从上往下打印二叉树(或曰层次遍历、广度优先遍历)
- 二叉树的广度优先遍历及完全二叉树判定算法 3ff0
- 【算法导论】二叉树的广度优先遍历
- 二叉树的广度优先遍历
- 二叉树的广度优先遍历、深度优先遍历的递归和非递归实现方式
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
- C++ 二叉树深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历、广度优先遍历和非递归遍历
- 转:二叉树的深度优先遍历和广度优先遍历
- 二叉树的递归,非递归遍历,深度优先遍历,广度优先遍历