Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.

Similar presentations


Presentation on theme: "Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1."— Presentation transcript:

1 Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1

2 Binary tree root parent child nonterminal node (terminal node) leaf Data Structures and Algorithms in Java, Third EditionCh06 – 2

3 Binary search tree el < el≥ el Data Structures and Algorithms in Java, Third EditionCh06 – 3

4 Node implementation public class BSTNode<T extends Comparable > { protected T el; protected BSTNode left, right; // constructors } el abbreviated as: Data Structures and Algorithms in Java, Third EditionCh06 – 4

5 Insertion in BST: algorithm insert(el ) p = root; prev = null; while p is not null // find a place for inserting new node; prev = p; if element in node p < el p = p.right; else p = p.left; if root is null // tree is empty; root becomes a new node with el; else if element in node prev < el new node with el is attached to the right of prev; else new node with el is attached to the left of prev; Data Structures and Algorithms in Java, Third EditionCh06 – 5

6 20 Insertion in the BST: example null 2030 20 15 1030 25 20 10 30 25 20 25 30 Data Structures and Algorithms in Java, Third EditionCh06 – 6

7 20 26 10 15 25 2327 20 23 1030 15 25 27 20 27 1030 25 30 15 Insertion in the BST: example (cont’d) Data Structures and Algorithms in Java, Third EditionCh06 – 7

8 Searching in the BST: algorithm search(el) p = root; while p is not null if element in node p equals el return element in node p; else if element in node p < el p = p.right; else p = p.left; return null; // el was not found; Data Structures and Algorithms in Java, Third EditionCh06 – 8

9 Searching in the BST: example 20 28 10 15 25 2327 30 26 20 25 10 15 25 2327 30 26 success failure Data Structures and Algorithms in Java, Third EditionCh06 – 9

10 Breadth-first traversal: algorithm public void breadthFirst() { BSTNode p = root; Queue > queue = new Queue >(); if (p != null) { queue.enqueue(p); while (!queue.isEmpty()) { p = queue.dequeue(); visit(p); if (p.left != null) queue.enqueue(p.left); if (p.right != null) queue.enqueue(p.right); }

11 20 10 15 25 2327 26 Breadth-first traversal: example 30 iteration queuep init 20 1 1030 2 10 30 3 20 15 4 25 5 23 27 6 2327 7 26 8 15 25 Data Structures and Algorithms in Java, Third EditionCh06 – 11

12 Depth-first traversals: algorithms Three tasks: V ‑ visiting a node L ‑ traversing the left subtree R ‑ traversing the right subtree The three tasks can be executed in 3! = 6 different orders: left to rightright to left preorder inorder postorder VLR LVR LRV VRL RVL RLV Data Structures and Algorithms in Java, Third EditionCh06 – 12

13 public void preorder() { preorder(root); } protected void preorder(BSTNode p) { if (p != null) { visit(p); preorder(p.left); preorder(p.right); } Depth-first traversals: implementation Data Structures and Algorithms in Java, Third EditionCh06 – 13

14 Depth-first traversals: example preorder inorder postorder 2010153025 20 10 15 25 2327 30 26 232726 1015202325262730 1510232627253020 Data Structures and Algorithms in Java, Third EditionCh06 – 14

15 Deletion by copying 1. Deleting a leaf 2. Deleting a node with one descendant Data Structures and Algorithms in Java, Third EditionCh06 – 15

16 Deletion by copying (cont’d) 3. Deleting a node with two descendants A BA Data Structures and Algorithms in Java, Third EditionCh06 – 16

17 Deletion by copying: example 20 10 15 25 2327 30 26 35 27 20 10 15 25 2326 27 35 15 10 25 2326 27 35 15 26 27 delete 30 delete 20 20 Data Structures and Algorithms in Java, Third EditionCh06 – 17

18 Rotations rotateRight(G,P,C) if P is not the root of the tree // i.e., if G is not null 1.grandparent G of child C becomes C ’s parent by replacing P; 2.right subtree of C becomes left subtree of C ’s parent P; 3.node C acquires P as its right child ; Data Structures and Algorithms in Java, Third EditionCh06 – 18

19 Step-by-step right rotation XY Z G P C XY Z G P C XY Z G P C XY Z G P C 2. right subtree of C becomes left subtree of C’s parent P; 3. node C acquires P as its right child; G C P YZ X = 1. grandparent G of child C becomes C’s parent by replacing P;

20 Left rotation YZ X G P C Y Z X G P C Data Structures and Algorithms in Java, Third EditionCh06 – 20

21 Self-adjusting trees  Single rotation: Rotate a child about parent if an element in the child is accessed unless it is the root.  Moving to the root: Repeat the child‑parent rotation until the element being accessed is in the root. Data Structures and Algorithms in Java, Third EditionCh06 – 21

22 Single rotation technique: example 8 5 9 3 6 2 4 7 8 6 9 5 7 3 2 4 6 5 8 3 7 9 2 4 5 3 6 2 4 8 7 9 Data Structures and Algorithms in Java, Third EditionCh06 – 22

23 Expression trees 5 + 6 7 (5 + 6) 7 + 67 * 5 preorder inorder postorder +5 * 67 * 56 7 + 5+6 * 7 567 * + preorder inorder postorder * +567 5+6 * 7 56+7 * ** Data Structures and Algorithms in Java, Third EditionCh06 – 23

24 Processing an expression in postfix notation 5 6 7 + 42 5 47 765765 6565 5 * Data Structures and Algorithms in Java, Third EditionCh06 – 24

25 (max) heaps 1.the value of each node is greater than or equal to the values stored in each of its children, 2.the tree is perfectly balanced and the leaves in in the last level are all in the leftmost positions. Data Structures and Algorithms in Java, Third EditionCh06 – 25

26 20 10 15 7 3 7 18 6 5 A binary tree that violates the first heap condition: 20 10 15 7 3 7 18 6 5 A binary tree that violates the second heap condition: Heaps: counterexamples Data Structures and Algorithms in Java, Third EditionCh06 – 26

27 heap binary search tree el ≤ el el < el≥ el Data Structures and Algorithms in Java, Third EditionCh06 – 27

28 2010 15 7371065 012345678 20 10 15 7 3 7 10 6 5 parentleft child right child 012 134 256 378 …. i2∙i +12∙i +2 Data Structures and Algorithms in Java, Third EditionCh06 – 28

29 Heap as a priority queue: enqueuing heapEnqueue(el) put el at the end of heap ; while el is not in the root and el > parent( el ) swap el with its parent ; Data Structures and Algorithms in Java, Third EditionCh06 – 29

30 Series of enqueuings 4 46 46 64 64 empty 6 4 3 6 4 3 7 6 7 3 4 7 6 3 4 5 643 7 7 6 3 4 5 4 7 6 3 4 5 4 7 6 4 4 5 3 764453 763454 763457634 6734643764364464

31 heapDequeue() extract the element from the root ; put the element from the last leaf in its place ; remove the last leaf ; // both subtrees of the root are heaps p = the root ; while p is not a leaf and p < any of its children swap p with the larger child ; Heap as a priority queue: dequeuing Data Structures and Algorithms in Java, Third EditionCh06 – 31

32 Series of dequeuings 7 6 4 4 5 3 764453 6 4 4 5 3 764453 3 6 4 4 5 36445 6 3 4 4 5 63445 6 5 4 4 3 65443 5 4 4 3 65443 3 5 4 4 3544 5 3 4 4 5344 5 4 4 3 5443 Data Structures and Algorithms in Java, Third EditionCh06 – 32

33 5 4 4 3 5443 4 4 3 5343 4 4 344 4 3 4 434 3 4 434 4 3 43 Series of dequeuings (cont’d) 3 43 3 3 Data Structures and Algorithms in Java, Third EditionCh06 – 33

34 Organizing arrays as heaps FloydAlgorithm(data[]) for i = index of the last nonleaf, down to 0 p = data[i]; while p is not a leaf and p < any of its children swap p with the larger child ; Data Structures and Algorithms in Java, Third EditionCh06 – 34

35 Organizing arrays as heaps: example 463754 4 6 3 7 5 4 last nonleaf 7 5 4 463754 4 3 464753 7 5 4 3 474653 7 6 5 4 7 4 4 6 5 3 744653 7 6 4 4 5 3 764453 3 6 Data Structures and Algorithms in Java, Third EditionCh06 – 35


Download ppt "Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1."

Similar presentations


Ads by Google