chapter 11 multiway trees. content points orchards, trees, and binary trees traverse of orchards...

74
Chapter 11 Multiway Tree s

Upload: daniela-hunter

Post on 13-Jan-2016

266 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Chapter 11 Multiway Trees

Page 2: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Content Points

Orchards, Trees, and Binary Trees

Traverse of Orchards and trees

Huffman trees

Page 3: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

On the classification of species

A (free) tree (自由树) is any set of points (called vertices ,顶点 ) and any set of pairs of distinct vertices (called edges or branches (边或分支) such that

(1) there is a sequence of edges (a path ,路径 ) from any vertex to any other,

(2) there are no circuits (回路) , that is, no paths starting from a vertex and returning to the same vertex.

Page 4: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

On the classification of species

Page 5: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

On the classification of species A rooted tree ( 有根树) is a tree in which one

vertex, called the root, is distinguished. An ordered tree (有序树) is a rooted tree in

which the children of each vertex are assigned an order.

A forest (森林) is a set of trees. We usually assume that all trees in a forest are rooted.

An orchard (also called an ordered forest ,有序森林 ) is an ordered set of ordered trees.

Page 6: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

A

B C D

E F G H I J

MK L

A( B(E, F(K, L)), C(G), D(H, I, J(M)) )

T1 T3T2树根

例如 :

Page 7: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

on the classification of species

Page 8: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

implementations of ordered Trees

Multiple links (多重链表) first_child and next_sibling links (孩子兄弟链表) Correspondence with binary trees (和二叉树的对应)

Page 9: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

implementations of ordered Trees

Page 10: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Recursive Definitions A rooted tree consists of a single vertex v, called the root of

the tree, together with a forest F , whose trees are called the subtrees of the root.

A forest F is a (possibly empty) set of rooted trees. An ordered tree T consists of a single vertex v, called the ro

ot of the tree, together with an orchard O, whose trees are called the subtrees of the root v. We may denote the ordered tree with the ordered pair T ={v,O}.

An orchard O is either the empty set , or consists of an ordered tree T , called the first tree of the orchard, together with another orchard O1 (which contains the remaining trees of the orchard). We may denote the orchard with the ordered pair O =(T ,O1).

Page 11: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

The Formal Correspondence A binary tree B is either the empty set , or consists of a root verte

x v with two binary trees B1 and B2 . We may denote the binary tree with the ordered triple (元组) B= [v,B1,B2].

THEOREM 11.1 Let S be any finite set (有限集) of vertices. There is a one-to-one correspondence f from the set of orchards whose set of vertices is S to the set of binary trees whose set of vertices is S.

Proof. Refer page 526Define f (Φ)=Φ.Define f ({v,O1},O2)= [v, f (O1), f (O2)].

Show by mathematical induction )(数学归纳法) on the number of vertices that f is a one-to-one correspondence.

Page 12: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Rotations Draw the orchard so that the first child of each vertex i

s immediately below the vertex. Draw a vertical link from each vertex to its first child, a

nd draw a horizontal link from each vertex to its next sibling.

Remove the remaining original links. Rotate the diagram 45 degrees clockwise (顺时针方

向) , so that the vertical links appear as left links and the horizontal links as right links.

Page 13: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Rotations

Page 14: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Summary

Orchards and binary trees correspond by any of:

first child and next sibling links,rotations of diagrams,formal notational equivalence.

Page 15: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

对比树型结构和线性结构的结构特点

Page 16: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

线性结构 树型结构第一个数据元素 ( 无前驱 )

根结点 ( 无前驱 )

最后一个数据元素 ( 无后继 )

多个叶子结点 ( 无后继 )

其它数据元素(一个前驱、 一个后继 )

其它数据元素(一个前驱、 多个后继 )

Page 17: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Traverse of trees and

orchards

Page 18: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Level traverse:

Preorder:

Postorder:

若树不空,则先访问根结点,然后依次先根遍历各棵子树。

若树不空,则先依次后根遍历各棵子树,然后访问根结点。

若树不空,则自上而下自左至右访问树中每个结点。

Page 19: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

A

B C D

E F G

H

I J K

preorder :

A B E F C D G H I J K

postorder :

E F B C I J K H G D A

Level order :

A B C D E F G H I J K

Page 20: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

B C D

E F G

H

I J K

1 . The root of the first tree

2 . The orchard of the first tree3 . Other trees in the orchard

Orchards have three parts :

Page 21: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

1. 先序遍历有序森林的遍历

若有序森林不空,则访问森林中第一棵树的根结点;先序遍历森林中第一棵树的子树森林;先序遍历森林中 ( 除第一棵树之外 ) 其 余树构成的森林。

