lec04-trees - What is a(General Tree A(general tree is a...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
What is a (General) Tree? A (general) tree is a set of nodes with the following properties: The set can be empty. Otherwise, the set is partitioned into k+1 disjoint subsets: a tree consists of a distinguished node r, called root , and zero or more nonempty subtrees T 1 , T 2 , … , T k , each of whose roots are connected by an edge to r. 17/10/2011 CS202 - Fundamentals of Computer Science II 1 T is a tree if either: T has no nodes, or T is of the form r . . . T 1 T 2 . . . . . . T k where r is a node and T 1 , T 2 , ..., T k are trees.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What is a (General) Tree? (cont.) The root of each subtree is said to be a child of r, and r is the parent of each subtree’s root. If a tree is a collection of N nodes, then it has N-1 edges. A path from node n to n is defined as a sequence of nodes n ,n , …,n 17/10/2011 CS202 - Fundamentals of Computer Science II 2 1 k is defined as a sequence of nodes n 1 ,n 2 , …,n k such that n i is the parent of n i+1 (1 i < k): There is a path from every node to itself. There is exactly one path from the root to each node.
Image of page 2
A Tree – Example A B C D E F G H I J K L M N 17/10/2011 CS202 - Fundamentals of Computer Science II 3 P Q –Node A has 6 children : B, C, D, E, F, G. –B, C, H, I, P, Q, K, L, M, N are leaves in the tree above. –K, L, M are siblings since F is their parent.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Tree Terminology Parent – The parent of node n is the node directly above n in the tree. Child – The child of node n is the node directly below n in the tree. If node m is the parent of node n, node n is the child of node m. Root – The only node in the tree with no parent. Leaf – A node with no children. Siblings – Nodes with a common parent. 17/10/2011 CS202 - Fundamentals of Computer Science II 4 Ancestor – An ancestor of node n is a node on the path from the root to n. Descendant – A descendant of node n is a node on the path from n to a leaf. Subtree – A subtree of node n is a tree that consists of a child (if any) of n and the child’s descendants (a tree which is rooted by a child of node n).
Image of page 4
Level of a Node Level – The level of node n is the number of nodes on the path from the root to node n. Definition – The level of node n in a tree T: If n is the root of T, the level of n is 1. If n is not the root of T, its level is 1 greater than the level of its 17/10/2011 CS202 - Fundamentals of Computer Science II 5 parent.
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Height of a Tree Height – The number of nodes on the longest path from the root to a leaf. The height of a tree T in terms of the levels of its nodes is defined as: If T is empty, its height is 0. If T is not empty, its height is equal to the maximum level of its nodes. 17/10/2011 CS202 - Fundamentals of Computer Science II 6 Or, the height of a tree T can be defined recursively as: If T is empty, its height is 0.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern