200 likes | 258 Views
Chapter 8. Binary and Other Trees Part 2. The ADT BinaryTree. The Class BinaryTree. The Class BinaryTree (continue). The Class BinaryTree (continue). Root. MakeTree. BreakTree. PreOrder. InOrder. PostOrder. LevelOrder. An Application. ADT extensions.
E N D
Chapter 8 Binary and Other Trees Part 2
ADT extensions • PreOutput(): output the data fields in preorder • InOutput(): output the data fields in inorder • PostOutput(): output the data fields in postorder • LevelOutput(): output the data fields in level order • Delete(): delete a binary tree, freeing up its nodes • Height(): return the tree height • Size(): return the number of nodes in the tree
OutputTime complexity: Θ(n) each static void Output(BinaryTreeNode<T> *t) {cout << t->data << ‘ ‘;} void PreOutput() {PreOrder(Output, root); cout << endl;} void InOutput() {InOrder(Output, root); cout << endl;} void PostOutput() {PostOrder(Output, root); cout << endl;} void LevelOutput() {LevelOrder(Output, root); cout << endl;}
DeleteTime complexity: Θ(n) static void Free(BinaryTreeNode<T> *t) {delete t;} void Delete() {PostOrder(Free, root); root = 0;}
Height • public: int Height() const {return Height(root);} • private: int Height(BinaryTreeNode<T> *t) const;
SizeTime complexity: Θ(n) • outside the class definition int _count; • public: int Size() {_count = 0; PreOrder(Addl, root); return _count;} • private: static void Addl(BinaryTreeNode *t) {_count++;}