即:依次从左至右对森林中的每一棵树进行先根遍历。

Page 22: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

2.中序遍历 若森林不空,则中序遍历森林中第一棵树的子树森林;访问森林中第一棵树的根结点;中序遍历森林中 ( 除第一棵树之外 ) 其 余树构成的森林。即:依次从左至右对森林中的每一棵树进行后根遍历。

Page 23: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

树的遍历和二叉树遍历的对应关系 ?

先根遍历

后根遍历

树 二叉树森林

先序遍历 先序遍历

中序遍历 中序遍历

Page 24: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

哈 夫 曼 树 与 哈 夫 曼 编 码

哈夫曼树 如何构造哈夫曼树 哈夫曼编码

Page 25: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

树的带权路径长度定义为: 树中所有叶子结点的带权路径长度之和 WPL(T) = wklk ( 对所有叶子结点 ) 。

在所有含 n 个叶子结点、并带相同权值的 m 叉树中,必存在一棵其带权路径长度取最小值的树,称为“最优树”。

例如:

一、哈夫曼树

Page 26: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

在所有含 n 个叶子结点、并带相同权值的 二 叉树中,必存在一棵其带权路径长度取最小值的树,称为“最优二叉树”或“哈夫曼树”。

Page 27: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

27 9

7 5

4

9

2

WPL(T)=

72+52+23+43+92

=60

WPL(T)=

74+94+53

+42+21

=89

5

4

Page 28: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

根据给定的 n 个权值 {w1, w2, …, w

n} ,

构造 n 棵二叉树的集合 F = {T1, T2, … , Tn} ,

其中每棵二叉树中均只含一个带权值 为 wi 的根结点,其左、右子树为空树;

二、如何构造哈夫曼树

(1)

Page 29: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

在 F 中选取其根结点的权值为最 小的两棵二叉树,分别作为左、 右子树构造一棵新的二叉树,并 置这棵新的二叉树根结点的权值 为其左、右子树根结点的权值之 和;

(2)

Page 30: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

从 F 中删去这两棵树,同时加入 刚生成的新树;

重复 (2) 和 (3) 两步,直至 F 中只 含一棵树为止。

(3)

(4)

Page 31: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

9

例如 : 已知权值 W={ 5, 6, 2, 9, 7 }

5 6 2 7

5 2

76 9 7

6 7

139

5 2

7

Page 32: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

6 7

139

5 2

7

9

5 2

7

16

6 7

13

290

0 0

0

1

1 1

100 01 10

110 111

Page 33: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

主要用途是实现数据压缩。主要用途是实现数据压缩。 设给出一段报文: 设给出一段报文: CAST CAST SAT AT A TASA CAST CAST SAT AT A TASA 字符集合是 字符集合是 { C, A, S, T }{ C, A, S, T } ,各个字符出现的频度,各个字符出现的频度 (( 次次

数数 )) 是 是 WW == { 2, 7, 4, 5 }{ 2, 7, 4, 5 } 。 。 ( 1 )以 ASCII 码传输

( 2 )若给每个字符以等长编码 若给每个字符以等长编码 AA : 00 T : 10 C : 01 S : 11 : 00 T : 10 C : 01 S : 11 则总编码长度为 则总编码长度为 ( 2+7+4+5 ) * 2 = 36. ( 2+7+4+5 ) * 2 = 36. (( 33 )若按各个字符出现的概率不同而给予不等长编)若按各个字符出现的概率不同而给予不等长编

码,可望减少总编码长度。码,可望减少总编码长度。

三、哈夫曼编码

Page 34: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

以各字符出现的次数 以各字符出现的次数 { 2, 7, 4{ 2, 7, 4, , 5 }5 } 为各叶结点上的权值,为各叶结点上的权值,建立哈夫曼树。左分支赋 建立哈夫曼树。左分支赋 00 ,右分支赋 ,右分支赋 11 ,得哈夫,得哈夫曼编码曼编码 (( 变长编码变长编码 )) 。 。

AA : 0 T : 10 C : 110 S : 111 : 0 T : 10 C : 110 S : 111

它的总编码长度:它的总编码长度: 7*1+5*2+7*1+5*2+( 2+4 )*3 = 35( 2+4 )*3 = 35 。比等长。比等长编码的情形要短。编码的情形要短。

Page 35: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

DefinitionOne method is to prune from the tree all the branches that do not lead to any key. In English, for example, there are no words that begin with the letters ‘bb,’ ‘bc,’ ‘bf,’ ‘bg,’ : : : , but there are words beginning with ‘ba,’ ‘bd,’ or ‘be.’ Hence all the branches and nodes for nonexistent words can be removed from the tree. The resulting tree is called a trie.A trie of order m is either empty or consists of an ordered sequence of exactly m tries of order m.

