<數(shù)據(jù)結(jié)構(gòu)>筆記1-【B樹和索引】

一、二叉排序樹(二叉查找樹)

空樹或具有下列性質(zhì)的二叉樹
1.左子樹所有節(jié)點(diǎn)值小于根節(jié)點(diǎn)
2.右子樹所有節(jié)點(diǎn)大于根節(jié)點(diǎn)
3.它的左右子樹分別為二叉排序樹

二、平衡二叉樹(AVL樹)

  • 滿足二叉排序樹
  • 左右子樹高度相差不超過1

三、B-樹

  • 平衡
  • 多路排序樹
  • 主要用于文件索引


    B-樹

1. 特性:

1)所有非終端節(jié)點(diǎn)包含以下信息(key-value paris)

(n,A0,K1,A1,K2,A2...Kn,An) **
Ki--關(guān)鍵字
Ai--指向子樹根節(jié)點(diǎn)指針
n--關(guān)鍵字個數(shù)

2)所有葉節(jié)點(diǎn)出現(xiàn)在同一層,包含關(guān)鍵字 或 指向關(guān)鍵字記錄的指針

關(guān)鍵字記錄?

關(guān)鍵字key為記錄的主鍵,只是記錄的一部分。

wikipedia for B-tree

The term leaf is also inconsistent.
Bayer & McCreight (1972) considered the leaf level to be the lowest level of keys, but Knuth considered the leaf level to be one level below the lowest keys (Folk & Zoellick 1992, p. 363).
There are many possible implementation choices.
In some designs, the leaves may hold the entire data record;
in other designs, the leaves may only hold pointers to the data record. Those choices are not fundamental to the idea of a B-tree.[5]

《數(shù)據(jù)結(jié)構(gòu)》嚴(yán)蔚敏版 此處有誤

3)樹中每個節(jié)點(diǎn)保存值

B-trees keep values in every node in the tree, and may use the same structure for all nodes.

2. B-樹查找分析

通常存儲在磁盤

兩步操作:

1) 找節(jié)點(diǎn)(磁盤)

磁盤隨機(jī)找到p所指節(jié)點(diǎn),并將節(jié)點(diǎn)信息讀入內(nèi)存

2) 節(jié)點(diǎn)中找關(guān)鍵字(內(nèi)存)

順序查找或折半查找關(guān)鍵字

四、B+樹

A B+ tree can be viewed as a B-tree in which each node contains only keys (not key–value pairs), and to which an additional level is added at the bottom with linked leaves.

note

12..34..567 are leaves,d1~d7 are not——they are data record.

A simple B+ tree example linking the keys 1–7 to data values d1-d7
A simple B+ tree example linking the keys 1–7 to data values d1-d7

與B-樹不同點(diǎn):

1. 所有葉子節(jié)點(diǎn)

1). 包含 全部關(guān)鍵字
2). 包含關(guān)鍵字記錄指針,如上圖12..34..567
3). 葉子節(jié)點(diǎn)升序順序鏈表鏈接

@Wikipedia-Btree

2. 非終端節(jié)點(diǎn)(即索引)只含關(guān)鍵字,非B樹的關(guān)鍵字-指針對
3. 其他
  • n棵子樹含n個關(guān)鍵字
  • 兩指針與查找:
  • root節(jié)點(diǎn)指針-->隨機(jī)查找(必須,搜索總是從root節(jié)點(diǎn)開始,代替全表掃描)
  • 最小關(guān)鍵字葉子節(jié)點(diǎn)指針-->順序查找

五、文件

1. 記錄2種結(jié)構(gòu)

邏輯結(jié)構(gòu)和物理結(jié)構(gòu)


2. 文件3種檢索方式

順序檢索

存取下一個邏輯記錄

直接檢索

存取第i個邏輯記錄

關(guān)鍵字檢索

查詢與關(guān)鍵字相關(guān)記錄

六、 索引文件

1. 索引表

記錄邏輯記錄物理記錄對應(yīng)關(guān)系

索引項(xiàng)
  • 定義:索引表中的項(xiàng)
  • 索引項(xiàng)關(guān)鍵字或邏輯記錄號排序

2. 索引文件

索引文件只能是==磁盤文件==

1)定義:

文件數(shù)據(jù)區(qū)+索引表

2)分類:

索引順序文件--數(shù)據(jù)區(qū)有序
索引非順序文件--數(shù)據(jù)區(qū)無序

3)索引文件檢索

兩步驟:

  1. 索引表(折半)
  2. 查記錄(依據(jù)索引項(xiàng))
  • 數(shù)據(jù)小--索引表在內(nèi)存,記錄在外存
  • 數(shù)據(jù)大--索引、記錄均在外存
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容