2012-11-21 32 views
18

只是想知道如果我能得到的形式打印漂亮二叉树一些提示打印二叉树:现在是它所打印在一个漂亮的方式

5 
    10 
      11 
      7 
       6 
    3 
      4 
      2 

是:

2 
    4 
    3 
    6 
    7 
    11 
    10 
    5 

我知道我的示例与我当前正在打印的内容相反,如果从当前打印的根目录下打印它并不重要。任何提示都非常赞赏我的完整问题:

如何修改我的印花使树看起来像一棵树?

//Binary Search Tree Program 

#include <iostream> 
#include <cstdlib> 
#include <queue> 
using namespace std; 

int i = 0; 

class BinarySearchTree 
{ 
    private: 
    struct tree_node 
    { 
     tree_node* left; 
     tree_node* right; 
     int data; 
    }; 
    tree_node* root; 

    public: 
    BinarySearchTree() 
    { 
     root = NULL; 
    } 

    bool isEmpty() const { return root==NULL; } 
    void print_inorder(); 
    void inorder(tree_node*); 
    void print_preorder(); 
    void preorder(tree_node*); 
    void print_postorder(); 
    void postorder(tree_node*); 
    void insert(int); 
    void remove(int); 
}; 

// Smaller elements go left 
// larger elements go right 
void BinarySearchTree::insert(int d) 
{ 
    tree_node* t = new tree_node; 
    tree_node* parent; 
    t->data = d; 
    t->left = NULL; 
    t->right = NULL; 
    parent = NULL; 

    // is this a new tree? 
    if(isEmpty()) root = t; 
    else 
    { 
     //Note: ALL insertions are as leaf nodes 
     tree_node* curr; 
     curr = root; 
     // Find the Node's parent 
     while(curr) 
     { 
     parent = curr; 
     if(t->data > curr->data) curr = curr->right; 
     else curr = curr->left; 
     } 

     if(t->data < parent->data) 
     { 
     parent->left = t; 
     } 
     else 
     { 
     parent->right = t; 
     } 
    } 
} 

void BinarySearchTree::remove(int d) 
{ 
    //Locate the element 
    bool found = false; 
    if(isEmpty()) 
    { 
     cout<<" This Tree is empty! "<<endl; 
     return; 
    } 

    tree_node* curr; 
    tree_node* parent; 
    curr = root; 

    while(curr != NULL) 
    { 
     if(curr->data == d) 
     { 
     found = true; 
     break; 
     } 
     else 
     { 
     parent = curr; 
     if(d>curr->data) curr = curr->right; 
     else curr = curr->left; 
     } 
    } 
    if(!found) 
    { 
     cout<<" Data not found! "<<endl; 
     return; 
    } 


    // 3 cases : 
    // 1. We're removing a leaf node 
    // 2. We're removing a node with a single child 
    // 3. we're removing a node with 2 children 

    // Node with single child 
    if((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL)) 
    { 
     if(curr->left == NULL && curr->right != NULL) 
     { 
     if(parent->left == curr) 
     { 
      parent->left = curr->right; 
      delete curr; 
     } 
     else 
     { 
      parent->right = curr->left; 
      delete curr; 
     } 
     } 
     return; 
    } 

    //We're looking at a leaf node 
    if(curr->left == NULL && curr->right == NULL) 
    { 
     if(parent->left == curr) 
     { 
     parent->left = NULL; 
     } 
     else 
     { 
     parent->right = NULL; 
     } 
     delete curr; 
     return; 
    } 


    //Node with 2 children 
    // replace node with smallest value in right subtree 
    if (curr->left != NULL && curr->right != NULL) 
    { 
     tree_node* chkr; 
     chkr = curr->right; 
     if((chkr->left == NULL) && (chkr->right == NULL)) 
     { 
     curr = chkr; 
     delete chkr; 
     curr->right = NULL; 
     } 
     else // right child has children 
     { 
     //if the node's right child has a left child 
     // Move all the way down left to locate smallest element 

     if((curr->right)->left != NULL) 
     { 
      tree_node* lcurr; 
      tree_node* lcurrp; 
      lcurrp = curr->right; 
      lcurr = (curr->right)->left; 
      while(lcurr->left != NULL) 
      { 
       lcurrp = lcurr; 
       lcurr = lcurr->left; 
      } 
      curr->data = lcurr->data; 
      delete lcurr; 
      lcurrp->left = NULL; 
     } 
     else 
     { 
      tree_node* tmp; 
      tmp = curr->right; 
      curr->data = tmp->data; 
      curr->right = tmp->right; 
      delete tmp; 
     } 

     } 
     return; 
    } 

} 
void BinarySearchTree::print_postorder() 
{ 
    postorder(root); 
} 

