1 / 37

Extension of linked list

Extension of linked list. Zhen Jiang West Chester University zjiang@wcupa.edu. Outline. Double Cyclic Linked List Queue & Stack Binary Tree Expression Tree HEAP Huffman Tree Tree balance and rotation Networks. (Single) Linked list. node. NULL. Double Cyclic Linked List. node.

Download Presentation

Extension of linked list

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. Extension of linked list Zhen Jiang West Chester University zjiang@wcupa.edu

  2. Outline • Double Cyclic Linked List • Queue & Stack • Binary Tree • Expression Tree • HEAP • Huffman Tree • Tree balance and rotation • Networks

  3. (Single) Linked list node NULL

  4. Double Cyclic Linked List node NULL

  5. NULL NULL NULL

  6. Queue node Delete an item from the tail NULL Insert a new item at the head

  7. Stack node NULL Insert a new item at the head Delete an item from the head

  8. (5 + 4) * 3 • 5 * (4 + 3) • 5 * 4 + 3 • 5 + (4 + 3) • 5 4 + 3 * • 5 4 3 + * • 5 4 * 3 + • 5 4 3 * +

  9. Read the formula from left to right • Number => push • Binary operator => 2 pops • Right number popped first • Then, left number popped. • Calculate result = <second> op <first> • Push (result) • Until expression reading finishes and only one (final) result is left in stack

  10. Redirecting the link connection node NULL

  11. Tree • Definition • Each node u has n(u) children • Considering the parent-child relationship is undirected, there is no cycle in a tree • There is only one node called the root in the entire tree. It has children only but no parent.

  12. Binary Tree • Definition • Definition of tree (3) • For each node u, n(u)<3 • L<S<R • < is a relation between any two nodes in the tree

  13. Construction • Assume each node has a number value so that we have the relation < • Development at the level of single/double linked list • public class BinaryTree<E extends Comparable<E>> { … } • root = null;

  14. Insertion • Assume each node has a number value so that we have the relation < • public void add (E item) • If (root == null) • root = new Node<E> (item); • Else • Add(root, item);

  15. public void add (Node<E>p, E item) • If ((item.compareTo(p.data) < 0) • If (p.left == null) • p.left = new Node<E> (item); • else • add (p.left, item) • Else If ((item.compareTo(p.data) > 0) • Similar to the case (value < 0)

  16. Another Insertion • Return what is updated in the lower level • public void add (E item) • root = add(root, item);

  17. public Node<E> add (Node<E>p, E item) • If (p==null) • return new Node<E>(item); • Else If ((item.compareTo(p.data) < 0) • p.left = add(p, item); • return p; • Else If ((item.compareTo(p.data) > 0) • Similar to the case (value < 0)

  18. Travel (print out all node values) • Infix travel, L->S->R • Postfix travel, L->R->S • Prefix travel, S->L->R

  19. InfixTravel (Node<E>n, int d, String s) • infixTravel(root, 1, “”) • s+= ““ • If node n is empty, s+=“null\n”; • Else • String str = “”; • str = infixTravel(n.left, d+1, s) • str += s+n.data+”\n”; • str += infixTravel(n.right, d+1, s) • Return str

  20. Search (Node<E> p, E item) • search(root, item); • r = item.compareTo(p.data); • If (r<0) • If (p.left!=null) return search(p.left, item); • Else return false; • If (r>0) • Similar processing as the above. • Else • return True;

  21. Deletion • Combined with a search • Need a replacement policy • For instance, the largest in the left sub-tree or infix order predecessor • Switch the value and delete the replacement node (why not the deletion target?)

  22. Deletion process (recursion) • If the current root is empty or null // not found, no action • If item.compareTo(data at local root)<0 • Save the deletion result and reset the left sub-tree • Return the local root • If item is larger than the data at local root • Save the deletion result and reset the right sub-tree • Return the local root • Else // item is the same as the local root • If either left or right is empty • Return the other child as the local root to connect the upper • Else • Find the rightmost node M in the right sub-tree of the left child, copy its data to local root, and delete it.

  23. Expression Tree • Definition • Definition of binary tree (5) • All leaves are numbers, and all intermediate nodes are operators • To simplify the discussion in this class, we use binary operators only.

  24. Evaluation • If node is not empty, R_E(node); • R_E(p) • If p is not leaf • Lvalue = R_E(p.left); • Rvalue = R_E(p.right); • Return Lvalue <p.data.toString().charAt(0)> Rvalue; • Else • Return Double.parseDouble(p.data.toString())

  25. Print • Postfix • Infix, adding ( ) only when it becomes necessary • Construction • From infix expression

  26. While (input!=null) • Input == ( ? • Push • Input == op ? • opPre = Last operator in opStack • Lower_priority(Input_op, opPre)? • R = num.pop(), L = num.pop() • Num.push(Current (op.pop, L, R)) • op.push(input) • Else • op.push(input)

  27. While (input!=null) -- continue • Input == )? • While op.peek() != ( • R = num.pop, L = num.pop • Num.push(Current(op.pop(), L, R)) • Op.pop() // delete ( • Else • Num.push(Current(input)); • Continue to read the expression -> input

  28. Is there anything missing? • While !op.empty • R = num.pop(), L = num.pop() • Num.push(Current(op.pop(),L,R)) • Root = num.pop(); // last thing to do

  29. private static class Node<E> {private E data;private Node<E> left;private Node<E> right;private Node(E item){ data = item; left = null; right = null;}private Node(E item, Node<E> refLeft, Node<E> refRight){ data = item; left = refLeft; right = refRight;}}

  30. private boolean lowerPriority(String upper, String lower){ if (lower == null) return false; char hi = upper.charAt(0); char lo = lower.charAt(0); switch(hi){ case '+': case '-': if (lo == '(') return false; else return true; case '*': case '/': if (lo == '*' || lo =='/') return true; else return false; } return false; }

  31. HEAP (smallest) • Deletion (quick sorting) and insertion • Insertion • Insert the new item at the end of list, i.e., addLast(item) • Child = size()-1, parent = (child-1)/2; • While new item does not reach the root (i.e., parent>=0) and it is smaller than its parent (i.e., get(parent)>get(child)) • Swap this item with its parent value • Child = parent, parent=(child-1)/2;

  32. Deletion (E item) • If !contains(item) return • Pos = indexOf(item); • If (pos != size()-1) • Val = remove (size()-1); • Set (pos, val); • Heapfy(pos); • Else remove(size()-1);

  33. Heapfy (int p) • Left = p*2+1, Right = p*2+2; • If(left <=size()-1 && ((right>size()-1 && get(p)>get(left)) || (right<=size()-1&&get(left)<get(right) && get(left)<get(p)) ) ) • Switch (left, p) • Heapfy(left) • Else

  34. Else if (right<=size()-1 && get(left)>get(right) && get(p) >get(right)) • Switch (right, p) • Heapfy(right)

  35. Sorting_out • Remove the root, the largest one! • Replace it with the tail of the list. • Repeat the above one by one until clear.

  36. Huffman Tree • Sorting by weight after each move • Merge the least two weights into one tree • https://www.siggraph.org/education/materials/HyperGraph/video/mpeg/mpegfaq/huffman_tutorial.html

  37. Other Trees? • AVL tree (height between L and R +/- 1) • B-tree and 2-3-4 tree • Split

More Related