Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies
Razdan CST2302 Tree a “non-linear” data structure Example: family “tree” tree is made up of nodes and edges Trees can be rooted (i.e., have a root node) trees have branches and leaves
Razdan CST2303 Binary Trees A binary tree is a finite (possibly emtpy) set of nodes. If the tree is not empty: –there is one special node, called the root –each node has a left child and right child –each node except the root has exactly one parent (the root has no parent)
Razdan CST2304 Vocabulary Parent Sibling Ancestor Descendant Subtree Left and Right subtrees of a Node Depth of a node leaf Depth of a tree Full Binary tree Complete Binary tree
Razdan CST2305 Tree Representations Two general methods for representing trees: –Arrays – Parent [(i-1)/2], LC[2i+1], RC[2i+2] –linked structure A complete binary tree can be represented using an array. Linked structure for binary trees uses A Node that contains data and two references: class BTNode{ private Object data; private BTNode left; private BTNode right;... }
Razdan CST2306 Example Binary Tree methods iterate boolean isEmpty() boolean equals() Object clone() merge( Object, BinaryTree, BinaryTree ) boolean contains( Object ) void remove( Object ) int size()
Razdan CST2307 BTNode does most of the work BTNode( Object, BTNode, BTNode ) getData, getLeft, getRight setData, setLeft, setRight isLeaf() clone() equals() subTreeContains( Object ) subTreeSize()
Razdan CST2308 Traversals Pre-order traversal –process root –pre-order traverse left subtree –pre-order traverse right subtree
Razdan CST2309 Traversals cont... In-order Traversal –in-order traverse left subtree –process root –in-order traverse right subtree
Razdan CST23010 Traversals cont... Post-order Traversal –post-order traverse left subtree –post-order traverse right subtree –process root