Chapter 10 Trees and Binary Trees Part 2.Traversal level by level.
-
Upload
simon-wright -
Category
Documents
-
view
223 -
download
0
Transcript of Chapter 10 Trees and Binary Trees Part 2.Traversal level by level.
Chapter 10Trees and Binary Trees
Part 2
?Traversal level by level
Definitions
Rooted trees with four vertices(Root is at the top of tree.)
Ordered trees with four vertices
Implementations of Ordered Trees
• Multiple links
• first child and next sibling links• Correspondence with binary trees
data child1 Child2 …
data first child Next sibling
Conversion Conversion
(from forest/Orchard to binary tree(from forest/Orchard to binary tree
Corresponded binary tree respectively
Corresponded binary tree
Huffman Tree(Huffman Tree( 哈夫曼树哈夫曼树 ))
Path Length (Path Length ( 路径长度 路径长度 ))
Path Length of the binary tree Path Length of the binary tree Total length of all from leaves to rootTotal length of all from leaves to root
Weighted Path LengthWeighted Path Length ( (WPL,WPL, 带权路径长度 带权路径长度 ))
树的带权路径长度是树的各叶结点所带的权值树的带权路径长度是树的各叶结点所带的权值与该结点到根的路径长度的乘积的和。与该结点到根的路径长度的乘积的和。
1
0
n
iii lwWPL
How about the WPLof the following binary trees
Huffman tree
A (extended) binary tree with minimal A (extended) binary tree with minimal WPLWPL 。。
Processes of huffman treeProcesses of huffman tree
Huffman coding Huffman coding
Compression Compression suppose we have a messagesuppose we have a message : : CAST CAST SAT AT A TASACAST CAST SAT AT A TASA alphabet ={ C, A, S, T }alphabet ={ C, A, S, T } ,, frequency of them frequency of them (( 次数次数 ) are ) are WW== { 2, 7, 4, 5 }{ 2, 7, 4, 5 } 。。 first case equal length codingequal length coding AA : 00 T : 10 C : 01 S : 11 : 00 T : 10 C : 01 S : 11Total coding length of the message is Total coding length of the message is ( 2+7+4+5 ) * 2 = 36.( 2+7+4+5 ) * 2 = 36.
AA : 0 T : 10 C : 110 S : 111 : 0 T : 10 C : 110 S : 111Total length of huffman codingTotal length of huffman coding : :
7*1+5*2+7*1+5*2+( 2+4 )*3 = 35( 2+4 )*3 = 35Which is shorter than that of equal length codingWhich is shorter than that of equal length coding 。。霍夫曼编码是一种无前缀编码。解码时不会混淆。霍夫曼编码是一种无前缀编码。解码时不会混淆。
Huffman tree ??
Binary Search Trees• Can we find an implementation for
ordered lists in which we can search quickly (as with binary search on a contiguous list) and in which we can make insertions and deletions quickly (as with a linked list)?
DEFINITION• A binary search tree is a binary tree that is either empt
y or in which the data entry of every node has a key and satisfies the conditions:
1. The key of the left child of a node (if it exists) is less than the key of its parent node.
2. The key of the right child of a node (if it exists) is greater than the key of its parent node.
3. The left and right subtrees of the root are again binary search trees.We always require:No two entries in a binary search tree may have equal keys.
different views• We can regard binary search trees
as a new ADT.• We may regard binary search trees
as a specialization of binary trees.• We may study binary search trees
as a new implementation of the ADT ordered list.
The Binary Search Tree Class
Recursive auxiliary function:
template <class Record>Binary node<Record> *Search tree<Record> :: search for
node( Binary node<Record>* sub root, const Record &target) const
{if (sub root == NULL || sub root->data == target)return sub root;else if (sub root->data < target)return search for node(sub root->right, target);else return search for node(sub root->left, target);}
Nonrecursive version:template <class Record>Binary node<Record> *Search tree<Record> :: search for
node( Binary node<Record> *sub root, const Record &target) const
{while (sub root != NULL && sub root->data != target)if (sub root->data < target) sub root = sub root->right;else sub root = sub root->left;return sub root;}
Public method for tree search:template <class Record>
Error code Search tree<Record> ::tree search(Record &target) const
{Error code result = success;Binary node<Record> *found = search for node(root, target);if (found == NULL)
result = not present;else
target = found->data;return result;}
Binary Search Trees with the Same Keys
search
Creating a BST by insertion
insertion
Method for Insertion
Method for Insertion
Analysis of insertion
Treesort• When a binary search tree is traversed in in
order, the keys will come out in sorted order.
• This is the basis for a sorting method, called treesort: Take the entries to be sorted, use the method insert to build them into a binary search tree, and then use inorder traversal to put them out in order.
Treesort
Comparison First advantage of treesort over quicksort: The
nodes need not all be available at the start of the process, but are built into the tree one by one as they become available.
Second advantage: The search tree remains available for later insertions and removals.
Drawback: If the keys are already sorted, then treesort will be a disasteróthe search tree it builds will reduce to a chain. Treesort should never be used if the keys are already sorted, or are nearly so.
Removal from a Binary Search Tree
Removal from a Binary Search Tree (continue)
Height Balance: AVL TreesDefinition:An AVL tree is a binary search tree in which the heights ofthe left and right subtrees of the root differ by at most 1 andin which the left and right subtrees are again AVL trees.With each node of an AVL tree is associated a balancefactor that is left higher, equal, or right higher according,respectively, as the left subtree has height greater than, equ
alto, or less than that of the right subtree.
Example AVL trees
Example AVL trees
Example non-AVL trees
Example non-AVL trees
Insertions into an AVL tree
Insertions into an AVL tree
Rotations of an AVL Tree
Double Rotation
Deletion with no rotations
Deletion with single left rotations
Deletion with double rotation
Worst-Case AVL Trees
Fibonacci Trees
Analysis of Fibonacci Trees
Analysis of Fibonacci 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.
Balanced Multiway Trees (B-Trees)
B-Tree Example
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:
1. Search the tree for the new key. This search (if the key is truly new) will terminate in failure at a leaf.
2. Insert the new key into to the leaf node. If the node was not previously full, then the insertion is finished.
Insertion into a B-Tree3. 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.
4. When a node splits, move up one level, insert the median key into this parent node, and repeat the splitting process if necessary.
5. 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.
Growth of a B-Tree
Growth of a B-Tree
Growth of a B-Tree
Growth of a B-Tree
Deletion from a B-Tree
Deletion from a B-Tree
Deletion from a B-Tree