Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees CS-212 Dick Steflik. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root.

Similar presentations


Presentation on theme: "Trees CS-212 Dick Steflik. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root."— Presentation transcript:

1 Trees CS-212 Dick Steflik

2 What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root –The remaining nodes are partitioned into n>0 disjoint sets T 1,..,T n, where each of these sets is a tree. T 1,..,T n are the subtrees of the root.

3 Examples

4 Tree Terms Root A B C D E F G H height (h) leaf nodes (exterior nodes) interior nodes A is the parent of B and C B and C are siblings B and C are children of A

5 height - the number of nodes in the longest path going from the root to the furthest leaf parent - any node in the tree at the next higher level in the tree child - any node in the tree at the next lower level in the tree siblings - any nodes in the tree having a common parent order - the number of children in the node having the largest number of children binary tree - any order 2 tree binary search tree - any binary tree having the search tree property

6 Things represented as trees Table of Contents Subassembly diagrams Genealogy diagrams Pedigree diagrams Tournament playoffs Graphics representation Organizational charts

7 Nodal Structure Options If we know the maximum order that an arbitrary tree is supposed to be we could allocate our data content and a child pointer for each possible child Ex suppose max. order = 5 Data child 1 child 2 child 3 child 4 child 5 each node would look like: If our tree has many nodes that have less than 5 children this representation could be very wasteful considering that each child pointer requires 4 bytes of storage. Is there a better, less wasteful representation?

8 As it turns out, YES there is The lowly order 2 (binary) tree can be used to represent any order n tree. and we can make the statement that: For any general tree, there is an equivalent binary tree To do this we must visualize an order 2 tree differently; instead of as a collection of parents and children we view it as parent, leftmost child and that child’s siblings A BCD Instead of this : A B C D This:

9 Why do we want to do this? It turns out that order 2 tree have a very nice structure in that there are only two choices to make; Right or Left. This makes it easier to design algorithms for them. To explore this let us look at the problem of creating an algorithm for visiting every node in a tree in some predictable order. This problem is called Traversal and can be accomplished with the following the following algorithm. 1. start at the root and 2. follow all of the left links until you can’t go any farther 3. back-up one node and try going right, if you can repeat steps 2 and 3, if you can’t repeat step 3

10 Node Structure (Static) typedef struct { Element data; Link left Link right; } Node; typedef struct { int numFree; int numInTree; Link free; Link root; Node Nodes[NUMNODES]; } tree;

11 Node Structure (Dynamic) typedef node * Tree; struct Node{ element e; Tree left; Tree right; };

12 Static or Dynamic ? Static –you know the maximum number of nodes not likely to change Dynamic –size of the tree will change frequently –slightly faster than static traversing pointers is faster than calculating addresses for array indexing

13 Traversal A B C 1 2 3 1 2 3 1 2 3 Notice that each node is visited 3 times Were we to print out the node data in the first visit to each node the printout would be : ABC Were we to printout the node data on the second visit to each node the printout would be: BAC Were we to printout the node data on the third visit to each node the printout would be: BCA These are called the: preorder, inorder and postorder traversals respevtively

14 Pre-order Traversal void preorder( tnode * t) { if (t != NULL) { printf (“ %d “, t -> data ); preorder(t -> left); preorder(t -> right); }

15 In-order Traversal void inorder( tnode * t) { if (t != NULL) { inorder(t -> left); printf(“ %d “, t -> data ); inorder(t -> right; }

16 Post-order Traversal void postorder( tnode * t) { if (t != NULL) { postorder(t -> left); postorder(t -> right); printf(“ %d “, t -> data ); }

17 Notice that... the first node visited on the pre-order traversal ia always the root the left-most node in the tree is the first node on the inorder traversal the last node visited on the inorder traversal is the rightmost node in the tree the last node visited on the postorder traversal is the root Knowing this and given any two traversal paths the tree can be constructed…….

18 Armed with this information… We should be able to construct the tree that produced any pair of traversal paths

19 Insertion Tree insert(Tree p, int v) { if (p == NULL) { p = (Tree) malloc(sizeof(struct Node)); p->data = v; p->right = NULL; p->left = NULL; } else if (v data) p->left = insert(p->left,v); else if (v > p->data) p->right = insert(p->right,v); return p; };

20 search Tree search(Tree p, int v) { if ((p == NULL) || (v == p->data)) { printf("Found\n"); return p; } if (v data) return search(p->left,v); else return search(p->right,v); };

21 Note: –If the tree is being used to represent a set the search function would be better named isMember( )

22 Priority Queue BST could be used as a Priority Queue –Needs functions: insert( ) findMin( ) removeMin( ) or findMax( ) removeMax( ) Node with minimum priority is leftmost node Node with maximum priority is rightmost node

23 Deletion Three cases we need to consider: –V has no children Remove v –V has one child Swap v with child and do 1 –V has 2 children Swap v with successor –leftmost node in right subtree Do case 1 or case 2 to delete

24 Deletion 20 10 5 15 12 25 30

25 Heaps The heap property: –max heap: the largest key is at the root; at each node the keys of the children must be less than the key of the parent –min heap: the smallest key is at the root; the keys of the children must be greater than the key of the parent –used as a priority queue; and the basis for heap sort

26 Visualize a one dimensional array as a tree as follows (the array contains the keys) 01234560123456 1 0 2 3 456

27 To Find the child or parent i L = (2*i P )+1 i R = (2*i P )+2 i P = (i C -1)/2

28 Adding a Key Tree is complete (i.e. it fills up in ascending index positions) Must keep track of where end of tree currently is Insert new key at end of tree, then bubble it up to it proper location by comparing to the parent’s key and swapping if necessary This gives O(log 2 n) for each add.

29 Deleting a key Swap the last key with the root and remove the last key Recursivly push the root down to its proper level by comparing it to its children and swapping with smallest child This gives O(log 2 n) for each deletion

30 Insuring O(log 2 n) performance The main problem with BST is that its hard to insure optimum performance. –shape of the tree is very dependent on the order the keys were inserted in trees tend to degenerate to varying degrees

31 The solution - Self-balancing trees AVL Trees Red/Black Trees 2-3 trees 2-3-4 trees B-Trees

32 AVL Trees Height balanced –at every node the allowable difference in height between the right and left subtree is one –an additional piece of data is required balance factor –value = 0 : right and left subtrees are same height –value = 1 : right subtree is one higher –value = -1 : left subtree is one higher –value = 2 or -2 : tree need to be rebalanced

33 AVL Method Insertion is pretty much like a BST recursive insertion but on the way out (unwinding the recursion) check and adjust the balance factors, if a difference of 2 or -2 is found rebalance the tree by doing an RR, LL, RL or LR rotation

34 Out of Balance Conditions insertion into left subtree of left child of x called LL (mirror image of RR) fix with a rotateRight insertion into right subtree of right child of x called RR (mirror image of LL) fix with a rotateLeft insertion into left subtree of right child of x called LR (mirror image of RL) fix with a rotateLeft, rotateRight insertion into right subtree of left child of x called RL (mirror image of LR) fix with a rotateRight, rotateLeft

35 LL k1 k2 k1 A A A B BB C C C Node * rotateLeft(Node * k2) { Node * k1 = k2->left; k2 ->left = k1->right; k1->right = k2; return k1 }

36 LR k2 k1 A k3 B C D k1 k3 k2 A BC D Node * DoubleRotateLeft( Node * k3) { k3->left = RotateLeft(k3->left); return RotateRight(k3); } k1 k2 A B C k3 D


Download ppt "Trees CS-212 Dick Steflik. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root."

Similar presentations


Ads by Google