Docsity

Techniques for Defining a Tree: Recursive and Non-Recursive Approaches, Study notes of Computer Science

Toggle sidebar
bg1
COP 3503 – Computer Science II – CLASS NOTES - DAY #22
TREES
Techniques for defining a tree
There are two basic techniques that can be used to define a tree.
1. Recursively : this allows for very simple algorithms to manipulate the tree.
2. Non-recursively : this allows for very straight-forward, easy and direct
algorithms for manipulation of the tree. We did this in Chapter 6/7.
Non-recursive Definition
Tree: A tree is a set of nodes and a set of directed edges which connect pairs of
nodes. It is a connected, cycle-free, directed graph.
Rooted Tree: A tree in which one node is specified to be the root, (call it node c).
Every node (other than c), call it b is connected by exactly one edge
to exactly one other node, call it p. Given this situation, p is b’s
parent. Further, b is one of p’s children.
Path length: A unique path traverses from the root to each node in the tree. The
number of edges along the path is the path length.
Height: The number of edges from a specific node to its deepest leaf.
Depth: The number of edges from the root to a specific node.
Siblings: Nodes which have the same parent.
Descendant: If there is a path from node a to node b then b is a descendant of a
and a is an ancestor of b.
Day 22 - 1
Note: A tree with n nodes has exactly n-1 edges since all nodes in the tree,
except for the root, have exactly one edge leading to it.
Chapter 17
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Techniques for Defining a Tree: Recursive and Non-Recursive Approaches and more Study notes Computer Science in PDF only on Docsity!

COP 3503 – Computer Science II – CLASS NOTES - DAY # TREES Techniques for defining a tree There are two basic techniques that can be used to define a tree.

  1. Recursively: this allows for very simple algorithms to manipulate the tree.
  2. Non-recursively: this allows for very straight-forward, easy and direct algorithms for manipulation of the tree. We did this in Chapter 6/7. Non-recursive Definition Tree: A tree is a set of nodes and a set of directed edges which connect pairs of nodes. It is a connected, cycle-free, directed graph. Rooted Tree: A tree in which one node is specified to be the root , (call it node c ). Every node (other than c ), call it b is connected by exactly one edge to exactly one other node, call it p. Given this situation, p is b ’s parent. Further, b is one of p ’s children. Path length: A unique path traverses from the root to each node in the tree. The number of edges along the path is the path length. Height: The number of edges from a specific node to its deepest leaf. Depth: The number of edges from the root to a specific node. Siblings: Nodes which have the same parent. Descendant: If there is a path from node a to node b then b is a descendant of a and a is an ancestor of b. Note: A tree with n nodes has exactly n-1 edges since all nodes in the tree, except for the root, have exactly one edge leading to it. Chapter 17

Proper descendant: If b is a descendant of a and ab then b is a proper descendant of a and a is a proper ancestor of b. The following diagrams illustrate the various properties of trees. … … There are x total nodes and thus x-1 total edges in the tree shown above. The depth of the tree to node c 1 is 2. Depth(1) = 1. Height(3) = 2, Height(2) = 0 Depth(root) = 0, Height(root) = 3 Node 3 is an ancestor of node 7. Node7 is a proper descendant of node 3. Nodes 4 and 5 are siblings. c b 0 b 1 b n c 0 c 1 c m m m root c 1 2 3 4 5 6 7

Given the conventional tree: This corresponds to the following tree using the first child/next sibling technique. Null pointers are shown in the example above for completeness only, typically they are omitted for clarity.

F

roo t A B C D E

G

F

H

G

F

I

F

J

H

F

roo t A B C D G F

H G F J H F

I

E F F

E

