170 likes | 187 Views
This article provides an introduction to trees, binary trees, array and dynamic implementations of binary trees, binary search trees, and tree traversals. Includes examples and explanations.
E N D
Department of Computer and Information Science,School of Science, IUPUI CSCI 240 Trees Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu
A B G C D E F I M H J L N K Q P Trees • Definitions • Tree: A graph is defined to be a collection of nodes connected by edges. A tree is an acyclic graph. If the collection is not empty, it must consist of a unique root node, r, and zero or more nonempty subtrees T1, T2,…, Tk, with roots connected by a direct edge from r • For a tree of N nodes, there must be N-1 edges. • Path: a path from node n1 to nk is a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 (1 i k); The no. of edges in the path is called the length of the path; A length 0 path is a path from a node to itself. There is a unique path from root to any node ni; The length of this path is called the depth of ni; thus, the root is a depth 0 node. • The height of a tree is the height of its root node. The depth of a tree is the depth of the deepest leaf node, which is the same as the height of the tree.
Trees • Height – deepest level, not including root. • Leaf nodes – have no children • Interior Notes – have at least one child • Degree of a node – Number of children • In a binary tree, interior nodes have degree 1 or 2.
Binary Trees • Binary trees • Definition: A binary tree is a tree in which no nodes can have more than two children. (That is, the degree of interior nodes is restricted to 1 or 2.) • The root node is the first node in a tree. • Each link in the root node refers to a child • A node with no children is called a leaf node • Total number of nodes in a full binary tree of height k is 2k-1
Array implementation of binary tree We can embed the nodes of a binary tree into a one-dimensional array by defining a relationship between the position of each parent node and the position of its children. 1. left_child of node i is 2*i 2. right_child of node i is 2*i+1 3. parent of node i is i/2 (integer division) How much space is required for the array to represent a tree of depth d? 2d+1 – 1 (must assume full tree)
Dynamic Implementation of Binary Tree A dynamic implementation of a binary tree uses space proportional to the actual number of nodes used. Not the size of the full tree. Struct BinaryNode { data_type element; BinaryNode left; BinaryNode right; }
47 25 77 555 555 555 555 555 555 555 555 501 701 501 701 501 701 501 501 501 701 701 701 11 43 65 93 501 501 701 358 358 513 358 513 561 358 358 358 513 513 513 561 561 561 797 797 797 7 17 31 44 68 345 490 490 724 345 Binary Search Tree (BST) • Binary search tree (BST) • Values in left subtree less than parent • Values in right subtree greater than parent • Facilitates duplicate elimination • Fast searches - for a balanced tree, maximum of log2(n) comparisons Example: Construct an Binary Search Tree (BST) for the data 555 501 701 358 513 561 797 345 490 724 Array Implementation: 555 555 501 701 358 513 561 797 345 490 724
Tree Traversals • Tree traversals: • In-order traversal – prints the node values in ascending order (LNR) 1. Traverse the left sub-tree with an in-order traversal 2. Process the value in the node (i.e., print the node value) 3. Traverse the right sub-tree with an in-order traversal • Pre-order traversal (NLR) 1. Process the value in the node 2. Traverse the left sub-tree with a preorder traversal 3. Traverse the right sub-tree with a preorder traversal • Post-order traversal (LRN) 1. Traverse the left sub-tree with a post-order traversal 2. Traverse the right sub-tree with a post-order traversal 3. Process the value in the node
Tree Traversal is “Naturally” recursive • Naturally recursive because: • Subgraphs are similar in character to the original graph. • Subgraphs are smaller. • Guaranteed to eventually hit a leaf (acyclic is assumed) Obeys fundamental rules of recursion 1.) Base cases 2.) Making progress through recursion 3.) Design rule: assuming all recursive call work (details hidden) 4.) Compound interest rule: do not duplicate recursive calls Always specify the base case; otherwise, indefinite recursive will occur and cause “stack-overflow” error.
Sample recursive programs Post-order: (LRN) void postorder(tree_pointer ptr) { if (ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf(“%d”, ptr->data); } } In-order: (LNR) void inorder(tree_pointer ptr) { if (ptr) { inorder(ptr->left_child); printf(“%d”, ptr->data); inorder(ptr->right_child); } } Pre-order: (NLR) void preorder(tree_pointer ptr) { if (ptr) { printf(“%d”, ptr->data); preorder(ptr->left_child); preorder(ptr->right_child); } }
+ - * A B / C F E Example: Expression Tree Example: An Expression Tree • Perform in-order traversal (LNR) A – B + C * E / F • Perform post-order traversal (LRN) A B – C E F / * + • Perform pre-order traversal (NLR) + - A B * C / E F
Tree Traversal Example Try your hand at traversing this tree: In-order: (LNR) Pre-order: (NLR) Post-order: (LRN) Apr, Aug, Dec, Feb, Jan, Jun, Jan, Mar, May, Nov, Oct, Sep Mar, Apr, Jul, Feb, Dec, Aug, Jun, Jan, May, Sep, Oct, Nov Aug, Dec, Jan, Jun, Feb, Jul, Apr, Nov, Oct, Sep, May, Mar
Adjacency List • We need a computer representation for a graph if we want to use the computer to solve graph problems. • One representation is to list the nodes, and then build a list of connections from each node. • Notice that in this graph, connections are not directional. If A is connected to B, then B is connected to A.
Adjacency Matrix • For a graph G with n vertices we create an n x n boolean (or equivalent) matrix. • Label the row and columns of the matrix with the names of the vertices. • Each element of the matrix represents a potential edge in the graph as defined by its associated vertex pair. • We set an element of the matrix to TRUE if there is an edge connecting the two corresponding vertices. • Notice that the matrix is symmetric along diagonal. • Connections can be give weights to represent the cost or distance between nodes. What’s a problem with this approach?
Dynamic Adjacency Matrix • A dynamic list can be used to represent the actual number of connections in the graph. Not the n x n worst case. What problem does this approach have that the matrix approach does not?
Summary • We have discussed how to represent trees in a computer system. • Binary Trees restricted to degree <= 2. • Unrestricted tree that are acyclic graphs. • Unrestricted graphs that use adjacency information to represent path.
Acknowledgements • Horowitz and Sahni. Data Structures in C++ • Bob Pilgram. Murray State University.