Professional Documents
Culture Documents
Hierarchical data structure Each position in the tree is called a node The top of the tree is called the root The nodes immediately below a node are called its children; nodes with no children are called leaves (or terminal nodes), or terminal nodes, and the node above a given node is its parent (or father) A node x is ancestor of node y if x is father of y or father of some ancestor of y. y is called descendent of x. Ancestor of a node is its parent, grand parent or grand-grand parent or so on.
Node H
Node I
Node J
Node K Node L
Nodes C, H, and I form a subtree Nodes H and I are siblings C is Hs parent, H and I are children of C What is the depth of this tree?
Binary Trees
A commonly used type of tree is a binary tree Each node has at most two children The tree is a conceptual structure The data can be stored either in a dynamic linked tree structure, or in contiguous memory cells (array) according to a set pattern; in other words, implementation can be pointer-based or array-based
Binary trees
A tree is called strictly binary tree if every non leaf node has exactly two children. A strictly binary tree having n leaves always contain 2n-1 nodes Text book definition of depth of a tree
Depth of binary tree is maximum level of any leaf in the tree. Root of tree is at level 0, and level of any other node in the tree is one more than the level of its father
Binary Trees
A complete binary tree of level d is the strictly binary tree all of whose leaves are at level d A complete binary tree of level d has 2l nodes at each level l where 0<=l<=d Total number of nodes (tn) in a complete binary tree of depth d will be:
d
tn ! 2 2 2 2
2 j ! 2 d 1 1
j !0
Binary Trees
For a complete binary tree if number of nodes (tn) are known, then we may find depth of the binary tree tn ! 2 d 1 1
Add and remove nodes by manipulating pointers Leaf nodes have child pointers set to null
class CBinTree { struct Node { int value; Node *LeftChild,*RightChild; }*Root; /***Operations*********/ /************************/ };
ROOT
Value in root node stored first, followed by left child, then right child Each successive level in the tree stored left to right; unused nodes in tree represented by a bit pattern to indicate nothing stored there Children of any given node n is stored in cells 2n and 2n + 1 (If array index starts at 1) Can calculate position for any given node Storage allocated as for full tree, even if many nodes empty For a tree of depth h we need array of 2h+1-1 cells
Binary Trees
Full binary tree of height (depth) h: all nodes at a height less than h have exactly two children Balanced binary tree: for each node, the difference in depth of the right and left subtrees is no more than one Completely balanced tree: left and right subtrees of every node have the same height
Primitive Operations
Left(node): Gives index/pointer of left child Right(node): Gives index/pointer of right child Parent(node): Returns index/pointer of parent Brother(node): Returns index/pointer of brother Root: Gives index/pointer of root node Info(Node): Data/Info stored at node IsLeft(node): Is node left child? Yes/No IsRight(node): Is node right child? Yes/No
Common Operations
Tree traversal Node addition Node deletion Destroy
Trees Traversal
Inorder
(Left) Root (Right)
Root
Preorder
Root (Left) (Right)
Left
Right
Postorder
(Left) (Right) Root
Inorder Traversal
+
(A*B)+(C*D+E)
Preorder Traversal
+
Root Left Right manner + Left Right + [*Left Right] [+Left Right] +(*AB) [+ *Left Right E] +*AB + *C D E
Postorder Traversal
+
Left Right + [Left Right *] [Left Right+] + (AB*) [Left Right * E + ]+ (AB*) [C D * E + ]+ AB* C D * E + +
Need to define greater than and less than for the specific data