1. 程式人生 > >建立完全搜尋二叉樹(Complete Binary Search Tree )(c++)

建立完全搜尋二叉樹(Complete Binary Search Tree )(c++)

Complete Binary Search Tree(c++)

因為題目要求,首先輸入二叉樹的結點個數,再輸入每個結點對應的值,建立二叉樹,既是二叉搜尋樹,又是完全二叉樹

整體思路

  1. 根據輸入的結點數,建立完全二叉樹;
  2. 將節點數值放在陣列中,從小到大排序;
  3. 根據根左兒子的結點數(左兒子的值都比根的值小,右兒子的值都比根的值大,所以,例如左兒子有6個結點,則根的值為陣列中第7大的值),選取陣列中對應的值,放入其中,然後更新左兒子陣列,右兒子陣列,遞迴將值放入其中。

程式碼如下

#include <iostream>

#define MAXSIZE 2000

using namespace
std; typedef struct LNode* List; struct LNode { int data; List left; List right; }; List BuideTree(int); //構建具有num個元素的完全二叉樹 int* Sort(int[], int); //將陣列num[]按從小到大排序 List FindFirstNullPosition(List); //順序找到第一個空位置 int FindNumberOfTree(List); //計算樹的結點數
List PutNumInTree(List, int[], int); //將資料放入結點中 int* SortNum(int[], int, int, int); //擷取陣列的一部分 int* LevelorderTraversal(List, int); //層次排列 int main() { int numOfElements, num; cin >> numOfElements; //樹的結點數 int nums[numOfElements]; for (int i = 0;
i < numOfElements; ++i) //將數結點的數值放入陣列 { cin >> num; nums[i] = num; } int* ptrNum, *ptrTest, *numQueue; List tree; tree = BuideTree(numOfElements); //建立完全二叉樹 ptrNum = Sort(nums, numOfElements); //陣列從小到大排序 tree = PutNumInTree(tree,ptrNum,numOfElements); //將有序陣列放入建好的樹 numQueue = LevelorderTraversal(tree, numOfElements); //層次遍歷,將結點資料放入陣列 for (int i = 0; i < numOfElements-1; ++i) //將陣列元素輸出 { cout << numQueue[i] << " "; } cout << numQueue[numOfElements-1] << endl; return 0; } /**構建具有num個元素的完全二叉樹*/ List BuideTree(int numOfElements ) { List ptrInit; if (numOfElements) { List T, temp; T = new LNode //建立第一個節點 T->left = 0; T->right = 0; ptrInit = T; for (int i = 0; i < numOfElements-1; ++i) //建立接下來的 numOfElements-1 個節點 { List T, temp; T = new LNode; T->left = 0; T->right = 0; temp = FindFirstNullPosition(ptrInit); //返回第一個有空子樹的結點地址 if (!temp->left) { temp->left = T; } else if (!temp->right) { temp->right = T; } } } return ptrInit; } /**查詢第一個空位置*/ List FindFirstNullPosition(List tree) { List midTree = tree; //類似層次遍歷的思想 List ptrQueue[MAXSIZE]; int flagIn = -1, flagOut = -1; if (tree) { ++flagIn; //放入第一個 ptrQueue[flagIn] = tree; while (flagIn > flagOut) { ++flagOut; //丟擲一個 midTree = ptrQueue[flagOut]; if (midTree->left && midTree->right) { ++flagIn; //放入左子樹指標 ptrQueue[flagIn] = midTree->left; ++flagIn; //放入右子樹指標 ptrQueue[flagIn] = midTree->right; } else { break; } } return midTree; } else { return tree; } } /**統計樹的結點數*/ int FindNumberOfTree(List tree) { int numOfTree = 0, numOfLeftTree = 0, numOfRightTree = 0; if (tree) { numOfLeftTree = FindNumberOfTree(tree->left); numOfRightTree = FindNumberOfTree(tree->right); numOfTree = numOfLeftTree + numOfRightTree; return numOfTree + 1; } else { return 0; } } /**將陣列從小到大排序*/ int* Sort(int ptrNums[], int numOfElements) { //本次採用氣泡排序 int temp; for (int i = 0; i < numOfElements-1; ++i) { for (int j = 0; j < numOfElements-1-i; ++j) { if (ptrNums[j] > ptrNums[j+1]) { temp = ptrNums[j]; ptrNums[j] = ptrNums[j+1]; ptrNums[j+1] = temp; } } } return ptrNums; } /**取陣列中連續的num個數*/ int* SortNum(int ptrNums[], int numOfElements, int num, int flag) //flag = 1,從0取;flag = 0,從end取 { int *nums; nums = new int[num]; if (flag) { for (int i = 0; i < num; ++i) { nums[i] = ptrNums[i]; } } else { for (int i = 0; i < num ; ++i) { nums[i] = ptrNums[numOfElements-num+i]; } } return nums; } /**將數字放到樹中*/ List PutNumInTree(List tree, int ptrNums[], int numOfElements) { if (tree) { int numOfLeftTree = 0, numOfRightTree = 0; numOfLeftTree = FindNumberOfTree(tree->left); //樹左子樹元素個數 numOfRightTree = FindNumberOfTree(tree->right); //樹的右子樹元素個數 tree->data = ptrNums[numOfLeftTree]; //對當前樹根賦值 int* numOfLeft, *numOfRight; numOfLeft = SortNum(ptrNums, numOfElements, numOfLeftTree, 1); numOfRight = SortNum(ptrNums, numOfElements, numOfRightTree, 0); PutNumInTree(tree->left, numOfLeft, numOfLeftTree); //遞迴左兒子 PutNumInTree(tree->right, numOfRight, numOfRightTree); //遞迴右兒子 } return tree; } /**遍歷輸出*/ int* LevelorderTraversal( List BT , int numOfElements) { int* numQueue, flagOfNum = 0; numQueue = new int[numOfElements]; int sequence[2000] = {0}; List addrSequenc[2000] = {NULL}; int flagOut = 0, flagIn = 0; if(!BT) { return 0; } sequence[flagIn] = BT->data; addrSequenc[flagIn] = BT; ++flagIn; while(flagOut != flagIn) { numQueue[flagOfNum++] = sequence[flagOut]; BT = addrSequenc[flagOut]; ++flagOut; if(BT->left) { sequence[flagIn] = BT->left->data; addrSequenc[flagIn] = BT->left; ++flagIn; } if(BT->right) { sequence[flagIn] = BT->right->data; addrSequenc[flagIn] = BT->right; ++flagIn; } } return numQueue; }

