Announcements – HW2 available, due 10/04, 11:59p. MT1 10/10, 7-9p. Trees: “… most important nonlinear structure[s] in computer science.” -- Donald Knuth, Art of Computer Programming Vol 1 A tree: _____________________________________ We’ll study more specific trees:
Tree terminology: • One of the vertices is called the root of the tree. Guess which one it is. • Make an English word containing the names of the vertices that have a parent but no sibling. • How many parents does each vertex have? • Which vertex has the fewest children? • Which vertex has the most ancestors? descendants? • What is d’s depth? What is d’s height? • List all the vertices is b’s left subtree. • List all the leaves in the tree.
Tree terminology: (for your reference) • root: the single node with no parent • leaf: a node with no children • child: a node pointed to by me • parent: the node that points to me • sibling: another child of my parent • ancestor: my parent or my parent’s ancestor • descendent: my child or my child’s descendent • subtree: a node and its descendents • depth of node x: number of edges on path from root to x. • height of node x: number of edges on longest path from x to a leaf.
A rooted tree:
Branching: d-ary trees ( binary if d = 2) A d-ary tree T is either • OR • Full d-ary tree: Perfect d-ary tree: Complete d-ary tree:
Binary Tree Height height(r) -- length of longest path from root r to a leaf Given a binary tree T, write a recursive defn of the height of T, height(T): Number of nodes in a perfect tree of height h, N(h):
Rooted, directed, ordered, binary trees 1 template<class T> 2 class tree { 3 public: 4 … 5 private: Tree ADT: 6 struct Node { 7 T data; 8 Node * left; insert 9 Node * left; 10 }; remove 11 Node * root; 12 … traverse 13 };
Theorem: Our implementation of an n item binary tree has ______ null pointers.
Recommend
More recommend