370 likes | 388 Views
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.
E N D
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 NULL
NULL NULL NULL
Queue node Delete an item from the tail NULL Insert a new item at the head
Stack node NULL Insert a new item at the head Delete an item from the head
(5 + 4) * 3 • 5 * (4 + 3) • 5 * 4 + 3 • 5 + (4 + 3) • 5 4 + 3 * • 5 4 3 + * • 5 4 * 3 + • 5 4 3 * +
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
Redirecting the link connection node NULL
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.
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
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;
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);
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)
Another Insertion • Return what is updated in the lower level • public void add (E item) • root = add(root, item);
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)
Travel (print out all node values) • Infix travel, L->S->R • Postfix travel, L->R->S • Prefix travel, S->L->R
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
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;
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?)
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.
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.
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())
Print • Postfix • Infix, adding ( ) only when it becomes necessary • Construction • From infix expression
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)
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
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
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;}}
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; }
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;
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);
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
Else if (right<=size()-1 && get(left)>get(right) && get(p) >get(right)) • Switch (right, p) • Heapfy(right)
Sorting_out • Remove the root, the largest one! • Replace it with the tail of the list. • Repeat the above one by one until clear.
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
Other Trees? • AVL tree (height between L and R +/- 1) • B-tree and 2-3-4 tree • Split