Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees
Topics Binary trees Array implementation Linked implementation Binary tree nodes 9/21/2018 Binary Trees
Binary Trees A binary tree is a data structure with the following properties. It is either empty or it has a root node. Each node in the binary tree may have up to two children (called left and right). Each node, except the root node, has exactly one parent. The root node has no parent. 9/21/2018 Binary Trees
A Binary Tree 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Terminology The tree metaphor The family metaphor tree, root, branch, leaf. The family metaphor parent, child, sibling, ancestor, descendant. 9/21/2018 Binary Trees
Tree Terminology 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Tree Terminology Root 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Tree Terminology 10 20 30 40 50 60 70 Parent 9/21/2018 Binary Trees
Tree Terminology 10 20 30 40 50 60 70 Leaves 9/21/2018 Binary Trees
Tree Terminology 10 20 30 40 50 60 70 Right Child 9/21/2018 Binary Trees
Tree Terminology Subtree 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Array Implementation of a Binary Tree 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. The parent is in position n/2. 9/21/2018 Binary Trees
Array Implementation 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 9/21/2018 Binary Trees
Array Implementation Unused 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 Binary Trees
Array Implementation Root 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 Binary Trees
Array Implementation Parent 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 Binary Trees
Array Implementation Parents, do you know where your children are? 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 Parents, do you know where your children are? 9/21/2018 Binary Trees
Array Implementation Parents, do you know where your children are? 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 Parents, do you know where your children are? 9/21/2018 Binary Trees
Advantages of the Array Implementation This representation is very efficient when The tree is full, and The structure of the tree will not be modified. 9/21/2018 Binary Trees
Linked Implementation of a Binary Tree 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. 9/21/2018 Binary Trees
Binary 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. 9/21/2018 Binary Trees
Binary 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.h 9/21/2018 Binary Trees
Binary Tree Implementation Lecture 37 Thu, Apr 22, 2004 9/21/2018 Binary Trees
Topics Binary tree interface Implementation of Search() Implementation of Draw() 9/21/2018 Binary Trees
Binary Tree Constructors BinaryTree(const T& value); BinaryTree(const BinaryTree& lft, const BinaryTree& rgt); BinaryTree(const T& value, const BinaryTree& lft, BinaryTree(const BinaryTree& tree); 9/21/2018 Binary Trees
Binary Tree Destructor 9/21/2018 Binary Trees
Binary Tree Inspectors int Size() const; int Height() const; bool Empty() const; T& Root() const; BinaryTree LeftSubtree() const; BinaryTree RightSubtree() const; bool IsCountBalanced() const; bool IsHeightBalanced() const; 9/21/2018 Binary Trees
Binary Tree Mutators void MakeEmpty(); void SetRoot(const T& value); 9/21/2018 Binary Trees
Binary Tree Facilitators void Input(istream& in); void Output(ostream& out); bool Equal(BinaryTree tree) const; 9/21/2018 Binary Trees
Binary Tree Operators BinaryTree& operator=(const BinaryTree&); istream& operator>>(istream&, BinaryTree&); ostream& operator<<(ostream&, const BinaryTree&); bool operator==(BinaryTree&); 9/21/2018 Binary Trees
Binary Tree Traversal Functions void PreorderTraversal(void (* Visit) (BinaryTreeNode*)) const; void InorderTraversal(void (* Visit) void PostorderTraversal(void (* Visit) void LevelorderTraversal(void (* Visit) 9/21/2018 Binary Trees
Other Binary Tree Functions T* Search(const T& value) const; void Draw() const; 9/21/2018 Binary Trees
Linked Binary Tree Implementation Example binarytree.h BinaryTreeTest.cpp 9/21/2018 Binary Trees
Binary Tree Traversals Lecture 38 Mon, Apr 26, 2004 9/21/2018 Binary Trees
Topics Binary tree traversals Binary tree iterators Pre-order traversals In-order traversals Post-order traversals Level-order traversals Binary tree iterators Pre-order iterators In-order iterators Post-order iterators Level-order iterators 9/21/2018 Binary Trees
Binary Tree Traversals Four Standard Traversals Pre-order Traversal. In-order Traversal. Post-order Traversal. Level-order Traversal. 9/21/2018 Binary Trees
Pre-order Traversal At each node, Also called an NLR traversal. First visit the node, Then perform a pre-order traversal of the left subtree, Then perform a pre-order traversal of the right subtree. Also called an NLR traversal. Node - Left - Right. 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees
Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Pre-Order Traversal Order: 10, 20, 40, 50, 30, 60, 70 10 20 30 40 50 9/21/2018 Binary Trees
In-order Traversal At each node, Also called an LNR traversal. First, perform an in-order traversal of the left subtree, Then visit the node, Then perform an in-order traversal of the right subtree. Also called an LNR traversal. Left - Node - Right. 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 40 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 20 40 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 20 40 50 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 60 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees
In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees
In-Order Traversal Order: 40, 20, 50, 10, 60, 30, 70 10 20 30 40 50 60 9/21/2018 Binary Trees
Post-order Traversal At each node, Also called an LRN traversal. First perform a post-order traversal of the left subtree, Then perform a post-order traversal of the right subtree, Then visit the node. Also called an LRN traversal. Left - Right - Node. 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 40 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 40 50 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 70 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 20 30 40 50 60 70 9/21/2018 Binary Trees
Post-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Post-Order Traversal Order: 40, 50, 20, 60, 70, 30, 10 10 20 30 40 50 9/21/2018 Binary Trees
Level-order Traversal Traverse the levels of the tree from top to bottom. Within each level, visit the nodes from left to right. 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees
Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 Order: 10, 20, 30, 40, 50, 60, 70 9/21/2018 Binary Trees
Binary Tree Iterators A binary tree iterator systematically visits each node of a binary tree. The four standard iterators correspond to the four standard traversal methods. Pre-order iterator In-order iterator Post-order iterator Level-order iterator 9/21/2018 Binary Trees
Pre-order Iterator Visit the nodes in a pre-order traversal. Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreepreorderiterator.h 9/21/2018 Binary Trees
Pre-order Iterator 10 20 30 40 50 60 70 10 10 10 20 20 20 30 30 30 40 50 60 70 9/21/2018 Binary Trees
In-order Iterator Visit the nodes in an in-order traversal. Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreeinorderiterator.h 9/21/2018 Binary Trees
In-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 9/21/2018 Binary Trees
Post-order Iterator Visit the nodes in a post-order traversal. Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreepostorderiterator.h 9/21/2018 Binary Trees
Post-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 9/21/2018 Binary Trees
Level-order Iterator Visit the nodes in a level-order traversal. Use a queue to store unvisited nodes. 9/21/2018 Binary Trees
Level-order Iterator 10 20 30 40 50 60 70 10 20 20 30 30 40 40 50 50 60 60 70 70 9/21/2018 Binary Trees
Traversals and Expression Trees Perform an in-order traversal of the expression tree and print the nodes. * + - 4 5 6 3 9/21/2018 Binary Trees
Traversals and Expression Trees Perform a post-order traversal to evaluate the expression tree. * + - 4 5 6 3 9/21/2018 Binary Trees
Binary Search Trees Lecture 39 9/21/2018 Binary Trees
Topics Binary search trees Inserting a node Deleting a node Balancing a binary search tree 9/21/2018 Binary Trees
Binary Search Trees A binary search tree is a binary tree with the following properties. There is a total order relation on the members in the tree. At every node, every member of the left subtree is less than or equal to the node value. At every node, every member of the right subtree is greater than or equal to the node value. 9/21/2018 Binary Trees
BinarySearchTree Implementation The BinarySearchTree class is implemented as a subclass of the BinaryTree class. 9/21/2018 Binary Trees
Binary Search Tree Interface Mutators void Insert(const T& value); void Delete(const T& value); Other member functions T* Search(const T& value) const; void CountBalance(); 9/21/2018 Binary Trees
Searching a BinarySearchTree Begin at the root node. Apply the following algorithm recursively. Compare the value to the node data. If it is equal, you are done. If it is less, search the left subtree. If it is greater, search the right subtree. If the subtree is empty, the value is not in the tree. 9/21/2018 Binary Trees
Inserting a Value into a BinarySearchTree Begin at the root node. Apply the following algorithm recursively. Compare the value to the node data. If it is less (or equal), continue with the left subtree. If is is greater, continue with the right subtree. When the subtree is empty, attach the node as a subtree. 9/21/2018 Binary Trees
Deleting a Value from a BinarySearchTree Perform a search to locate the value. This node has Two children. One child. No child. 9/21/2018 Binary Trees
Deleting a Value from a BinarySearchTree Case 1: The node has no child. Delete the node. Case 2: The node has one child. Replace the node with the subtree of which the child is the root. 9/21/2018 Binary Trees
Deleting a Value from a BinarySearchTree Case 3: The node has two children. Locate the next smaller value in the tree. This value is the rightmost value of the left subtree. Move left one step. Move right as far as possible. Swap this value with the value to be deleted. The node to be deleted now has at most one child. 9/21/2018 Binary Trees
Count-Balancing a BinarySearchTree Write a function MoveNodeRight() that will move the largest value of the left subtree to the right subtree. Locate the largest value in the left subtree. Delete it (but save the value). Place it at the root. Insert the old root value into the right subtree. 9/21/2018 Binary Trees
Count-Balancing a BinarySearchTree Write a similar function MoveNodeLeft(). Apply either MoveNodeRight() or MoveNodeLeft() repeatedly at the root node until the tree is balanced at the root. Then apply these functions recursively, down to the leaves. 9/21/2018 Binary Trees
BinarySearchTree Implementation Example binarysearchtree.h BinarySearchTreeTest.cpp 9/21/2018 Binary Trees