BinaryTree

binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Outside the tree, there is often a reference to the "root" node (the ancestor of all nodes), if it exists. Any node in the data structure can be reached by starting at root node and repeatedly following references to either the left or right child. A tree which does not have any node other than root node is called a null tree. In a binary tree, a degree of every node is maximum two. A tree with n nodes has exactly n−1 branches or degree.

Binary trees are used to implement binary search trees and binary heaps.

  • rooted binary tree is a tree with a root node in which every node has at most two children.
  • full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree) is a tree in which every node other than the leaves has two children. Or, perhaps more clearly, every node in a binary tree has exactly 0 or 2 children. Sometimes a full tree is ambiguously defined as a perfect tree(see next).
  • perfect binary tree is a full binary tree in which all leaves are at the same depth or same level, and in which every parent has two children. (This is ambiguously also called a complete binary tree(see next).) An example of a perfect binary tree is the ancestry chart of a person to a given depth, as each person has exactly two biological parents (one mother and one father); note that this reverses the usual parent/child tree convention, and these trees go in the opposite direction from usual (root at bottom).
  • complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
  • balanced binary tree is commonly defined as a binary tree in which the depth of the two subtrees of every node differ by 1 or less,although in general it is a binary tree where no leaf is much farther away from the root than any other leaf. 
 1 public class BinaryTree {
 2     private Node root;
 3     public Node getRoot(){
 4         return this.root;
 5     }
 6     
 7     private static class Node{
 8         private int data;
 9         private Node partent;
10         private Node left;
11         private Node right;
12         
13         public Node getLeft(){
14             return this.left;
15         }
16         
17         public Node getRight(){
18             return this.right;
19         }
20         
21         public Node getParent(){
22             return this.partent;
23         }
24         
25         public int getData(){
26             return data;
27         }
28     }
29 
30     public static void main(String[] args) {
31         // TODO Auto-generated method stub
32 
33     }
34 
35 }

 

posted on 2013-04-01 00:28  melotang  阅读(174)  评论(0)    收藏  举报