數據結構(五)層次遍歷

數據結構(五)層次遍歷

// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType BiTree
using namespace std;
typedef struct BiTNode
{char data;struct BiTNode* lchild, * rchild;
}BitTNode, * BiTree;
typedef struct  linknode //鏈式節點
{ElemType data;struct linknode* next;}LinkNode;//鏈式隊列
typedef struct
{LinkNode* front, *rear;}LinkQueue;void InitQueue(LinkQueue &Q)
{//帶頭節點的隊列初始化Q.rear=Q.front=(LinkNode*)malloc(sizeof(LinkNode));Q.front->next = NULL;
}bool IsEmpty(LinkQueue& Q)
{if (Q.rear == Q.front){return true;}return false;}void EnQueue(LinkQueue& Q, ElemType x)
{LinkNode* s= (LinkNode*)malloc(sizeof(LinkNode));s->data = x;s->next = Q.rear->next;Q.rear->next = s;Q.rear = s;}bool DeQueue(LinkQueue& Q, ElemType &x)
{if (IsEmpty(Q)){//隊列為空return false;}LinkNode* p = Q.front->next;x = p->data;Q.front->next = p->next;if (p == Q.rear)  //要刪除的為尾隊列{Q.rear = Q.front;}free(p);return true;
}//層次遍歷
void LevelOrder(BiTree T)
{LinkQueue q;BiTNode* p;//初始化隊列InitQueue(q);EnQueue(q,T);  //將根節點入隊while (!IsEmpty(q)){DeQueue(q,p);printf("%c\t",p->data);if (p->lchild != NULL){EnQueue(q,p->lchild);}if (p->rchild != NULL){EnQueue(q, p->rchild);}}}
bool createBiTree(BiTree& T)
{char ch;cin >> ch;if (ch == '.'){T = NULL; //如果輸入 '.' , 該樹空結點}else{T = (BitTNode*)malloc(sizeof(BitTNode));if (T == NULL){printf("tree error!\n");exit(1);}T->data = ch;createBiTree(T->lchild);createBiTree(T->rchild);}return true;
}int main()
{//int x;//LinkQueue Q;//InitQueue(Q);//EnQueue(Q, 5);//EnQueue(Q, 7);//EnQueue(Q, 9);//DeQueue(Q, x);//LinkNode* p = Q.front->next;//while (p != NULL)//{//    printf("%d\n",p->data);//    p = p->next;//}BiTree T;createBiTree(T);LevelOrder(T);}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

測試要完成如圖
在這里插入圖片描述

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384369.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384369.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384369.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++成員函數指針的應用

轉載&#xff1a;http://www.cppblog.com/colys/archive/2009/08/18/25785.html C中&#xff0c;成員指針是最為復雜的語法結構。但在事件驅動和多線程應用中被廣泛用于調用回叫函數。在多線程應用中&#xff0c;每個線程都通過指向成員函數的指針來調用該函數。在這樣的應用中…

cv2.VideoCapture()無法打開視頻解決方法

cv2.VideoCapture無法打開視頻解決方法問題解決方法問題 cv2.VideoCapture打開mp4文件&#xff0c;直接報錯 解決方法 我們打開D:\opencv_3.4.2_Qt\opencv_3.4.2_Qt\x86\bin\&#xff08;opencv的dll動態庫中找到&#xff09; 找到opencv_ffmpeg342.dll文件&#xff0c;放入…

函數指針指向類的靜態成員函數

轉載&#xff1a;http://www.cnblogs.com/dongyanxia1000/p/4906592.html 1. 代碼 1 #include<iostream>2 #include<stdio.h>3 using namespace std;4 class Point5 {6 public:7 Point(int x0,int y0):x(x),y(y)8 { 9 count; 10 } 11 P…

Qt+OpenCV打開視頻文件并在窗口界面上顯示

QtOpenCV打開視頻文件并在窗口界面上顯示1、新建一個Qt Widgets Application&#xff0c;工程配置文件&#xff08;.pro文件&#xff09;內容如下&#xff1a;#------------------------------------------------- # # Project created by QtCreator 2021-03-19T09:06:07 # #--…

OpenCV Mat的數據類型

OpenCV Mat的數據類型Mattype類型內存拷貝簡單實現Mat Mat類(Matrix的縮寫)是OpenCV用于處理圖像而引入的-一個封裝類。他是一個自動內存管理工具。 Mat:本質上是由兩個數據部分組成的類:(包含信息有矩陣的大小&#xff0c;用于存儲的方法&#xff0c;矩陣存儲的地址等)矩陣頭…

C++指向成員函數的指針

轉載&#xff1a;http://www.cnblogs.com/tracylee/archive/2012/11/15/2772176.html C指向函數的指針定義方式為&#xff1a; 返回類型 &#xff08;*指針名&#xff09;&#xff08;函數參數列表&#xff09;&#xff0c;例如 void &#xff08;*p&#xff09;&#xff08;in…

OpenCV基礎知識 圖像

OpenCV基礎知識 圖像位圖模式灰度模式RGB模式位圖模式 位圖模式是是1位深度的圖像&#xff0c;只有黑和白兩種顏色。它可以由掃描或置入黑色的矢量線條圖像生成&#xff0c;也能由灰度模式轉換而成。其他圖像模式不能直接轉換為位圖模式。 灰度模式 灰度模式是8位的圖像&…

C++中引用與指針的區別(詳細介紹)

轉載&#xff1a;http://www.cnblogs.com/tracylee/archive/2012/12/04/2801519.html C&#xff0b;&#xff0b;中的引用與指針的區別指向不同類型的指針的區別在于指針類型可以知道編譯器解釋某個特定地址&#xff08;指針指向的地址&#xff09;中的內存內容及大小&#xff…

mysql遠程連接權限grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘123456‘ with grant option語句報錯

mysql遠程連接權限grant all privileges on *.* to ‘root‘‘%‘ identified by ‘123456‘ with grant option語句報錯記錄一下自己安裝mysql遇到的小坑grant all privileges on *.* to ‘root‘‘%‘ identified by ‘123456‘ with grant option只適用于mysql8.0之前的版本…

C++虛繼承的概念

轉載&#xff1a;http://blog.csdn.net/crystal_avast/article/details/7678704 C中虛擬繼承的概念 為了解決從不同途徑繼承來的同名的數據成員在內存中有不同的拷貝造成數據不一致問題&#xff0c;將共同基類設置為虛基類。這時從不同的路徑繼承過來的同名數據成員在內存中就只…

數組名和取數組名的區別

先來個簡單的小案例 #include <stdio.h> #include <iostream>using namespace std;int main() {int a[10] { 0 };printf("%d\n", a);printf("%d\n", &a);printf("%d\n", a1);printf("%d\n", &a1);printf("…

C++繼承詳解三 ----菱形繼承、虛繼承

轉載&#xff1a;http://blog.csdn.net/pg_dog/article/details/70175488 今天呢&#xff0c;我們來講講菱形繼承與虛繼承。這兩者的講解是分不開的&#xff0c;要想深入了解菱形繼承&#xff0c;你是繞不開虛繼承這一點的。它倆有著什么關系呢&#xff1f;值得我們來剖析。 菱…

Linux :IO多路復用模型

轉載&#xff1a;http://blog.csdn.net/mr253727942/article/details/50827127 一、IO多路復用定義 IO多路復用允許應用在多個文件描述符上阻塞&#xff0c;并在某一個可以讀寫時通知&#xff0c; 一般遵循下面的設計原則&#xff1a;、 IO多路復用&#xff1a;任何文件描述符…

leetcode(一)刷題兩數之和

給定一個整數數組 nums 和一個整數目標值 target&#xff0c;請你在該數組中找出 和為目標值 target 的那 兩個 整數&#xff0c;并返回它們的數組下標。 示例 1&#xff1a; 輸入&#xff1a;nums [2,7,11,15], target 9 輸出&#xff1a;[0,1] 解釋&#xff1a;因為 nums[…

Linux并發服務器編程之多線程并發服務器

轉載&#xff1a;http://blog.csdn.net/qq_29227939/article/details/53782198 上一篇文章使用fork函數實現了多進程并發服務器&#xff0c;但是也提到了一些問題&#xff1a; fork是昂貴的。fork時需要復制父進程的所有資源&#xff0c;包括內存映象、描述字等&#xff1b;目…

leetcode(二)二分法查找算法

給定一個 n 個元素有序的&#xff08;升序&#xff09;整型數組 nums 和一個目標值 target &#xff0c;寫一個函數搜索 nums 中的 target&#xff0c;如果目標值存在返回下標&#xff0c;否則返回 -1。 示例 1: 輸入: nums [-1,0,3,5,9,12], target 9 輸出: 4 解釋: 9 出現在…

leetcode(977)有序數組的平方

給你一個按 非遞減順序 排序的整數數組 nums&#xff0c;返回 每個數字的平方 組成的新數組&#xff0c;要求也按 非遞減順序 排序。 示例 1&#xff1a; 輸入&#xff1a;nums [-4,-1,0,3,10] 輸出&#xff1a;[0,1,9,16,100] 解釋&#xff1a;平方后&#xff0c;數組變為 […

IO多路復用之select全面總結(必看篇)

轉載&#xff1a;http://www.jb51.net/article/101057.htm 1、基本概念 IO多路復用是指內核一旦發現進程指定的一個或者多個IO條件準備讀取&#xff0c;它就通知該進程。IO多路復用適用如下場合&#xff1a; &#xff08;1&#xff09;當客戶處理多個描述字時&#xff08;一般…

leetcode(189) 旋轉數組

**給定一個數組&#xff0c;將數組中的元素向右移動 k 個位置&#xff0c;其中 k 是非負數。 進階&#xff1a; 盡可能想出更多的解決方案&#xff0c;至少有三種不同的方法可以解決這個問題。 你可以使用空間復雜度為 O(1) 的 原地 算法解決這個問題嗎&#xff1f; 示例 1: …

I/O 多路復用之select

轉載&#xff1a;http://blog.csdn.net/u012432778/article/details/47347133 概述 Linux提供了三種 I/O 多路復用方案&#xff1a;select&#xff0c;poll和epoll。在這一篇博客里先討論select, poll 在將下一篇中介紹&#xff0c;epoll是Linux特有的高級解決方案&#xff0c;…