void BinarySearchTree::postorder(tree_node* p) 
{ 
    if(p != NULL) 
    { 
     if(p->left) postorder(p->left); 
     if(p->right) postorder(p->right); 
     cout<<"  "<<p->data<<"\n "; 
    } 
    else return; 
} 

int main() 
{ 
    BinarySearchTree b; 
    int ch,tmp,tmp1; 
    while(1) 
    { 
     cout<<endl<<endl; 
     cout<<" Binary Search Tree Operations "<<endl; 
     cout<<" ----------------------------- "<<endl; 
     cout<<" 1. Insertion/Creation "<<endl; 
     cout<<" 2. Printing "<<endl; 
     cout<<" 3. Removal "<<endl; 
     cout<<" 4. Exit "<<endl; 
     cout<<" Enter your choice : "; 
     cin>>ch; 
     switch(ch) 
     { 
      case 1 : cout<<" Enter Number to be inserted : "; 
        cin>>tmp; 
        b.insert(tmp); 
        i++; 
        break; 
      case 2 : cout<<endl; 
        cout<<" Printing "<<endl; 
        cout<<" --------------------"<<endl; 
        b.print_postorder(); 
        break; 
      case 3 : cout<<" Enter data to be deleted : "; 
        cin>>tmp1; 
        b.remove(tmp1); 
        break; 
      case 4: 
        return 0; 

     } 
    } 
} 
+0

http://www.leetcode.com/2010/09/printing-binary-tree-in-level-order.html – jpumford

回答

12

为了递归地漂亮地打印一棵树,你需要两个参数传递到您的打印功能:

  • 树节点要打印,并
  • 缩进程度

例如,你可以这样做:

void BinarySearchTree::postorder(tree_node* p, int indent=0) 
{ 
    if(p != NULL) { 
     if(p->left) postorder(p->left, indent+4); 
     if(p->right) postorder(p->right, indent+4); 
     if (indent) { 
      std::cout << std::setw(indent) << ' '; 
     } 
     cout<< p->data << "\n "; 
    } 
} 

最初的电话应该是postorder(root);

如果你想在顶部与根打印树,移动coutif的顶部。

+0

这很好,谢谢。 – user1840555

+0

[This](http://coliru.stacked-crooked.com/a/0da14664b6f6809e)满足了我对更多表现的渴望。 –

+0

@MooingDuck我同意!并且做一个完美的编辑:-) – dasblinkenlight

3

如果您只需要将您的树形象化,更好的方法是将其输出为点格式并用grapviz绘制。

你可以看一下dot guide为ABT语法等

+2

这可能是很好的注释。但不能被视为答案。 –

1

做一个中序遍历的更多信息,移动到兄弟姐妹之前下降到孩子。在每个级别,即当你下降到一个孩子时,增加缩进。在输出每个节点之后,打印一个换行符。

一些伪代码。请拨打Print与您的树的根。

void PrintNode(int indent, Node* node) 
{ 
    while (--indent >= 0) 
     std::cout << " "; 
    std::cout << node->value() << "\n"; 
} 

