Docsity

Trees - Advanced Programming - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Trees - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Trees

• Definitions Trees

  • 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 T 1 , T 2 ,…, Tk , with roots connected by a direct edge from r
  • For a tree of N nodes, there must be N- edges.

A

C (^) D E F G

H I

B

J (^) K L M N

P Q

• Path: a path from node n 1 to n k is a sequence of nodes n 1 , n 2 , …, n k such that

n i is the parent of n i+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 n i ; The length of this path is called the depth of n i ; 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.

  • 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 2 k-

Binary Trees

left child right child

root

left child right child

root

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+
  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?

2 d+1^ – 1 (must assume full tree)

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 log 2 (n) comparisons Example: Construct an Binary Search Tree (BST) for the data 555 501 701 358 513 561 797 345 490 724

Array Implementation:

47

25 77

11 43 65 93

7 17 31 44 68

555

555

501

555

501 701

555

501 701

358

555

501 701

358 513

555

501 701

358 513 561

555

501 701

358 513 561 797 555

501 701

358 513 561 797

345 490 345

555

501 701

358 513 561 797

490 724

(^555 501 701 358 513 561 797 345 490 )

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

In-order: (LNR)Sample recursive programs

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); } }

Post-order: (LRN) void postorder(tree_pointer ptr) { if (ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf(“%d”, ptr->data); } }

Example: Expression Tree

Example: An Expression Tree

  1. Perform in-order traversal (LNR)

A – B + C * E / F

  1. Perform post-order traversal (LRN)

A B – C E F / * +

  1. Perform pre-order traversal (NLR)

+ - A B * C / E F

A B (^) C /

E F

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?

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.