Docsity

Binary Tree Properties, Number of Nodes, and Representation, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Binary Tree Properties & Representation
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Binary Tree Properties, Number of Nodes, and Representation and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Binary Tree Properties & Representation

Minimum Number Of Nodes • Minimum number of nodes in a binary treewhose height is h. • At least one node at each of first h levels.

minimum number ofnodes is h

  • Maximum Number Of Nodes • All possible nodes at first h levels are present. Maximum number of nodes = 1 + 2 + 4 + 8 + … +
    • h-
      • h = 2-
  • Number Of Nodes & Height • Let n be the number of nodes in a binarytree whose height is h.h • h <= n <=^2 – 1 • log (n+1) <= h <= n

Full Binary Tree

• A full binary tree of a given height h has 2

h^ – 1

nodes.^ Height 4 full binary tree.

Node Number Properties

• Parent of node i is node i / 2, unless i = 1. • Node 1 is the root and has no parent.

Node Number Properties

• Left child of node i is node 2i, unless 2i > n,where n is the number of nodes. • If 2i > n, node i has no left child.

Complete Binary Tree With n Nodes^ • Start with a full binary tree that has at leastn nodes.^ • Number the nodes as described earlier.^ • The binary tree defined by the nodesnumbered 1 through n is the unique n nodecomplete binary tree.

Example^12

• Complete binary tree with 10 nodes.

Array Representation • Number the nodes using the numbering schemefor a full binary tree. The node that is numberedi is stored in tree[i].^ b

a c d^ e

f^ g h^ i^ j

8 9 10 tree[]^0

a^ b^ c^ d^ e^ f^ g^ h^ i^ j^5

Right-Skewed Binary Tree

(^1) a^3 b^7 c^ d

tree[]^0

a^ -^ b^ -^ -^ -^ c^ -^ -^ -^5

-^ -^ -^ - d^15

• An n node binary tree needs an array whoselength is between n+1 and 2

n.

The Class BinaryTreeNode package dataStructures; public class BinaryTreeNode { Object element; BinaryTreeNode leftChild; // left subtree BinaryTreeNode rightChild;// right subtree // constructors and any other methods // come here }

Linked Representation Example

a^ cb d

f

e g^ h

root^ leftChildelementrightChild

Binary Tree Traversal • Many binary tree operations are done byperforming a traversal of the binary tree. • In a traversal, each element of the binary tree isvisited exactly once. • During the visit of an element, all action (makea clone, display, evaluate the operator, etc.)with respect to this element is taken.

Binary Tree Traversal Methods • Preorder • Inorder • Postorder • Level order