Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 8: Trees. cis 335 Fall 2001 Barry Cohen Definitions n A tree is a nonlinear hierarchical structure (more than one successor) n Consists of nodes.

Similar presentations


Presentation on theme: "Class 8: Trees. cis 335 Fall 2001 Barry Cohen Definitions n A tree is a nonlinear hierarchical structure (more than one successor) n Consists of nodes."— Presentation transcript:

1 Class 8: Trees

2 cis 335 Fall 2001 Barry Cohen Definitions n A tree is a nonlinear hierarchical structure (more than one successor) n Consists of nodes (vertices) and edges n Parent, child and sibling relations parent left child right child

3 cis 335 Fall 2001 Barry Cohen Not all hierarchies are trees n A tree must satisfy these conditions: * At most one node -- the root -- has no ancestor * Every other node has exactly one parent n Each edge has a direction (from parent to child) n A parent may have multiple children n Therefore, a tree is connected

4 cis 335 Fall 2001 Barry Cohen More definitions n Root -- node with no parent n Leaf -- node with no children n Ancestor, descendant -- generalization of parent-child relationship n Subtree -- a node and its descendants n Binary tree -- each node has 0, 1 or 2 children

5 cis 335 Fall 2001 Barry Cohen Things which are not trees n Directed acyclic graph (dag). Node may have multiple parents.

6 cis 335 Fall 2001 Barry Cohen Geneological trees n Can the family’s parent-child relation be the same as the tree’s? Why (or why not)? n Can the family parent-child relation be inverted? Why or why not? n What about the matrilineal line? n Other hierarchical relations? Organization chart. Algebraic expression.

7 cis 335 Fall 2001 Barry Cohen Height and level n Height -- number of nodes on longest path n Level -- number of nodes on path to root n Full binary tree -- every leaf on same level n If there are n nodes in a full binary tree, now many levels are there?

8 cis 335 Fall 2001 Barry Cohen Binary search tree n Each node has a value n Each node has value greater than its left child n Each node has a value less than its right child

9 cis 335 Fall 2001 Barry Cohen Binary tree ops n createBT() n destroyBT() n bool isEmpty() n btType getRootData() n setRootData(item:btType) n attachLeft(item:btType) n attachRight(item:btType) n attachLeftTree(item:bt) n attachRightTree(item:bt) n bt detachLeftTree() n bt detachRightTree()

10 cis 335 Fall 2001 Barry Cohen Preorder traversal Preorder(binTree:BinaryTree) if (binTree not empty) { visit root preorder(left subtree) preorder(right subtree) }

11 cis 335 Fall 2001 Barry Cohen Inorder traversal Inorder(binTree:BinaryTree) if (binTree not empty) { inorder(left subtree) visit root inorder(right subtree) }

12 cis 335 Fall 2001 Barry Cohen Postorder traversal Postorder(binTree:BinaryTr ee) if (binTree not empty) { postorder(left subtree) postorder(right subtree) visit root }


Download ppt "Class 8: Trees. cis 335 Fall 2001 Barry Cohen Definitions n A tree is a nonlinear hierarchical structure (more than one successor) n Consists of nodes."

Similar presentations


Ads by Google