用到的函式
List BuideTree(int); //構建具有num個元素的完全二叉樹
int* Sort(int[], int); //將陣列num[]按從小到大排序
List FindFirstNullPosition(List); //順序找到第一個空位置
int FindNumberOfTree(List); //計算樹的結點數
List PutNumInTree(List, int[], int); //將資料放入結點中
int* SortNum(int[], int, int, int); //擷取陣列的一部分
int* LevelorderTraversal(List, int); //層次排列

List BuideTree(int numOfElements): 構建樹的函式,根據輸入的正整數,建立有numOfElements個結點的完全二叉樹,過程中呼叫了FindFirstNullPosition() ,根據其返回的地址,判斷是左兒子為空還是右兒子為空,注意要首先判斷左兒子,若左兒子為空則不再判斷右兒子,返回二叉樹根的地址;

List FindFirstNullPosition(List):List BuideTree() 呼叫,在建樹過程中,用層次遍歷的方法,返回第一個有空兒子(左兒子或右兒子為NULL)的結點的地址;

int* Sort(int[], int): 因博主水平有限,僅用氣泡排序,將陣列從小到大排列,輸入為陣列首地址和元素個數,返回陣列首地址;

int FindNumberOfTree(List): 輸入樹的根的地址,返回二叉樹中結點的個數,用於PutNumInTree() 函式中,計算左兒子和右兒子的結點數,並根據結點數劃分左右兒子的陣列,以及確定根的值,返回結點個數;

List PutNumInTree(List, int[], int): 將陣列的值放入二叉樹中,輸入為完全二叉樹根的地址,陣列首地址和陣列元素個數,返回即使完全二叉樹又是二叉搜尋樹的二叉樹;

int* SortNum(int[], int, int, int): 將陣列擷取一部分,輸入為陣列首地址,陣列元素個數,要擷取的元素個數,flag標誌位,flag = 1時,陣列開始位置向後複製,flag = 0時,陣列從後向前複製,返回複製後陣列的首地址;

int* LevelorderTraversal(List, int): 層次遍歷函式,根據層次遍歷的方法,將二叉樹資料按順序存入陣列中,輸入為二叉樹根的地址和二叉樹元素個數,層次遍歷順序的陣列。

相關推薦

建立完全搜尋Complete Binary Search Tree c++

Complete Binary Search Tree(c++) 因為題目要求,首先輸入二叉樹的結點個數,再輸入每個結點對應的值,建立二叉樹,既是二叉搜尋樹,又是完全二叉樹。 整體思路 根據輸入的結點數,建立完全二叉樹; 將節點數值放在陣列中,從小到大排序;