void PrintNodeChildren(int indent, Node* node) 
{ 
    for (int child = 0; child < node->ChildCount(); ++child) 
    { 
     Node* childNode = node->GetChild(child); 
     PrintNode(indent, childNode); 
     PrintNodeChildren(indent + 1, childNode); 
    } 
} 

void Print(Node* root) 
{ 
    int indent = 0; 
    PrintNode(indent, root); 
    PrintNodeChildren(indent + 1, root); 
} 
0

从你的根,计算你的左儿童人数。从留下的孩子总数中,继续留下留下孩子的人数。移动到树的下一个级别,对于左边的孩子,缩小的数量是缩小的,然后是右边孩子的初始两个凹痕。根据左边儿童的水平和父母的缩小程度,对其右边的兄弟姐妹进行双重剔除。

2

下面是一个以树形式打印基于数组的堆的小例子。它需要对较大数字的算法进行一些调整。我只是在纸上创建了一个网格,并计算出每个节点看起来不错的空间索引,然后注意到每个节点需要根据父节点的空间数量和递归级别需要多少空间,以及如何树高。该解决方案不仅仅是按照级别顺序打印并满足“美观”要求。

#include <iostream> 
#include <vector> 

static const int g_TerminationNodeValue = -999; 

class HeapJ 
{ 
public: 
HeapJ(int* pHeapArray, int numElements) 
{ 
    m_pHeapPointer = pHeapArray; 
    m_numElements = numElements; 

    m_treeHeight = GetTreeHeight(1); 
} 

void Print() 
{ 
    m_printVec.clear(); 

    int initialIndex = 0; 
    for(int i=1; i<m_treeHeight; ++i) 
    { 
     int powerOfTwo = 1; 
     for(int j=0; j<i; ++j) 
     { 
      powerOfTwo *= 2; 
     } 

     initialIndex += powerOfTwo - (i-1); 
    } 

    DoPrintHeap(1,0,initialIndex); 

    for(size_t i=0; i<m_printVec.size(); ++i) 
    { 
     std::cout << m_printVec[i] << '\n' << '\n'; 
    } 
} 

private: 
int* m_pHeapPointer; 
int m_numElements; 
int m_treeHeight; 
std::vector<std::string> m_printVec; 

int GetTreeHeight(int index) 
{ 
    const int value = m_pHeapPointer[index-1]; 

    if(value == g_TerminationNodeValue) 
    { 
     return -1; 
    } 

    const int childIndexLeft = 2*index; 
    const int childIndexRight = childIndexLeft+1; 

    int valLeft = 0; 
    int valRight = 0; 

    if(childIndexLeft <= m_numElements) 
    { 
     valLeft = GetTreeHeight(childIndexLeft); 
    } 

    if(childIndexRight <= m_numElements) 
    { 
     valRight = GetTreeHeight(childIndexRight); 
    } 

    return std::max(valLeft,valRight)+1; 
} 

void DoPrintHeap(int index, size_t recursionLevel, int numIndents) 
{ 
    const int value = m_pHeapPointer[index-1]; 

    if(value == g_TerminationNodeValue) 
    { 
     return; 
    } 

    if(m_printVec.size() == recursionLevel) 
    { 
     m_printVec.push_back(std::string("")); 
    } 

    const int numLoops = numIndents - (int)m_printVec[recursionLevel].size(); 
    for(int i=0; i<numLoops; ++i) 
    { 
     m_printVec[recursionLevel].append(" "); 
    } 

    m_printVec[recursionLevel].append(std::to_string(value)); 

    const int childIndexLeft = 2*index; 
    const int childIndexRight = childIndexLeft+1; 

    const int exponent = m_treeHeight-(recursionLevel+1); 
    int twoToPower = 1; 
    for(int i=0; i<exponent; ++i) 
    { 
     twoToPower *= 2; 
    } 
    const int recursionAdjust = twoToPower-(exponent-1); 

    if(childIndexLeft <= m_numElements) 
    { 
     DoPrintHeap(childIndexLeft, recursionLevel+1, numIndents-recursionAdjust); 
    } 

    if(childIndexRight <= m_numElements) 
    { 
     DoPrintHeap(childIndexRight, recursionLevel+1, numIndents+recursionAdjust); 
    } 
} 
}; 

