8._The_Binary_Tree.pdf - Binary Trees CS221(A Data...

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

Binary Trees CS221(A) Data Structures & Algorithms
Image of page 1

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

Binary Trees Tree with no node having more than two children. A finite set of nodes, one of which is designated as the root. The root node may have at most two sub-trees, each of which is also a binary tree. The two sub-trees of a given node are ordered and we refer to them as left child and the right child. Respectively.
Image of page 2
Binary Trees Nodes in a binary tree may have zero, one or two children. A B C D E F G H
Image of page 3

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

Binary Trees The maximum number of nodes for an entire binary tree of depth k is. 2 k - 1 for k ≥ 1 A full binary tree of depth k is a binary tree with 2 k - 1 nodes. That is the maximum number of nodes a binary tree can have. A B C D E F G H Max. No of Node = 15
Image of page 4
Binary Tree Implementation typedef struct TreeNode *PtrToNode; typedef struct *PtrToNode Tree; Typedef struct *PtrToNode Root; struct TreeNode { ElementType Element; Tree Left; Tree Right; }
Image of page 5

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

Binary Tree Implementation Element Variable will serve as our data field. Left and Right points to the two sub-trees. Value NULL indicates the absence of a sub- tree. Root points at the root node of the tree. Root == NULL indicates an empty tree.
Image of page 6
Binary Tree Traversal Visiting each node exactly once, is called Traversal. When positioned at any given node a traversal function may Continue down to left sub-tree or Continue down to right sub-tree or Or process the current node
Image of page 7

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

Binary Tree Traversal To process the data item we have the following options.
Image of page 8
Image of page 9
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