Page 36: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

Page 37: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

C++ Tie Declarationsclass Trie {public: // Add method prototypes here.private: // data membersTrie node *root;};const int num chars = 28;struct Trie node {// data membersRecord *data;Trie node *branch[num chars];// constructorsTrie node( );};

Page 38: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

Searching a TrieError code Trie :: trie search(const Key &target, Record &x) const/* Post: If the search is successful, a code of success is returned, an

d the output parameterx is set as a copy of theTrie 's record that holdstarget . Otherwise, a code ofnot present is returned.

Uses: Methods of classKey . */{int position = 0;char next char;Trie node *location = root;while (location != NULL &&(next char = target.key letter(position)) != 0 0) {// Terminate search for aNULL location or a blank in the target.location = location->branch[alphabetic order(next char)];// Move down the appropriate branch of the trie.

Page 39: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

Searching a Trieposition++;

// Move to the next character of the target.

}

if (location != NULL && location->data != NULL) {

x = *(location->data);

return success;

}

else

return not present;

}

Page 40: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

inserting into a Trie:Error code Trie :: insert(const Record &new entry)/* Post: If theKey ofnew entry is already in the Trie , a

code of duplicate error is returned. Otherwise, a code of success is returned and the Record new entry is inserted into theTrie .

Uses: Methods of classesRecord andTrie node . */{

Error code result = success;if (root == NULL) root = new Trie node; // Create a new empt

yTrie .int position = 0; //indexes letters ofnew entrychar next char;

Page 41: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Lexicographic Search Trees: Tries

inserting into a Trie:Trie node *location = root; // moves through theTriewhile (location != NULL && (next char = new entry.key letter(po

sition)) != 0 0) { int next position = alphabetic order(next char);

if (location->branch[next position] == NULL)location->branch[next position] = new Trie node;location = location->branch[next position];position++;

}// At this point, we have tested for all nonblank characters of new

entry .if (location->data != NULL) result = duplicate error;else location->data = new Record(new entry);return result;

}

Page 42: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Multiway Search Trees An m-way search tree is a tree in which, for some

integer m called the order of the tree, each node has at most m children.

If k<=m is the number of children, then the node contains exactly k -1 keys, which partition all the keys into k subsets consisting of all the keys less than the first key in the node, all the keys between a pair of keys in the node, and all keys greater than the largest key in the node.

Page 43: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Multiway Search Tree

Page 44: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Balanced Multiway Trees (B-Trees)

Page 45: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Balanced Multiway Trees (B-Trees)

Page 46: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Insertion into a B-Tree In contrast to binary search trees, B-trees are not

allowed to grow at their leaves; instead, they are forced to grow at the root.

General insertion method:Search the tree for the new key. This search (if the key is truly new) will terminate in failure at a leaf.Insert the new key into to the leaf node. If the node was not previously full, then the insertion is finished.When a key is added to a full node, then the node splits into two nodes, side by side on the same level, except that the median key is not put into either of the two new nodes.When a node splits, move up one level, insert the median key into this parent node, and repeat the splitting process if necessary.When a key is added to a full root, then the root splits in two and the median key sent upward becomes a new root. This is the only time when the B-tree grows in height.

Page 47: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Growth of a B-Tree

Page 48: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Growth of a B-Tree

Page 49: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

B-Tree Declaration in C++ B-Tree Class declaration:

template <class Record, int order>

class B tree {

public: // Add public methods.

private: // data members

B node<Record, order> *root;

// Add private auxiliary functions here.

};

Page 50: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

B-Tree Declaration in C++ B-Tree Node declaration:

template <class Record, int order>

struct B node {

// data members:

int count;

Record data[order - 1];

B node<Record, order> *branch[order];

// constructor:

B node( );

};

Page 51: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Searching in a B-Tree Public Method:

template <class Record, int order>

Error code B tree<Record, order> :: search tree(Record &target)

/* Post: If there is an entry in the B-tree whose key matches that intarget , the parameter target is replaced by the correspondingRecord from the B-tree and a code ofsuccess is returned. Otherwise a code ofnot present is returned.

Uses: recursive search tree */

{

return recursive search tree(root, target);

}

Page 52: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Searching in a B-Tree Recursive Function:

template <class Record, int order>

Error code B tree<Record, order> :: recursive search tree(B node<Record, order> *current, Record &target)