const int g_heapArraySample_Size = 14; 
int g_heapArraySample[g_heapArraySample_Size] = {16,14,10,8,7,9,3,2,4,1,g_TerminationNodeValue,g_TerminationNodeValue,g_TerminationNodeValue,0}; 

int main() 
{ 
    HeapJ myHeap(g_heapArraySample,g_heapArraySample_Size); 
    myHeap.Print(); 

    return 0; 
} 

/* output looks like this: 

      16 

    14   10 

    8  7  9  3 

2 4 1   0 

*/ 
+0

可能值得一提的是'std :: to_string()'可用于-std = C++ 11'或类似的标准版本标志。它不适用于'-std = C++ 03'或'-std = C++ 98'。 –

0

对于数组我觉得这更简洁。仅仅通过阵列。可以改进以处理非常大的数字(长数字长度)。复制并粘贴在C++ :)

#include <math.h> 
using namespace std; 
void printSpace(int count){ 
    for (int x = 0; x<count; x++) { 
     cout<<"-"; 
    } 
} 
void printHeap(int heap[], int size){ 
    cout<<endl; 
    int height = ceil(log(size)+1); //+1 handle the last leaves 
    int width = pow(2, height)*height; 
    int index = 0; 
    for (int x = 0; x <= height; x++) { //for each level of the tree 
     for (int z = 0; z < pow(2, x); z++) { // for each node on that tree level 
      int digitWidth = 1; 
      if(heap[index] != 0) digitWidth = floor(log10(abs(heap[index]))) + 1; 
      printSpace(width/(pow(2,x))-digitWidth); 
      if(index<size)cout<<heap[index++]; 
      else cout<<"-"; 
      printSpace(width/(pow(2,x))); 
     } 
     cout<<endl; 
    } 
} 
0

这里是预购例程打印以紧凑的方式一般树图:

 void preOrder(Node* nd, bool newLine=false,int indent=0) 
     { 
       if(nd != NULL) {  
         if (newLine && indent) { 
           std::cout << "\n" << std::setw(indent) << ' ' 
         } else if(newLine) 
           std::cout << "\n"; 
         cout<< nd->_c; 
         vector<Node *> &edges=nd->getEdges(); 
         int eSize=edges.size(); 
         bool nwLine=false; 
         for(int i=0; i<eSize; i++) { 
           preOrder(edges[i],nwLine,indent+1); 
           nwLine=true; 
         } 
       } 
     } 

int printGraph() 
{ 
    preOrder(root,true); 
} 
15
void btree::postorder(node* p, int indent) 
{ 
    if(p != NULL) { 
     if(p->right) { 
      postorder(p->right, indent+4); 
     } 
     if (indent) { 
      std::cout << std::setw(indent) << ' '; 
     } 
     if (p->right) std::cout<<" /\n" << std::setw(indent) << ' '; 
     std::cout<< p->key_value << "\n "; 
     if(p->left) { 
      std::cout << std::setw(indent) << ' ' <<" \\\n"; 
      postorder(p->left, indent+4); 
     } 
    } 
} 

有了这个树:

btree *mytree = new btree(); 
mytree->insert(2); 
mytree->insert(1); 
mytree->insert(3); 
mytree->insert(7); 
mytree->insert(10); 
mytree->insert(2); 
mytree->insert(5); 
mytree->insert(8); 
mytree->insert(6); 
mytree->insert(4); 
mytree->postorder(mytree->root); 

会导致这样的结果:

