Binary Tree
Tree Definition Tree is a finite set T which satisfies the following conditions. It has one root. Except the root, nodes are divided into n subsets, T1, T2, T3, …, Tn. Each subset is also a tree. Thus, tree is defined recursively.
Quick Review: Basic terms (1) L F C G D H I J M Level 0 Level 1 Level 2 Level 3 Node: a basic component which can contain data and references to 0 or more other nodes. (ex) A~M Root: the node on the level 0. (ex) A Level
Quick Review: Basic Terms (2) L F C G D H I J M Level 0 Level 1 Level 2 Level 3 Subtree: a node and all of its descendents (ex) {B, E, F, K, L}, {E, K, L}, … Leaf (or Terminal node): a node of which degree is 0. (ex) F, G, I, J, K, L, M
Quick Review: Basic terms (3) L F C G D H I J M Level 0 Level 1 Level 2 Level 3 Interior node (or Nonterminal node): a node of which degree is greater than 0. Child/children Parent Siblings: nodes which have the same parent node. (ex) K and L are siblings. H, I and J are siblings.
Quick Review: Basic terms (4) L F C G D H I J M Level 0 Level 1 Level 2 Level 3 Ancestors: the ancestors of a node N are all the nodes on the path from the root to the node N. (ex) the ancestors of node L are A, B, E. Descendants Forest: a set of separated trees
Quick Review: Binary Tree [Definition] If every node in a tree can have at most two children, the tree is called a binary tree. [Term] Left child Right child
Quick review: Binary Search Tree (BST) Definition A binary tree in which every node is greater than its left child and less than its right child, if the left and/or right child exists. class Node BST operations find: p378 insert (or add): always add new leaf. p380 traverse min and max in tree (or subtree) delete
Quick Review: Traverse in details (1) inorder traversal (in a recursive way) call itself to traverse the node’s left subtree visit the node call itself to traverse the node’s right subtree
Quick Review: Traverse in details (2) preorder traversl (in a recursive way) visit the node call itself to traverse the node’s left subtree call itself to traverse the node’s right subtree
Quick Review: Traverse in details (3) postorder traversl (in a recursive way) call itself to traverse the node’s left subtree call itself to traverse the node’s right subtree visit the node Example: p386 infix notation, prefix notation, postfix notation
Quick Review: Traverse in details (4) Infix: A*(B+C) Prefix: *A+BC Postfix: ABC+*
Delete operation in details (1) Deleting a node is the most complicated common operation required for binary search trees. There are three possible cases when you delete a node. The node to be deleted is a leaf (had no children). : easy The node to be deleted has one child. The node to be deleted has two children. : quite complicated
Delete operation in details (2) Case 1: The node to be deleted is a leaf (had no children). [How?] Change the appropriate child field in the node’s parent to point to null. Java will automatically collect the garbage. Before deletion After deletion
Delete operation in details (3) Case 2: The node to be deleted has one child. [How?] Connect its parent directly to its child. In other words, change the appropriate reference in the parent (leftChild or rightChild) to point to the deleted node’s child.
Delete operation in details (4) Case 3: The node to be deleted has two children. [Definition] Inorder successor: For each node, the node with the next-highest key is called its inorder successor, or simply its successor. [How to delete?] Replace the node to be deleted with its inorder successor and then delete it. fig 8.16 in page 394 Finding the successor: fig 8.17 in page 395
Delete operation in details (4) Case 3 (continued) Successor can be the right child of the delNode: fig 8.18 in page 396 Deletion: fig 8.19 in page 399 Unplug current from the rightChild field of its parent (or leftChild field if appropriate), and set this filed to point to successor. Unplug current’s left child from current, and plug it into the leftChild field of successor.
Delete operation in details (5) Case 3 (continued) Successor can be a left descendant of the right child of delNode Deletion: fig 8.20 in page 400 Plug the right child of successor into the leftChild field of the successor’s parent. Plug the right child of the node to be deleted into the rightChild field of successor. Unplug current from the rightChild field of its parent, and set this field to point to successor. Unplug current’s left child from current, and plug it into the leftChild field of successor.
The efficiency of Binary Search Trees Time Complexity of Binary Search Tree: O(log N)
Tree Represented as Arrays the node array[index]’s left child = array[2*index + 1] right child = array[2*index + 2] parent = array[(index -1) /2] fig 8.21 in page 404
Duplicate Keys Possible – insert() method in the following tree.java will insert a duplicate key as the right child of its twin. However, it is recommended to forbid duplicate keys.
Binary Search Tree Java code of BST operations
The Huffman Code (1) Discovered by David Huffman in 1952 The Huffman code uses a binary tree and is applied to data compression. (Ex) [Character Codes] Some ASCII Codes Character Decimal Binary A B C … X Y Z 65 66 67 88 89 90 01000001 01000010 01000011 01011001 01011010 01011100
The Huffman Code (2) Frequency Table (example) Character Count A E I S Space Linefeed 2 3 6 1 4
22 9 13 6 4 5 7 S sp 3 4 2 3 I A 2 2 1 2 Y T E 1 1 If U
Creating the Huffman Code (3) Huffman Code: Textbook (pages 416~420) Character Code A E I S T U Y Space Linefeed 010 1111 110 10 0110 01111 1110 00 01110