/* Pre: current is eitherNULL or points to a subtree of theB tree .

Post: If the Key of target is not in the subtree, a code of not present is returned. Otherwise, a code ofsuccess is returned andtarget is set to the correspondingRecord of the subtree.

Uses: recursive search tree recursively andsearch node */

{

Error code result = not present;

Page 53: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Searching in a B-Tree Recursive Function: (continued)

int position;if (current != NULL) {

result = search node(current, target, position);if (result == not present)

result =recursive search tree(current->branch[position], target);else

target = current->data[position];}return result;

}

Page 54: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Searching in a B-Tree Searching a Node

template <class Record, int order>

Error code B tree<Record, order> :: search node(B node<Record, order> *current, const Record &target, int &position)

/* Pre: current points to a node of aB tree .

Post: If theKey oftarget is found in*current , then a code ofsuccess is returned, the parameter position is set to the index of target , and the corresponding Record is copied totarget . Otherwise, a code ofnot present is returned, andposition is set to the branch index on which to continue the search.

Uses: Methods of classRecord . */

{position = 0;

Page 55: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Searching in a B-Tree

Searching a Node: (continued)while (position < current->count && target >

current->data[position])

position++;

if (position < current->count

&& target == current->data[position])

return success;

else

return not present;

}

Page 56: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

public insertion methodtemplate <class Record, int order>Error code B tree<Record, order> :: insert(const Record &new entry)/* Post: If theKey ofnew entry is already in theB tree , a code ofduplicate

erroris returned. Otherwise, a code of success is returned and the Record ne

w entry is inserted into the B-tree in such a way that the properties of a B-tree are preserved.

Uses: Methods ofstruct B node and the auxiliary functionpush down . */{

Record median;B node<Record, order> *right branch, *new root;Error code result =push down(root, new entry, median, right branch);

Page 57: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

public insertion method

if (result == overow) { // The whole tree grows in height.// Make a brand new root for the whole B-tree.

new root = new B node<Record, order>;new root->count = 1;new root->data[0] = median;new root->branch[0] = root;new root->branch[1] = right branch;root = new root;result = success;

}return result;

}

Page 58: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Recursive insertion into a Subtreetemplate <class Record, int order>Error code B tree<Record, order> :: push down(B node<Record, order>

*current, const Record &new entry, Record &median, B node<Record, order> * &right branch)

/* Pre: current is eitherNULL or points to a node of a B-tree .Post: If an entry with a Key matching that ofnew entry is in the subtree t

o which current points, a code of duplicate error is returned. Otherwise, new entry is inserted into the subtree: If this causes the height of the subtree to grow, a code of oveflow is returned, and the Record median is extracted to be reinserted higher in the B-tree, together with the subtree right branch on its right. If the height does not grow, a code of success is returned.

Uses: Functions push_down (called recursively), search node ,split node , and push in . */

