Download presentation
Presentation is loading. Please wait.
Published byBathsheba Maria Matthews Modified over 8 years ago
1
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee
2
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, T 1, T 2, T 3, …, T n. Each subset is also a tree. Thus, tree is defined recursively.
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 A B E KL F C G D HIJ M Level 0 Level 1 Level 2 Level 3
4
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 A B E KL F C G D HIJ M Level 0 Level 1 Level 2 Level 3
5
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. A B E KL F C G D HIJ M Level 0 Level 1 Level 2 Level 3
6
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 A B E KL F C G D HIJ M Level 0 Level 1 Level 2 Level 3
7
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
8
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 class Node BST operations find: p378 find insert (or add): always add new leaf. p380 insert (or add) traverse min and max in tree (or subtree) delete
9
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
10
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
11
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
12
Infix: A*(B+C) Prefix: *A+BC Postfix: ABC+*
13
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. : easy The node to be deleted has two children. : quite complicated
14
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
15
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.
16
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 Finding the successor
17
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.
18
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.
19
Time Complexity of Binary Search Tree: O(log N)O(log N)
20
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
21
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.
22
Java code of BST operations Java code of BST operations
23
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 CharacterDecimalBinary ABC…XYZABC…XYZ 65 66 67 … 88 89 90 01000001 01000010 01000011 … 01011001 01011010 01011100
24
Frequency Table (example) CharacterCount A E I S T U Y Space Linefeed 223611241223611241
25
U sp A T If S I Y E 22 9 5 3 2 1 4 2 1 1 13 7 4 2 6 3 2
26
CharacterCode A E I S T U Y Space Linefeed 010 1111 110 10 0110 01111 1110 00 01110 Huffman Code: Textbook (pages 416~420)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.