1 / 26

Binary Tree

Binary Tree. Tree. Definition Tree is a finite set T which satisfies the following conditions . I t 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.  Level 0. A.  Level 1. C. D.

particia
Télécharger la présentation

Binary Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Tree

  2. 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.

  3.  Level 0 A  Level 1 C D B  Level 2 E F G H I J  Level 3 K L M Quick Review: Basic terms (1) • 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

  4.  Level 0 A  Level 1 C D B  Level 2 E F G H I J  Level 3 K L M Quick Review: Basic Terms (2) • 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

  5.  Level 0 A  Level 1 C D B  Level 2 E F G H I J  Level 3 K L M Quick Review: Basic terms (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.

  6.  Level 0 A  Level 1 C D B  Level 2 E F G H I J  Level 3 K L M Quick Review: Basic terms (4) • 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Quick Review:Traverse in details (4) • Infix: A*(B+C) • Prefix: *A+BC • Postfix: ABC+*

  13. 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. : easy • The node to be deleted has two children. : quite complicated

  14. 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

  15. 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.

  16. 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

  17. 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.

  18. 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.

  19. The efficiency of Binary Search Trees • Time Complexity of Binary Search Tree: O(log N)

  20. 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

  21. 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.

  22. Binary Search Tree • Java code of BST operations

  23. 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

  24. The Huffman Code (2) Frequency Table (example)

  25. 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

  26. Creating the Huffman Code (3) Huffman Code: Textbook (pages 416~420)

More Related