Representation of a Threaded Tree class Threaded Node

  • Slides: 6
Download presentation
Representation of a Threaded Tree class Threaded. Node { <declarations for info stored in

Representation of a Threaded Tree class Threaded. Node { <declarations for info stored in node, e. g. int info; > Threaded. Node left; Threaded. Node right; boolean l. Thread; // true if left is a thread boolean r. Thread; // true if right is a thread } public void display. Node(){…} // display info stored in node left l. Thread info right r. Thread COSC 2 P 03 Week 3 1

Threaded Trees – find. Successor Threaded. Node find. Successor(Threaded. Node T) // find successor

Threaded Trees – find. Successor Threaded. Node find. Successor(Threaded. Node T) // find successor S of node T { if (T. r. Thread) // thread points to successor S = T. right; else // successor is leftmost node in right subtree { S = T. right; while (!S. l. Thread) S = S. left; } return S; } COSC 2 P 03 Week 4 2

Threaded Trees – Inorder Traversal inorder. Threaded. Traverse(Threaded. Node T) { S = T;

Threaded Trees – Inorder Traversal inorder. Threaded. Traverse(Threaded. Node T) { S = T; // find leftmost node, which is the first node // visited in the traversal while (!S. l. Thread) S=S. left; // keep going until you reach the end of the // traversal (last pointer will be null) while (S != null) { display. Node(S); // visit S S = find. Successor(S); } } COSC 2 P 03 Week 4 3

Threaded Trees – thread existing tree Threaded. Node right. Thread. Tree(Threaded. Node T) {

Threaded Trees – thread existing tree Threaded. Node right. Thread. Tree(Threaded. Node T) { Thread. This = null; if (T. left != null) { // thread left subtree Thread. This = right. Thread. Tree(T. left); // Thread. This is last node visited in T’s left subtree // so its successor is T: set thread Thread. This. right = T; } if (T. right != null) // thread right subtree Thread. This = right. Thread. Tree(T. right); else // T. right is null, so must be turned into a thread { Thread. This = T; T. r. Thread = true; } return Thread. This; // return node that needs a thread } COSC 2 P 03 Week 4 4

Threaded Trees – threaded. Insert void threaded. Insert(Threaded. Node T, Threaded. Node new. Node)

Threaded Trees – threaded. Insert void threaded. Insert(Threaded. Node T, Threaded. Node new. Node) // insert new. Node into tree with root T // T is already threaded – ensure this is maintained { if(T == null) T = new. Node; else if (new. Node. info < T. info) // insert in left subtree { if(T. l. Thread) // insert here { new. Node. left = T. left; // predecessor of new. Node is T. left new. Node. right = T; // successor of new. Node is T T. left = new. Node; // attach to left of T T. l. Thread = false; // T. left is no longer a thread } else T. left = threaded. Insert(T. left, new. Node); } else // insert in right subtree { if(T. r. Thread) // insert here { new. Node. left = T; // predecessor of new. Node is T new. Node. right = T. right; // successor of new. Node is T. right = new. Node; // attach to right of T T. r. Thread = false; // T. right is no longer a thread } else T. right = threaded. Insert(T. right, new. Node); } COSC 2 P 03 Week 4 } 5

Height-Balanced Trees • Search complexity for binary search tree with n nodes: – Best

Height-Balanced Trees • Search complexity for binary search tree with n nodes: – Best case: O(log n) Worst-case: O(n) • Height-balanced trees attempt to keep subtrees at roughly the same height • Some possible options for a balance condition: – Left and right subtrees of the root have the same height: but height can still be n/2 – For every node, left and right subtrees have the same height: only possible if n = 2 k - 1 – For every node, left and right subtrees differ in height by at most 1: this is the AVL property COSC 2 P 03 Week 4 6