Java Implementation public class TreeNode { protected Object data; protected Singly_Linked_List children; protected TreeNode sibling; private TreeNode ( ) { } //defeat improper instantiation public TreeNode (Object x, TreeNode, r) { data = x; sibling = r; children = null; //children = new Singly_Linked_List(null) } //end constructor r null public TreeNode (Object x, Singly_Linked_List sll, TreeNode r) { data = x; children = sll; sibling = r; } //end constructor r header null data children sibling x data children sibling data next null data children sibling x data children sibling

Binary Trees  A special case of a general tree in which each node can have at most two children. Called a left child and a right child.  Used for many applications such as expression trees, binary searching, Huffman coding, etc..  Expression Trees: have a operator in the root of every subtree (all internal nodes), including the main root and every operand is a leaf node.  Huffman Coding Trees: every alphabet symbol is stored in a leaf node and its coding sequence is obtained by following the path from the root node to the leaf. Typically a left link is assigned a “0” code and a right link is assigned a “1” code. Huffman coding is based upon frequency of occurrence for the various letters in the alphabet with more frequently occurring letters assigned to shorter codes thus saving significant amounts of space in data storage, message transmission, etc.. Example: Suppose that we have a four letter alphabet consisting of a, b, c, and d, and e only. To encode four letters requires 2 bits. Suppose that these are assigned as follows: a = 00, b = 01, c = 10, and d = 11. Now suppose that we have a sentence of these letters which is 15 characters long. This sentence will require 30 bits to encode. Suppose that we also have some information about the frequency of occurrence of each of our letters and know that “a” occurs most frequently, followed by b and so on. A Huffman coding tree is built as shown below with the most frequently occurring letters closest to the root. 0 1 0 1 0 1 Reading the new codes from the tree we have: a = 0, b = 10, c = 100, and d = 101. Now suppose our 15 character sentence contains 8 a’s, 4 b’s, 2 c’s, and 1 d. With 1 5 a 7 3 d b b c b

the new code this sentence requires (81) + (42) + (23) + (13) bits = 8 + 8 + 6

  • 3 = 25 bits. The original code required 30 bits so we have save (30-25)/30 = 16%. Implementation public class BinTreeNode { protected Object data; protected BinTreeNode left; protected BinTreeNode right; private BinTreeNode() { } public BinTreeNode (Object x, BinTreeNode l, BinTreeNode r) { data = x; left = l; right = r; } // end constructor }// end BinTreeNode BinaryTree implementation left as an exercise. Do this just like we did for the linked list classes, define a binary tree class, define a constructor, and then define some of the methods for operating on the binary tree. How to Insert a Node Want to insert the left-child to the left of its parent and the right-child to the right of its parent. Common technique is to insert in order of arrival and always place element as close to the root as possible, given a choice. In other words do not arbitrarily make the tree taller. don’t insert here root 1 root 1 2 2

Postorder: processed in order left-child/right-child/root public void postorderTrav (BinTree) { if (root != null) { postorderTrav (left); postorderTrav (right); process (root); //assumed method } //endif } //end postorderTrav Expressions An infix expression represented as a tree can be evaluated with an inorder traversal of the tree. public void getExp (BinTreeNode t) { (1) if (t == null) return; (2) if ((t.left != null)) & (t.right != null)) System.out.print (“(“); (3) getEXP (t.left); (4) System.out.print (t.data); (5) getEXP (t.right); (6) if ((t.right.left == null) && (t.right.right == null)) System.out.print (“)”); } //end getEXP Example : Consider the expression “ (a + ((b – c) * d) t

a *

  • d b c

Trace: set t to root of the tree

  1. t != null
  2. t.left != null && t.right != null // so print “(“
  3. call getEXP(t.left) // t now points to t.left – the node with “a”
    1. t != null
    2. t.left == null && t.right == null // so no print
    3. call getEXP(t.left) // t now points to t.left.left {which is null in this case}
      1. t == null so return
    4. print t.data // so “a” is printed
    5. call getEXP(t.right)
      1. t == null so return
  4. System.out.print (t.data) // print out “+” the root node
  5. call getEXP(t.right) // t now point to t.right //node with “*”
  6. t != null
  7. t.left != null && t.right != null // so print “(“ output = “(a + (”
  8. call getEXP(t.left) //t now points to node with “-“
  9. t != null
  10. t.left != null && t.right != null // so print “(“
  11. call getEXP(t.left) // t now points to node containing “b”
  12. t != null
  13. t.left == null && t.right == null //so no print
  14. call getEXP(t.left)
  15. //t.left == null so return
  16. System.out.print(t.data) //so print “b”
  17. call getEXP(t.right)
  18. //t.right == null so return
  19. System.out.print(t.data) //so print “-“
  20. call getEXP(t.right) //t now points to node containing “c”
  21. t != null
  22. t.left ==null && t.right == null so no print
  23. call getEXP(t.left)
  24. //tleft == null so return
  25. System.out.print(t.data) //so print “c”
  26. call getEXP(t.right)
  27. //t.right ==null so return
  28. t.right.left!=null &&t.right.right!=null so no print
  29. t.right.left==null && t.right.right ==null so print “)”

Tree # Tree #

Tree # 40 30 70 10 32 60 90 35