Presentation is loading. Please wait.

Presentation is loading. Please wait.

Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.

Similar presentations


Presentation on theme: "Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies."— Presentation transcript:

1 Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies razdan@asu.edu http://dcst2.east.asu.edu/~razdan/cst230/

2 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

3 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)

4 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

5 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;... }

6 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()

7 Razdan CST2307 BTNode does most of the work BTNode( Object, BTNode, BTNode ) getData, getLeft, getRight setData, setLeft, setRight isLeaf() clone() equals() subTreeContains( Object ) subTreeSize()

8 Razdan CST2308 Traversals Pre-order traversal –process root –pre-order traverse left subtree –pre-order traverse right subtree

9 Razdan CST2309 Traversals cont... In-order Traversal –in-order traverse left subtree –process root –in-order traverse right subtree

10 Razdan CST23010 Traversals cont... Post-order Traversal –post-order traverse left subtree –post-order traverse right subtree –process root


Download ppt "Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies."

Similar presentations


Ads by Google