Handout 4.0 - TREES.pdf

# Handout 4.0 - TREES.pdf

This preview shows pages 1–4. Sign up to view the full content.

TREES A is the root node B is the parent to D& E A is the ancestor/grand parent of D& E D and E are the descendants of A C is the sibling of B. G and E are not siblings H,I,J,K,L,M,N,O are leaves. Leaves have no children. A node which is not a leaf is known as an internal node. A,B,C,D,E,F,G are internal nodes A is at level zero. The depth (level) of H is 3. …………………….level 0 ………….. level 1 ……level 2 ……level 3 The height of the tree is the maximum levels the tree has. In the above case, 3 The degree of node is the number of nodes it has. All the nodes above each have a degree of 2. The tree structure represent a hierarchy e.g. the organization structure of a corporation , a basic family unit table of content of a book , the file system of an operation system. An ordered tree is one in which the children of each node has utmost two nodes. Like the tree above. Binary tree An example of binary trees- arithmetic expression (((2+3)4)+(4*3)/8)-(12+(7-(3+2))) O A B D H I E J K L M F C N G O A B D H I E J K L M F C N G - / + 4 + 2 3 4 3 x 8 12 + - 7 + 2 3

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

View Full Document
2 The tree above is complete because: At the i th level, there are 2 i nodes – it’s full. Every internal node having two children is not necessarily right to say it is complete. Check arithmetic example above If h is the height of the tree (for the tree above, height = 3). It means : - All the leaves are at level h. - The number of levels in the tree is 2 h - The number internal nodes is given by = 1 + 2 + 2 2 + … + 2 h-1 = 2 h – 1 - Total number of nodes is 2 h + 2 h – 1 = 2 h +1 – 1 = n If the tree is complete and has n nodes, - The number of leaves is (n + 1)/2 - The height of the tree is = log 2 (number of leaves) ADTs for trees generic container method : o size(), isEmpty(), element() Positional container method : o positional( ) - gives all position in trees as a sequence. It has no parameters, when invoked in a tree, it gives a sequence of all the nodes in the tree. o swapElement(p,q) – swap the elements at this positions . o replaceElement(p,e) – replaces the element at the position, p with e Query methods: o isRoot (p) – given a particular position , is the root of the tree? o IsInternal (P) – given a particular position in the tree, is P on an internal node o isExternal (p) – given a particular position , does it correspond to a leaf accessor method o root() – will return the position of the root o parent(p) – given a particular position P, the returns the position of the parent node O A B D H I E J K L M F C N G
3 o children(p) – given a particular position, p, return the children of the node. The children (if they are many) will be returned as a sequence of the positions.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern