C++ 二叉樹的基本操作

來源:互聯網
上載者:User

C++實現二叉樹的基本操作

包括 添加節點、刪除節點、前序走訪、中序遍曆、後續遍曆、層序遍曆、最大值、最小值、二叉樹的高度

//Tree.h 標頭檔

#include <stdio.h>
 
class Tree
{
 private :
  //節點元素類型為結構體
   struct LinkNode
   {
   int data;
   LinkNode *left;
   LinkNode *right;
   LinkNode(const int& dat,LinkNode *l,LinkNode *r):data(dat),left(l),right(r){}
   };
 
   LinkNode *head;//表前端節點

   //添加節點
      void AddTreeNode(LinkNode *node,LinkNode *newNode);
   //顯示中序排列
   void ShowCLR(LinkNode *root);
      //顯示前序排列
   void ShowLCR(LinkNode *root);
      //顯示右序排列
   void ShowLRC(LinkNode *root);
      //高度
   int  Height(LinkNode *root);

  public :
   //添加節點
   bool AddTreeNode(int  data);
   //顯示中序排列
   bool ShowCLR();
   //顯示前序排列
      bool ShowLCR();
      //顯示右序排列
   bool ShowLRC();
   //前序排列
   bool Floor();
      //最小值
   bool Min(int** minValue);
   //最大值
   bool Max(int** maxValue);
   //是否是空樹
   bool Empty();
   //高度
   void Height(int** height);

   ~Tree()
   {
      delete[] head;
   }

   Tree()
   {
      head=new LinkNode(-1,NULL,NULL);
   }
};

//實現檔案Tree.cpp

#include "stdafx.h"
#include <stdio.h>
#include<iostream>
using namespace std ;
#include "Tree.h";
#include <queue>

//添加節點
void Tree::AddTreeNode(LinkNode *node,LinkNode *newNode)
{
 if(node->data>newNode->data)
 {
  if(node->left==NULL)
  {
   node->left=newNode;
  }else{
   AddTreeNode(node->left,newNode);
  }

 }else if(node->data<newNode->data)
 {
  if(node->right==NULL)
  {
   node->right=newNode;
  }else{
      AddTreeNode(node->right,newNode);
  }
 }

}

//添加節點
bool Tree::AddTreeNode(int data)
{
 LinkNode *node=new LinkNode(data,NULL,NULL);
    if(head->left==NULL)
 {
  head->left=node;
 }
 AddTreeNode(head->left,node);

 return true;
}

//中序遍曆
void Tree::ShowCLR(LinkNode *root)
{
 if(root!=NULL){
  cout<<root->data<<"  ";
 }
   
 if(root->left!=NULL)
 {
  ShowCLR(root->left);
 }

 if(root->right!=NULL)
 {
  ShowCLR(root->right);
 }
}

//中序遍曆
bool Tree::ShowCLR()
{
 if(Empty())
 {
   return false;
 }
 ShowCLR(head->left);
 
 return true;
}

//前序走訪
void Tree::ShowLCR(LinkNode *root)
{
 if(root->left!=NULL)
 {
  ShowLCR(root->left);
 }

 if(root!=NULL){
  cout<<root->data<<"  ";
 }
   
 if(root->right!=NULL)
 {
  ShowLCR(root->right);
 }
}

//前序走訪
bool Tree::ShowLCR()
{
 if(Empty())
 {
   return false;
 }
 ShowLCR(head->left);
 
 return true;
}

//後序遍曆
void Tree::ShowLRC(LinkNode *root)
{
 if(root->left!=NULL)
 {
  ShowLRC(root->left);
 }

 if(root->right!=NULL)
 {
  ShowLRC(root->right);
 }

 if(root!=NULL){
  cout<<root->data<<"  ";
 }
}

//後序遍曆
bool Tree::ShowLRC()
{
 if(Empty())
 {
   return false;
 }
 ShowLRC(head->left);
 
 return true;
}

//最小值
bool Tree::Min(int** minValue)
{
 if(Empty())
 {
   return false;
 }
 LinkNode *tmp=head->left;
 while(tmp!=NULL && tmp->left!=NULL)
 {
    tmp=tmp->left;
 }
    **minValue= tmp->data;

 return true;
}

//最大值
bool Tree::Max(int** maxValue)
{
 if(Empty())
 {
   return false;
 }
 LinkNode *tmp=head->left;
 while(tmp!=NULL && tmp->right!=NULL)
 {
    tmp=tmp->right;
 }
    **maxValue= tmp->data;

 return true;
}

//判斷樹是否為空白
bool Tree::Empty()
{
 return head->left==NULL;
}

//用隊列實現二叉樹層序遍曆
//1:添加根節點
//2:列印根節點的資料,添加根節點的子節點,彈出根節點
//3:迴圈第二步
bool Tree::Floor()
{
    queue<LinkNode*> q;
 LinkNode* cur=head->left;
 q.push(head->left);
 while(!q.empty())
 {
  cur=q.front();
  cout<<cur->data<<"  ";

  if(cur->left!=NULL){
   q.push(cur->left);
  }
  if(cur->right!=NULL)
  {
   q.push(cur->right);
  }
  q.pop();
 }
 return true;

}

//求兩個數中較大的一個
int max(int a,int b)
{
 if(a>b)
 {
  return a;
 }else
 {
  return b;
 }
}

//遞迴求二叉樹的高度
//二叉樹的高度就是左子樹和右子樹中較大一顆二叉樹的高度
int Tree::Height(LinkNode *root)
{
 if(root==NULL)
 {
  return 0;
 }
 return 1+max(Height(root->left),Height(root->right));
}

//用指向指標的指標接受二叉樹的高度
void Tree::Height(int** height)
{
 **height=Height(head->left);
}

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#include "Tree.h"
 
void TreeTest() ;

int main()
{
   TreeTest();
 int a=0;
 cin>>a;
    return 0;
}
 

 //指標類型調用屬性和方法用“->” 物件類型用“.”
 //變數在不需要使用的時候就要釋放掉它的記憶體
void TreeTest()                  
{                               
   Tree  tree;
   int a[]={1,3,6,7,8,2,4,9,10,5};
   for(int i=0;i<10;i++){
    tree.AddTreeNode(a[i]);
   }
   cout<<"-----------原始數組----------"<<endl;
   for(int i=0;i<10;i++)
   {
  cout<<a[i]<<"  "; 
   }

   cout<<endl;
   cout<<endl;
   cout<<"-----------中序排列----------"<<endl;
   tree.ShowCLR();
   cout<<endl;cout<<endl;
   cout<<"-----------前序排列----------"<<endl;
   tree.ShowLCR();
   cout<<endl;cout<<endl;
   cout<<"-----------後序排列----------"<<endl;
   tree.ShowLRC();
   cout<<endl;
   cout<<endl;
   cout<<"-----------層序排列----------"<<endl;
   cout<<endl;
   tree.Floor();
   cout<<endl;
   int min=-1;
   int *pmin=&min;
   tree.Max(&pmin);
    cout<<endl;
   cout<<"最大值:"<<min<<endl;
   cout<<endl;
   tree.Min(&pmin);
   cout<<"最小值:"<<min<<endl;
 
   cout<<endl;
   int h=-1;
   int *height=&h;
   tree.Height(&height);
   cout<<"高度:"<<h<<endl;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.