根據中序遍歷順序構建完全搜尋-04-6 Complete Binary Search Tree (30分)

題目分析 題目就是給出一棵完全二叉搜尋樹的各個結點的值,然後讓我們輸出該樹層序遍歷的結果。 我們首先可以分析一下,完全二叉搜尋樹 有什麼特點?顯然可以知道: 1.它是一棵完全二叉樹,那麼可以用

04-6 Complete Binary Search Tree 30 分

null pro arc line 結束 his ott hat sin 04-樹6 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a

04-6 Complete Binary Search Tree30 分

題目來源:中國大學MOOC-陳越、何欽銘-資料結構-2018春 作者: 陳越 單位: 浙江大學 問題描述: A Binary Search Tree (BST) is recursively defined as a binary tree whic

c語言 04-6 Complete Binary Search Tree

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains

《資料結構》04-6 Complete Binary Search Tree

題目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a no

04-6 Complete Binary Search Tree (30分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: 二叉搜尋樹(BST)被遞迴地定義為具有以下屬性的二叉樹: T

1064 Complete Binary Search Tree 30 分完全搜尋

題目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contain

第八十六題搜尋建立

如何編寫一個程式,把一個有序整數陣列放到二叉樹中? 分析:為了讓搜尋二叉樹的查詢等操作接近於O(log(n))。我們以有序陣列的中間位置的數字作為搜尋二叉樹的根節點,以其左半部分資料建立搜尋二叉樹作為根節點的左子樹。以其有半部分資料建立搜尋二叉樹作為根節點的右子樹。這是一個遞

Complete Binary Search Tree(完全搜尋)用陣列表示和計算左子的規模

void solve(int ALeft,int ARight,int TRoot) {//初始呼叫為solve(0,N-1,0) n=ARignt-ALeft+1; if(n==0) ret

【PAT】1064. Complete Binary Search Tree (30)【完全搜尋

題目描述 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a

1064 Complete Binary Search Tree 30 分查詢

  中序遍歷建樹 #include<bits/stdc++.h> using namespace std; const int N=1e3+10; int s[N]; int n; int tree[N]; int cnt; void inorder(int root)

pat1064--搜尋+滿

1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper

一個簡單的英語到漢語的翻譯過程搜尋

英語到漢語的翻譯: 原理:在我看來,既可以運用陣列,也可以運用搜索二叉樹,或者是其它任意一種儲存結構應該都可以完成這個操作,但應用搜索二叉樹,效率會更高,因為每一次搜尋,就會排除許多無用的資料。      例如,在上圖中查詢1,只需要通過上圖的3次比較就可以得出結果,每

判斷一棵是否是搜尋 判斷一棵是否是完全

package class_04; import java.util.LinkedList; import java.util.Queue; /** * * 判斷一棵樹是否是搜尋二叉樹 * 判斷一棵樹是否是完全二叉樹 * */ public class Code

LeetCode刷題-98——Validate Binary Search Tree驗證搜尋

連結:題目:給定一個二叉樹,判斷其是否是一個有效的二叉搜尋樹。一個二叉搜尋樹具有如下特徵:節點的左子樹只包含小於當前節點的數。節點的右子樹只包含大於當前節點的數。所有左子樹和右子樹自身必須也是二叉搜尋樹

搜尋完全

搜尋二叉樹又叫作二叉樹查詢樹或者二叉排序樹, 所謂搜尋二叉樹是指對於任何一個結點,它的左子樹的所有結點都比這個根結點要小,它的右子樹的所有結點都比這個根結點要大。注意是根結點與左右子樹上所有的結點進行比較而不是僅僅與左右孩子結點進行比較,因此根據這個定義,那麼當按照中序

遞迴先序、非遞迴層次建立並用三序遍歷之C語言

 先序就是直接用遞迴的方法建立,層次使用了輔助陣列,後一種方法我覺得友好多了。 #include "stdio.h" #define MAXSIZE 50 #define TRUE 1 #define FALSE 0 typedef int boo

1064 Complete Binary Search Tree 30 分中序轉化成層序】

1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propert

年會 記憶化搜尋+思想-------------------------------------C語言——菜鳥級

時間限制: 1Sec 記憶體限制: 128MB 提交: 54 解決: 24 題目描述 背景 某大學校長準備開一次年會. 該校的員工具有等級結構, 即師生關係構成一棵樹, 以校長為樹根. 員工號是