Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.

Similar presentations


Presentation on theme: "Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas."— Presentation transcript:

1 Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas and Halloween because Oct31 == Dec25. -Andrew Rutherford

2 Tree tree: directed acyclic data structure of linked nodes
directed - links point in one direction acyclic - no path in the structure leads from a node back to itself binary tree: each node has at most two children A binary tree is recursively defined as: empty/NULL, or a root node that has data a left sub-tree and a right sub-tree parent node has direct links to its child nodes leaf – node with no children internal node – node with 1+ children internal node is parent of its child nodes root 1 3 2 4 5 6 7

3 Tree Properties Typically drawn upside down, with the root at the top and leaves at the bottom One access point: the root All nodes other than the root have one parent siblings: two nodes with the same parent edge: the link from one node to another path length: the number of edges traversed to get from one node to another

4 Tree Properties depth: the path length from the root of the tree to this node height of a node: maximum distance (path length) of any leaf from this node leaf has height of 0 height of a tree: the height of the root of the tree descendants: nodes that can be reached via 1 or more edges from this node ancestors: nodes for which this node is a descendant

5 Example A B C D I J E F G H K L M
Depth of B, C, D: 1 Height of E, F, G, K, L, M, I: 0 Depth of E, F, G, H, I, J: 2 Height of B, C, H, J: 1 Depth of K, L, M: 3 K L M

6 Question In the tree on the previous slide, what's the depth of the node containing M? A. 0 B. 1 C. 2 D. 3 E. 4

7 Full Binary Tree full binary tree: binary tree in which every node has 0 or 2 children

8 Question What's the maximum height of a full binary tree with 11 nodes? A. 1 B. 3 C. 5 D. 7 E. No full binary tree has 11 nodes

9 Complete Binary Tree complete binary tree: a binary tree in which every level, except possibly the deepest, is completely filled. At depth n, the height of the tree, all nodes are as far left as possible If we added a new node, where would it go to maintain a complete binary tree?

10 Question What's the height of a complete binary tree that contains N nodes? A. 1 B. logN C. N1/2 D. N E. NlogN

11 Perfect Binary Tree perfect binary tree: a binary tree such that all leaf nodes are at the same depth and all internal nodes have two children a perfect binary tree has the maximum number of nodes for a given height a perfect binary tree has 2n+1 – 1 nodes, where n is the height of the tree height 0  1 node height 1  3 nodes height 2  7 nodes height 3  15 nodes

12 Tree Node a tree node object stores data and pointers to left and right children left data right 25 left data right 13 left data right 44 left data right 62

13 TreeNode struct // BinaryTree.h #ifndef _BINARYTREE_H #define _BINARYTREE_H struct TreeNode { int data; TreeNode* left; TreeNode* right; TreeNode(int data, TreeNode* left = NULL, TreeNode* right = NULL) { thisdata = data; thisleft = left; thisright = right; } bool isLeaf() { return left == NULL && right == NULL; };

14 Example TreeNode* node = new TreeNode(25); nodeleft = new TreeNode(13); noderight = new TreeNode(44); noderightright = new TreeNode(62);

15 BinaryTree class: interface (cont'd)
class BinaryTree { private: TreeNode* root; // NULL for empty tree public: BinaryTree(TreeNode*); void print(); bool contains(int); ... }; #endif

16 Binary Tree Traversals
Often we need to visit every node in a binary tree and process each node's data Types of traversal: preorder traversal: process root, process left subtree, process right subtree in-order traversal: process left subtree, process root, process right subtree post-order traversal: process left subtree, process right subtree, process root level order traversal: starting at root, process all nodes at the same depth from left to right, and then traverse the nodes at the next depth

17 Binary Tree Traversals
To determine the order that the nodes are visited in a traversal, draw a path around the tree. Start on the left side of the root and trace around the tree, staying close to the tree. pre-order: process when pass down left side of node F B A D C E G I H in-order: process when pass under node A B C D E F G H I post-order: process when pass up right side of node A C E D B H I G F

18 Question What is the result of a post-order traversal this tree?
B C D

19 print Method print() method in BinaryTree class: prints the value in each node during an in-order traversal of the tree print the left subtree, print the root, print the right subtree To implement this recursively, need a helper method that takes a pointer to the root of the tree we're printing Add to BinaryTree.h as a private method: void printHelper(TreeNode* node);

20 print Method In BinaryTree.cpp: void BinaryTree::print() {
printHelper(root); } void BinaryTree::printHelper(TreeNode* node) { // implicit base case: do nothing if node is NULL if(node != NULL) { printHelper(nodeleft); cout << nodedata << endl; printHelper(noderight); Note: We could just use overloading and name both functions print

21 Tree Methods Tree methods are often implemented recursively in this way: public method: called by the client helper method: takes a pointer to the node it should process in addition to parameters of public method the public method often only calls the helper method and passes the root node returnType methodName(parameters) { helperMethod(root, parameters); } returnType helperMethod(node, parameters) { ...

22 Exercise Write the contains method for BinaryTree class


Download ppt "Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas."

Similar presentations


Ads by Google