enter image description here

+0

你的例子显示调用函数'postOrder(node * p)',但你声明的函数是'postOrder(node * p,int indent)'?缩进的初始值是多少? –

5

它永远不会很漂亮,除非有人做一些回溯来重新校准显示输出。但是可以使用启发式方法有效地发射出足够多的二叉树:给定树的高度,可以猜测不同深度的节点的期望宽度和伪距。 有几件需要做到这一点,所以让我们先从更高级别的函数开始提供上下文。

漂亮的打印功能:

// create a pretty vertical tree 
    void postorder(Node *p) 
    { 
     int height = getHeight(p) * 2; 
     for (int i = 0 ; i < height; i ++) { 
     printRow(p, height, i); 
     } 
    } 

上面的代码很容易。主要的逻辑是在printRow函数中。我们来深入研究一下。

void printRow(const Node *p, const int height, int depth) 
{ 
     vector<int> vec; 
     getLine(p, depth, vec); 
     cout << setw((height - depth)*2); // scale setw with depth 
     bool toggle = true; // start with left 
     if (vec.size() > 1) { 
       for (int v : vec) { 
         if (v != placeholder) { 
           if (toggle) 
             cout << "/" << " "; 
           else 
             cout << "\\" << " "; 
         } 
         toggle = !toggle; 
       } 
       cout << endl; 
       cout << setw((height - depth)*2); 
     } 
     for (int v : vec) { 
       if (v != placeholder) 
         cout << v << " "; 
     } 
     cout << endl; 
} 

getLine()做你所期望的:它将具有给定的相同深度的所有节点存储到vec中。下面是该代码:

void getLine(const Node *root, int depth, vector<int>& vals) 
{ 
     if (depth <= 0 && root != nullptr) { 
       vals.push_back(root->val); 
       return; 
     } 
     if (root->left != nullptr) 
       getLine(root->left, depth-1, vals); 
     else if (depth-1 <= 0) 
       vals.push_back(placeholder); 
     if (root->right != nullptr) 
       getLine(root->right, depth-1, vals); 
     else if (depth-1 <= 0) 
       vals.push_back(placeholder); 
} 

现在回到printRow()。对于每一行,我们根据二叉树中的深度来设置流的宽度。这种格式会很好,因为通常情况下,你走得越深,需要的宽度就越大。我说通常是因为在退化的树木中,这看起来不太漂亮。只要树大致平衡和小(< 20项),它应该很好。 需要占位符来正确对齐“/”和“\”字符。所以当通过getLine()获得一行时,如果没有任何节点出现在指定的深度,我们插入占位符。例如,占位符可以设置为任何类似(1<<31)。显然,这是不健壮的,因为占位符可能是一个有效的节点值。如果一个编码器被强制执行并且只处理小数,那么可以修改代码以通过getLine()发送经过十进制转换的字符串,并使用像“_”这样的占位符。 (不幸的是,我不是这样的编码器:P)

用于插入顺序的以下项目的结果:8,12,4,2,5,15是

 8 
    / \ 
    4 12 
/ \ \ 
    2 5 15 

的getHeight()是作为练习留给读者。 :) 通过追溯更新基于深度节点中项目数量的浅节点的setw,甚至可以获得更漂亮的结果。 这也是作为练习留给读者的。

