Presentation is loading. Please wait.

Presentation is loading. Please wait.

Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.

Similar presentations


Presentation on theme: "Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting."— Presentation transcript:

1 Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting trees: k-ary ↔ binary

2 Nodes Edges Root node Interior node Leaf Parent Children Ancestor / Proper ancestor Descendant / Proper descendant Level of a node Height of a node / tree Degree of a node / tree Depth of node Path Acyclic graph

3 bj c d g k m l i hfe a root, height=4, depth=level=0 leaf, height=0, depth=level=4 degree=2 interior node, height=2, depth=level=2 degree of tree = 2 height of tree = 4 descendants of “a” proper descendants of “a” degree=0 degree=1

4 Implementing a Tree Nodes and Links A BC D ▲ ▲ ▲ ▲▲ Node { Object value; Node lchild; Node rchild; } // Node ▲ =null link

5 Implementing a Tree One array A BC D ▲ ▲ ▲ ▲▲ A: 0 1 2 3 4 5 6 7 8 9 - A B C - - D - - - ▲ =null link A[1] is root lchild of A[i] is A[2i] rchild of A[i] is A[2i+1] “-” means array element is null / not used A[0] not used as a node A[0] may be used to hold general info (e.g., number of nodes in tree)

6 Preorder N L R preorder (Node t) if (t == null) return; visit (t.value()); preorder (t.lchild()); preorder (t.rchild()); } // preorder

7 Traversals inorder (Node t) if (t == null) return; inorder (t.lchild()); visit (t.value()); inorder (t.rchild()); } // inorder Inorder L N R

8 Traversals postorder (Node t) if (t == null) return; postorder (t.lchild()); postorder (t.rchild()); visit (t.value()); } // postorder Postorder L R N

9 bj c d g k m l i hfe a preorder: a j k m l b c g i h d f e inorder: m k l j a b i g h c f d e postorder: m l k j i h g f e d c b a

10 n g e jk m d c b f i p q a degree of tree = 4 degree of nodes f and n = 3 height of tree = 3 depth=level of m = 2

11 K-ary Tree => Binary Tree n g e jk m d c b f i p q a K-aryBinaryroot leftmost childleft child right siblingright child

12 n g e jk m d c b f i p q a n g e jk m d c b f i p q a K-ary TreeBinary Tree Preorder: Inorder: Postorder: Preorder: Inorder: Postorder:

13

14 Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree are larger than items in any node

15 Items must be comparable All items have a unique value Given two distinct items x and y either value(x) < value(y) value(x) > value(y) If value(x) = value(y) then x = y It will simplify programming to assume there are no duplicates in our set of items.

16 Need to map Items to a numerical value Integers Value(x) = x People Value(x) = ssn Value(x) = student id

17

18 Constructor Insert Find Findmin Findmax Remove

19 Generally Recursive BinaryNode operation( Comparable x, BinaryNode t ) { // End of path if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return operation( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return operation( x, t.right ); else return t; // Match }

20 private BinaryNode find( Comparable x, BinaryNode t ) { if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); else return t; // Match }

21 Remove Node is leaf Remove node Node has one child Replace node with child Node has two children Replace node with smallest child of right subtree.

22 private BinaryNode remove( Comparable x, BinaryNode t ) { if( t == null ) return t; // Item not found; do nothing if( x.compareTo( t.element ) < 0 ) t.left = remove( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return t; }

23 Review depth/height Depth Depth is number of path segments from root to node Depth of node is distance from root to that node. Depth is unique Depth of root is 0

24 Height Height is maximum distance from node to a leaf. There can be many paths from a node to a leaf. The height of the tree is another way of saying height of the root.

25 IPL is the sum of the depths of all the nodes in a tree It gives a measure of how well balanced the tree is.

26 11 2 N = 4 IPL = 1 + 1 + 2 = 4

27 1 2 N = 4 IPL = 1 + 2 + 3 = 6 3

28 Calculate IPL of all possible trees 1 2 3 11 2 1 22

29 Simple to understand Works for small datasets Basis for more complicated trees Using inheritance can implement AVL trees Splay trees Red Black trees


Download ppt "Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting."

Similar presentations


Ads by Google