H-SC COMS 262 - Lecture 37 - Binary Tree Implementation

Unformatted text preview:

Binary Tree ImplementationTopicsBinary Tree ConstructorsBinary Tree DestructorBinary Tree InspectorsBinary Tree MutatorsBinary Tree FacilitatorsBinary Tree OperatorsBinary Tree Traversal FunctionsOther Binary Tree FunctionsArray Implementation of a Binary TreeArray ImplementationSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Advantages of the Array ImplementationLinked Implementation of a Binary TreeBinary Tree NodesSlide 23Linked Binary Tree Implementation01/13/19Binary Trees 1Binary Tree ImplementationLecture 37Wed, Apr 26, 200601/13/19Binary Trees 2TopicsArray implementationLinked implementationBinary tree nodesImplementation of search()Implementation of draw()01/13/19Binary Trees 3Binary Tree ConstructorsBinaryTree();BinaryTree(const T& value);BinaryTree(const BinaryTree& lft, const BinaryTree& rgt);BinaryTree(const T& value, const BinaryTree& lft, const BinaryTree& rgt);BinaryTree(const BinaryTree& tree);01/13/19Binary Trees 4Binary Tree Destructor~BinaryTree();01/13/19Binary Trees 5Binary Tree Inspectorsint size() const;int height() const;bool isEmpty() const;T& rootValue() const;BinaryTree leftSubtree() const;BinaryTree rightSubtree() const;bool isCountBalanced() const;bool isHeightBalanced() const;01/13/19Binary Trees 6Binary Tree Mutatorsvoid makeEmpty();void setRootValue(const T& value);01/13/19Binary Trees 7Binary Tree Facilitatorsvoid input(istream& in);void output(ostream& out);bool isEqual(BinaryTree tree) const;01/13/19Binary Trees 8Binary Tree OperatorsBinaryTree& operator=(const BinaryTree&);istream& operator>>(istream&, BinaryTree&);ostream& operator<<(ostream&, const BinaryTree&);bool operator==(BinaryTree&);01/13/19Binary Trees 9Binary Tree Traversal Functionsvoid preorderTraversal(void (*visit)(BinaryTreeNode*)) const;void inorderTraversal(void (*visit)(BinaryTreeNode*)) const;void postorderTraversal(void (*visit)(BinaryTreeNode*)) const;void levelorderTraversal(void (*visit)(BinaryTreeNode*)) const;01/13/19Binary Trees 10Other Binary Tree FunctionsT* search(const T& value) const;void draw() const;01/13/19Binary Trees 11In an array binary tree, the nodes of the tree are stored in an array.Position 0 is left empty.The root is stored in position 1.For the element in position n,The left child is in position 2n.The right child is in position 2n + 1.For the element in position n,The parent is in position n/2.Array Implementation of a Binary Tree01/13/19Binary Trees 12Array Implementation10 20 30 40 50 60 701 2 3456789001/13/19Binary Trees 13Array Implementation10 20 30 40 50 60 701 2 34567890Unused01/13/19Binary Trees 14Array Implementation10 20 30 40 50 60 701 2 34567890Root01/13/19Binary Trees 15Array Implementation10 20 30 40 50 60 701 2 34567890Parent01/13/19Binary Trees 16Parents, do you know where your children are?Array Implementation10 20 30 40 50 60 701 2 34567890Parent01/13/19Binary Trees 17Yes, they are at 2n and 2n + 1.Array Implementation10 20 30 40 50 60 701 2 34567890ParentChildren01/13/19Binary Trees 18Children, do you know where your parents are?Array Implementation10 20 30 40 50 60 701 2 34567890Children01/13/19Binary Trees 19Yes, he (she) is at  n/2.Or, he (she) is at n >> 1.Array Implementation10 20 30 40 50 60 701 2 34567890ChildrenParent01/13/19Binary Trees 20This representation is very efficient when The tree is full, and The structure of the tree will not be modified. Advantages of the Array Implementation01/13/19Binary Trees 21In a linked binary tree, each node contains pointers to its left and right children. A linked binary tree object has one data member: BinaryTreeNode* root - A pointer to the root node. Linked Implementation of a Binary Tree01/13/19Binary Trees 22Binary Tree NodesA binary tree node has three components. A value (data) that contains the tree element. A left pointer (left) that points to the root node of the left subtree. A right pointer (right) that points to the root node of the right subtree.01/13/19Binary Trees 23Binary Tree NodesNodes are allocated and deallocated dynamically. The binary tree always has allocated the exact number of nodes necessary to store the tree elements. Examplebinarytreenode.h01/13/19Binary Trees 24binarytree.hBinaryTreeTest.cpp Linked Binary Tree


View Full Document

H-SC COMS 262 - Lecture 37 - Binary Tree Implementation

Download Lecture 37 - Binary Tree Implementation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 37 - Binary Tree Implementation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 37 - Binary Tree Implementation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?