5
//Binary tree (pretty print): 
    //      ________________________50______________________       
    //   ____________30         ____________70__________    
    //  ______20____           60    ______90   
    //  10   15               80     


    // prettyPrint 
    public static void prettyPrint(BTNode node) { 
     // get height first 
     int height = heightRecursive(node); 

     // perform level order traversal 
     Queue<BTNode> queue = new LinkedList<BTNode>(); 

     int level = 0; 
     final int SPACE = 6; 
     int nodePrintLocation = 0; 

     // special node for pushing when a node has no left or right child (assumption, say this node is a node with value Integer.MIN_VALUE) 
     BTNode special = new BTNode(Integer.MIN_VALUE); 

     queue.add(node); 
     queue.add(null); // end of level 0 
     while(! queue.isEmpty()) { 
      node = queue.remove(); 

      if (node == null) { 
       if (!queue.isEmpty()) { 
        queue.add(null); 
       } 

       // start of new level 
       System.out.println(); 
       level++; 
      } else { 
       nodePrintLocation = ((int) Math.pow(2, height - level)) * SPACE; 

       System.out.print(getPrintLine(node, nodePrintLocation)); 

       if (level < height) { 
        // only go till last level 
        queue.add((node.left != null) ? node.left : special); 
        queue.add((node.right != null) ? node.right : special); 
       } 
      } 
     }  
    } 
    public void prettyPrint() { 
     System.out.println("\nBinary tree (pretty print):"); 
     prettyPrint(root); 
    } 
    private static String getPrintLine(BTNode node, int spaces) { 
     StringBuilder sb = new StringBuilder(); 

     if (node.data == Integer.MIN_VALUE) { 
      // for child nodes, print spaces 
      for (int i = 0; i < 2 * spaces; i++) { 
       sb.append(" "); 
      } 

      return sb.toString(); 
     } 

     int i = 0; 
     int to = spaces/2; 

     for (; i < to; i++) { 
      sb.append(' '); 
     } 
     to += spaces/2; 
     char ch = ' '; 
     if (node.left != null) { 
      ch = '_'; 
     } 
     for (; i < to; i++) { 
      sb.append(ch); 
     } 

     String value = Integer.toString(node.data); 
     sb.append(value); 

     to += spaces/2; 
     ch = ' '; 
     if (node.right != null) { 
      ch = '_'; 
     } 
     for (i += value.length(); i < to; i++) { 
      sb.append(ch); 
     } 

     to += spaces/2; 
     for (; i < to; i++) { 
      sb.append(' '); 
     } 

     return sb.toString(); 
    } 

    private static int heightRecursive(BTNode node) { 
     if (node == null) { 
      // empty tree 
      return -1; 
     } 

     if (node.left == null && node.right == null) { 
      // leaf node 
      return 0; 
     } 

     return 1 + Math.max(heightRecursive(node.left), heightRecursive(node.right)); 
    } 
0

我有一个更简单的代码.......... 考虑由结构的节点树

struct treeNode{ 
    treeNode *lc; 
    element data; 
    short int bf; 
    treeNode *rc; 
}; 

树的深度可以发现使用

int depth(treeNode *p){ 
    if(p==NULL) return 0; 
    int l=depth(p->lc); 
    int r=depth(p->rc); 
    if(l>=r) 
     return l+1; 
    else 
     return r+1; 
} 

下面gotoxy功能将光标移动到所需的位置

void gotoxy(int x,int y) 
{ 
printf("%c[%d;%df",0x1B,y,x); 
} 

然后打印树可以这样做:

void displayTreeUpDown(treeNode * root,int x,int y,int px=0){ 
if(root==NULL) return; 
gotoxy(x,y); 
int a=abs(px-x)/2; 
cout<<root->data.key; 
displayTreeUpDown(root->lc,x-a,y+1,x); 
displayTreeUpDown(root->rc,x+a,y+1,x); 
} 

可使用被称为:

display(t,pow(2,depth(t)),1,1); 
0

这里是我的代码。它打印得非常好,也许它不完美对称。 小描述:

  • 第一功能 - 通过水平打印水平(根LV - >叶LV)
  • 第二功能 - 从新线
  • 第三函数的开始距离 - 打印节点并计算之间的距离两张印刷品;

void Tree::TREEPRINT() 
{ 
    int i = 0; 
    while (i <= treeHeight(getroot())){ 
     printlv(i); 
     i++; 
     cout << endl; 
    } 
} 