Page 59: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Recursive insertion into a Subtree{ Error code result;

int position;if (current == NULL) {// Since we cannot insert in an empty tree, the recursion terminates.median = new entry;right branch = NULL;result = overflow;

} else { // Search the current node.if (search node(current, new entry, position) == success)

result = duplicate error;else {

Record extra entry;B node<Record, order> *extra branch;

Page 60: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Recursive insertion into a Subtreeresult = push_down(current->branch[position], new entry,extra entry, extra

branch);if (result == overflow) {// Record extra entry now must be added tocurrent

if (current->count < order - 1) {result = success;push_in(current, extra entry, extra branch, position);

}else split_node( current, extra entry, extra branch, position,right branch, median);// Record median and itsright branch will go up to a higher node.} } }return result;

}

Page 61: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

push_in methodtemplate <class Record, int order>void B tree<Record, order> ::push_in(B node<Record, order> *current,const Record &entry,B node<Record, order> *right branch,int position)/* Pre: current points to a node of a B-tree . The node*current is not full an

d entry belongs in*current at index position .Post: entry has been inserted along with its right-hand branch right branc

h into *current at index position . */{

for (int i = current->count; i > position; i--) {// Shift all later data to the right.

current->data[i] = current->data[i - 1];current->branch[i C 1] = current->branch[i];}current->data[position] = entry;current->branch[position C 1] = right branch;current->countCC;

}

Page 62: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Example of Splitting a Full Node

Page 63: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Example of Splitting a Full Node

Page 64: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Function split_node Specificationstemplate <class Record, int order>void B tree<Record, order> :: split node(B node<Record, order> *curre

nt, const Record &extra entry, B node<Record, order> *extra branch,int position, B node<Record, order> * &right half, Record &median) /* Pre: current points to a node of aB tree . The node*current is full, but i

f there were room, the recordextra entry with its right-hand pointerextra branch would belong in*current at positionposition ,0 position < order .

Post: The node*current withextra entry and pointerextra branch at position

position are divided into nodes *current and *right half separated by a Record median .

Uses: Methods ofstruct B node , functionpush in . */

Page 65: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Function split_node Actions{

right half = new B node<Record, order>;int mid = order/2; if (position <= mid) { // First case: extra entry belongs in left half.

for (int i = mid; i < order - 1; i++) { // Move entries to right_half .

right half->data[i - mid] = current->data[i];right half->branch[i +1 - mid] = current->branch[i +1];

}current->count = mid;right half->count = order - 1 - mid;push in(current, extra entry, extra branch, position);

}

Page 66: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Function split_node Actions (continued)else { // Second case: extra entry belongs in right half.

mid++; // Temporarily leave the median in left half.for (int i = mid; i < order - 1; i++) { // Move entries to right_half .

right half->data[i - mid] = current->data[i];right half->branch[i+1 - mid] = current->branch[i+1];

}current->count = mid;right half->count = order - 1 - mid;push_in(right half, extra entry, extra branch, position - mid);

}median = current->data[current->count - 1];// Remove median from left half.right half->branch[0] = current->branch[current->count];current->count--;}

Page 67: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Deletion from a B-tree

Page 68: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Deletion from a B-tree

Page 69: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

public Deletion Method

template <class Record, int order>Error code B tree<Record, order> :: remove(const Record &target)/* Post: If a Record with Key matching that of target belongs to the B tr

ee , a code of success is returned and the corresponding node is removed from the B-tree. Otherwise, a code ofnot present is returned.

Uses: Function recursive remove */{ Error code result;

result = recursive remove(root, target);if (root != NULL && root->count == 0) { // root is now empty.

B node<Record, order> *old root = root;root = root->branch[0];delete old root;

}return result;

}

Page 70: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

recursive deletion

template <class Record, int order>Error code B tree<Record, order> :: recursive remove(B node<Record,

order> *current, const Record &target)/* Pre: current is eitherNULL or points to the root node of a subtree of a

B tree .Post: If a Record with Key matching that of target belongs to the subtree,

a code of success is returned and the corresponding node is removed from the subtree so that the properties of a B-tree are maintained. Otherwise, a code of not present is returned.

Uses: Functionssearch node ,copy in predecessor ,recursive remove recursively), remove data , andrestore . */

{ Error code result;int position;if (current == NULL) result = not present;

Page 71: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

recursive deletion (continued)else {

if (search node(current, target, position) == success) {// The target is in the current node.result = success;if (current->branch[position] != NULL) { // not at a leaf node

copy in predecessor(current, position);recursive remove(current->branch[position],current->data[position]); }

else remove data(current, position); // Remove from a leaf node.}else result = recursive remove(current->branch[position], target);if (current->branch[position] != NULL)if (current->branch[position]->count < (order - 1)/2)

restore(current, position);}return result;

}

Page 72: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Auxiliary Functions Remove data from a leaf:

template <class Record, int order>

void B tree<Record, order> ::remove data(B node<Record, order> *current,int position)

/* Pre: current points to a leaf node in a B-tree with an entry atposition .

Post: This entry is removed from*current . */

{

for (int i = position; i < current->count - 1; iCC)

current->data[i] = current->data[i C 1];

current->count--;

}

Page 73: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Auxiliary Functions Replace data by its immediate predecessor:

template <class Record, int order>void B tree < Record, order > :: copy_in_predecessor(B node<Reco

rd, order> *current, int position)/* Pre: current points to a non-leaf node in a B-tree with an entry atpo

sition .Post: This entry is replaced by its immediate predecessor under orde

r of keys.*/{

B node<Record, order> *leaf = current->branch[position];// First go left from the current entry.while (leaf->branch[leaf->count] != NULL)

leaf = leaf->branch[leaf->count];// Move as far rightward as

possible.current->data[position] = leaf->data[leaf->count - 1];}

Page 74: Chapter 11 Multiway Trees. Content Points  Orchards, Trees, and Binary Trees  Traverse of Orchards and trees  Huffman trees

Pointers and Pitfalls

1. Trees are flexible and powerful structures both for modeling problems and for organizing data. In using trees in problem solving and in algorithm design, rst decide on the kind of tree needed (ordered, rooted, free, or binary) before considering implementation details.

2. Most trees can be described easily by using recursion; their associated algorithms are often best formulated recursively.

3. For problems of information retrieval, consider the size, number, and location of the records along with the type and structure of the entries while choosing the data structures to be used.