void Tree::printlv(int n){ 
    Node* temp = getroot(); 
    int val = pow(2, treeHeight(root) -n+2); 
    cout << setw(val) << ""; 
    prinlv(temp, n, val); 
} 

void Tree::dispLV(Node*p, int lv, int d) 
{ 
    int disp = 2 * d; 
    if (lv == 0){ 
     if (p == NULL){ 

      cout << " x "; 
      cout << setw(disp -3) << ""; 
      return; 
     } 
     else{ 
      int result = ((p->key <= 1) ? 1 : log10(p->key) + 1); 
      cout << " " << p->key << " "; 
      cout << setw(disp - result-2) << ""; 
     } 
    } 
    else 
    { 
     if (p == NULL&& lv >= 1){ 
      dispLV(NULL, lv - 1, d); 
      dispLV(NULL, lv - 1, d); 
     } 
     else{ 
      dispLV(p->left, lv - 1, d); 
      dispLV(p->right, lv - 1, d); 
     } 
    } 
} 

输入:

50-28-19-30-29-17-42-200-160-170-180-240-44-26-27 

输出:https://i.stack.imgur.com/TtPXY.png

+1

请不要粘贴图像作为_output_,prefere **总是**粘贴(和格式化)您的代码/追溯/日志在SO – Arount

1
#include <stdio.h> 
#include <stdlib.h> 

struct Node 
{ 
    struct Node *left,*right; 
    int val; 
} *root=NULL; 

int rec[1000006]; 
void addNode(int,struct Node*); 
void printTree(struct Node* curr,int depth) 
{ 
    int i; 
    if(curr==NULL)return; 
    printf("\t"); 
    for(i=0;i<depth;i++) 
     if(i==depth-1) 
      printf("%s\u2014\u2014\u2014",rec[depth-1]?"\u0371":"\u221F"); 
     else 
      printf("%s ",rec[i]?"\u23B8":" "); 
    printf("%d\n",curr->val); 
    rec[depth]=1; 
    printTree(curr->left,depth+1); 
    rec[depth]=0; 
    printTree(curr->right,depth+1); 
} 
int main() 
{ 
    root=(struct Node*)malloc(sizeof(struct Node)); 
    root->val=50; 
    //addNode(50,root); 
    addNode(75,root); addNode(25,root); 
    addNode(15,root); addNode(30,root); 
    addNode(100,root); addNode(60,root); 
    addNode(27,root); addNode(31,root); 
    addNode(101,root); addNode(99,root); 
    addNode(5,root); addNode(61,root); 
    addNode(55,root); addNode(20,root); 
    addNode(0,root); addNode(21,root); 
    //deleteNode(5,root); 

    printTree(root,0); 
    return 0; 
} 

void addNode(int v,struct Node* traveller) 
{ 
    struct Node *newEle=(struct Node*)malloc(sizeof(struct Node)); 
    newEle->val=v; 
    for(;;) 
    { 
     if(v<traveller->val) 
     { 
      if(traveller->left==NULL){traveller->left=newEle;return;} 
      traveller=traveller->left; 
     } 
     else if(v>traveller->val) 
     { 
      if(traveller->right==NULL){traveller->right=newEle;return;} 
      traveller=traveller->right; 
     } 
     else 
     { 
      printf("%d Input Value is already present in the Tree !!!\n",v); 
      return; 
     } 
    } 
} 

希望,你会发现它非常......

输出:

50 
ͱ———25 
⎸ ͱ———15 
⎸ ⎸ ͱ———5 
⎸ ⎸ ⎸ ͱ———0 
⎸ ⎸ ∟———20 
⎸ ⎸  ∟———21 
⎸ ∟———30 
⎸  ͱ———27 
⎸  ∟———31 
∟———75 
    ͱ———60 
    ⎸ ͱ———55 
    ⎸ ∟———61 
    ∟———100 
      ͱ———99 
      ∟———101